GKE Pod未连接到Cloudsql

时间:2019-04-04 03:59:48

标签: kubernetes google-cloud-platform google-cloud-sql google-kubernetes-engine cloud-sql-proxy

我的应用似乎无法连接到代理,因此无法连接到Cloudsql数据库。

下面是我的设置:

my-simple-app.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    name: web
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      name: web
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  minReadySeconds: 5
  template:
    metadata:
      labels:
        name: web
    spec:
      nodeSelector:
        cloud.google.com/gke-nodepool: default-pool
      containers:
      - image: joelaw/nameko-hello:0.2
        name: web
        env:
          - name: DB_HOST
            value: 127.0.0.1
          - name: DB_PASSWORD
            valueFrom:
              secretKeyRef:
                name: cloudsql-db-credentials
                key: password
          - name: DB_USER
            valueFrom:
              secretKeyRef:
                name: cloudsql-db-credentials
                key: username
        ports:
        - containerPort: 3000
          name: http-server
      - image: gcr.io/cloudsql-docker/gce-proxy:1.09
        name: cloudsql-proxy
        command: ["/cloud_sql_proxy", "--dir=/cloudsql",
                  "-instances=spheric-veric-task:asia-southeast1:authdb:5432",
                  "-credential_file=/secrets/cloudsql/credentials.json"]
        volumeMounts:
          - name: cloudsql-instance-credentials
            mountPath: /secrets/cloudsql
            readOnly: true
          - name: ssl-certs
            mountPath: /etc/ssl/certs
          - name: cloudsql
            mountPath: /cloudsql
      volumes:
        - name: cloudsql-instance-credentials
          secret:
            secretName: cloudsql-instance-credentials
        - name: ssl-certs
          hostPath:
            path: /etc/ssl/certs
        - name: cloudsql
          emptyDir:

我猜想我已经正确设置了机密。

以下是我从实例中收集的一些数据:

吊舱愉快地生活:

web-69c7777c68-s2jt6   2/2       Running   0          9m
web-69c7777c68-zbwtv   2/2       Running   0          9m

当我跑步时:kubectl logs web-69c7777c68-zbwtv -c cloudsql-proxy

它记录了这一点:

2019/04/04 03:25:35 using credential file for authentication; email=auth-db-user@spheric-verve-228610.iam.gserviceaccount.com
2019/04/04 03:25:35 Listening on /cloudsql/spheric-veric-task:asia-southeast1:authdb:5432/.s.PGSQL.5432 for spheric-veric-task:asia-southeast1:authdb:5432
2019/04/04 03:25:35 Ready for new connections

由于未将应用程序配置为连接到数据库,所以我要做的是使用以下命令SSH到pod中:

kubectl exec -it web-69c7777c68-mrdpn -- /bin/bash
# Followed by installing postgresql driver:
apt-get install postgresql
# Trying to connect to cloudsql:
psql -h 127.0.0.1 -p 5432 -U

当我在容器中运行psql时:

psql: could not connect to server: Connection refused
        Is the server running on host "127.0.0.1" and accepting
        TCP/IP connections on port 5432?

你们中的任何人都可以建议我该怎么做才能连接到数据库吗?

1 个答案:

答案 0 :(得分:1)

您指定的实例连接字符串错误,因此代理正在监听/cloudsql/目录中的Unix套接字,而不是TCP端口。

要告诉代理侦听TCP端口,请使用以下命令:

-instances=<INSTANCE_CONNECTION_NAME>=tcp:5432

否则,以下格式将创建unix套接字(默认为/cloudsql目录):

-instances=<INSTANCE_CONNECTION_NAME>