简体   繁体   English

在 kubernetes 中运行 rails + postgres:连接被拒绝

[英]Running rails + postgres in kubernetes: Connection refused

My rails deployment is not able to connect to the postgres database.我的 rails 部署无法连接到 postgres 数据库。 Error in the logs:日志中的错误:

PG::ConnectionBad (could not connect to server: Connection refused
  Is the server running on host "db" (10.0.105.11) and accepting
  TCP/IP connections on port 5432?
):

This is my kubectl get services output:这是我的kubectl get services output:

NAME                     TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
load-balancer            LoadBalancer   10.0.5.147     60.86.4.33    80:31259/TCP   10m
db                       ClusterIP      10.0.105.11    <none>        5432/TCP       10m
kubernetes               ClusterIP      10.0.0.1       <none>        443/TCP        10m
web                      ClusterIP      10.0.204.107   <none>        3000/TCP       10m

db-deployment.yaml: db-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: db
  name: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
      - env:
        - name: POSTGRES_DB
          value: postgres
        - name: POSTGRES_HOST_AUTH_METHOD
          value: trust
        - name: POSTGRES_USER
          value: postgres
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        ports:
        - containerPort: 5432
        image: postgres
        imagePullPolicy: ""
        name: db
        resources: {}
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgres
      restartPolicy: Always
      serviceAccountName: ""
      volumes:
      - name: postgres
        persistentVolumeClaim:
          claimName: postgres
status: {}

db-service.yaml: db-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: db
  labels:
    app: db
spec:
  ports:
    - port: 5432
  selector:
    app: db
    tier: database

config/database.yml:配置/数据库.yml:

default: &default
  adapter: postgresql
  encoding: utf8
  host: db
  username: postgres
  pool: 5

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production

How do I ensure the postgres instance is running and accepting connections?如何确保 postgres 实例正在运行并接受连接? Did I misconfigure something in the manifest files, or does the database.yml file need to point to a different host?我是否在清单文件中配置错误,或者 database.yml 文件是否需要指向不同的主机?

The first thing that jumps out at me is that your db service is targeting two selectors app: db and tier: database , however the corresponding deployment.yml only has the db label.我首先想到的是您的db服务针对两个选择器app: dbtier: database ,但是相应的deployment.yml只有db label。 You will need to add the tier label to your deployment template metadata so that the service will appropriately target the right pod.您需要将tier label 添加到您的部署模板元数据,以便该服务将适当地定位正确的 pod。

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: db
    tier: database 
  name: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db
      tier: database
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: db
        tier: database

In general, if a service is not connecting to a backend pod, you can easily diagnose the issue with a simple kubectl command.通常,如果服务未连接到后端 pod,您可以使用简单的 kubectl 命令轻松诊断问题。
This will tell you what current selectors are applied to your service这将告诉您哪些当前选择器应用于您的服务

kubectl describe service db -o yaml

Then you can fetch the pods that the selectors refer to and make sure that this is returning something.然后你可以获取选择器引用的 pod 并确保它返回了一些东西。

kubectl get pods -l app=db -l tier=database

Finally, I would recommend using the DNS name of the service setup through kube-proxy instead of the cluster IP address.最后,我建议通过 kube-proxy 使用服务设置的DNS名称,而不是集群 IP 地址。 I tend to view this as more resilient then a cluster up, as it will be automatically routed if the services IP ever changes.我倾向于认为这比集群更具弹性,因为如果服务 IP 发生变化,它将自动路由。 In your connection string use db:5432 instead of 10.0.105.11 .在您的连接字符串中使用db:5432而不是10.0.105.11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM