【问题标题】:GKE pods not connecting to CloudsqlGKE pod 未连接到 Cloudsql
【发布时间】:2019-08-25 17:14:55
【问题描述】:

我的应用似乎无法连接到代理,因此我的 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?

谁能告诉我应该怎么做才能连接到数据库?

【问题讨论】:

  • 在您的 kubectl exec 命令中,我没有看到您指定要连接的 pod 中的哪个容器。您确定要连接到 SQL 代理容器吗?
  • 它自己链接到它吗?或者我必须创建一个服务来链接它们? @科尔班。感谢回复
  • 也许我创建了一个代理实例,但没有连接两个容器?

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


【解决方案1】:

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

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

-instances=<INSTANCE_CONNECTION_NAME>=tcp:5432

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

-instances=<INSTANCE_CONNECTION_NAME>

【讨论】:

  • 我相信你是正确的,很好的发现/知识...这里是 cloudsql-proxy 的 GitHub 页面的链接,其中列出了示例和命令行选项github.com/GoogleCloudPlatform/cloudsql-proxy
  • 谢谢伙计,设法改善这种情况,现在当我检查代理的日志时,我得到了这个:2019/04/05 01:34:00 使用凭证文件进行身份验证; email=auth-db- 2019/04/05 01:34:00 在 127.0.0.1:5432 上收听 spheric-veric-task:asia-southeast1:authdb 2019/04/05 01:34:00 准备新连接。但是当我尝试在我的应用程序容器中使用 psql 时,我仍然收到相同的错误: psql:无法连接到服务器:没有这样的文件或目录服务器是否在本地运行并接受 Unix 域套接字上的连接“/var/run/ postgresql/.s.PGSQL.5433"?
  • @Joe.L -- 确保告诉 postgres 你希望它在哪里连接到 127.0.0.1-psql -U postgres -h 127.0.0.1
  • 我在主机上尝试了 psql,但它仍然返回相同的错误代码。不知道这里发生了什么
  • 如果是同样的错误,psql 没有跟随标志。也许您还需要指定端口? psql -p 5432 -h 127.0.0.1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 2022-01-18
  • 2017-04-18
  • 2019-07-31
相关资源
最近更新 更多