0

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

以下是我的设置:

我的简单应用程序.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?

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

4

1 回答 1

1

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

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

-instances=<INSTANCE_CONNECTION_NAME>=tcp:5432

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

-instances=<INSTANCE_CONNECTION_NAME>
于 2019-04-04T16:07:04.237 回答