【问题标题】:Issue connecting to Database during Google Cloud Build process在 Google Cloud Build 过程中连接到数据库的问题
【发布时间】:2020-02-18 22:54:17
【问题描述】:

类似于this issue.(我将在下面更详细地描述链接)

问题: 运行我的 Google Cloud Build 时,我收到一条错误消息:

django.db.utils.OperationalError: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/cloudsql/sample-kubernetes-268320:us-west1:cloud-build-staging/.s.PGSQL.3306"

我正在关注合作者posted here 的解决方案。他们提供了一个示例 cloudbuild.yaml,我没有任何运气就密切关注它。

工作 cloudbuild.yaml

steps:
  - id: proxy-install
    name: alpine:3.10
    entrypoint: sh
    args:
      - -c
      - 'wget -O /workspace/cloud_sql_proxy https://storage.googleapis.com/cloudsql-proxy/v1.16/cloud_sql_proxy.linux.386 &&  chmod +x /workspace/cloud_sql_proxy'
    waitFor: ['-']
  - id: execute-with-proxy
    name: python:3.7
    timeout: 100s
    entrypoint: sh
    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=[INSTANCE_CONNECTION_NAME] & sleep 2) && (pip install -r requirements.txt && python test_sql.py)'
    waitFor: ['proxy-install']

我的 cloudbuild.yaml

steps:
  - id: proxy-install
    name: alpine:3.10
    entrypoint: sh
    args:
      - -c
      - 'wget -O /workspace/cloud_sql_proxy https://storage.googleapis.com/cloudsql-proxy/v1.16/cloud_sql_proxy.linux.386 &&  chmod +x /workspace/cloud_sql_proxy'
    waitFor: ['-']
  - id: Test
    name: 'python:3.7.6-buster'
    env:
      - "CLOUDBUILD_TEST=True"
    timeout: 100s
    entrypoint: /bin/sh
    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'
    waitFor: ['proxy-install']

我已采取的调试步骤:

  1. 我已将 Cloud Build Service 添加为 Cloud SQL Admin
  2. 我已通过使用gcloud sqlinstances describe cloud-build-staging 并复制“连接名称”来确保我的实例名称是正确的

编辑: 我从

更改了我的 cloudbuild.yaml 文件
    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

到:

    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/cloudsql -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

没有效果。

【问题讨论】:

  • 看来您正在将套接字安装在/workspace 上,我认为应该是/cloudsql?例如:` - '(/workspace/cloud_sql_proxy -dir=/cloudsql -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fasterfollowup && python3 manage.py test)' `
  • @JerpDertin 我改变了它没有任何效果。我上面的编辑反映了这一点。但是,我认为我做错了什么,因为你和 Kurtis 都得出了同样的结论。
  • 感谢您提供该 github 链接

标签: google-app-engine google-cloud-storage google-cloud-build


【解决方案1】:

看起来您使用 -dir=/workspace 启动了代理,但随后尝试使用 /cloudsql 连接。您需要更新代理或更新您的应用程序用于连接的路径。

行:

args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

应阅读:

args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/cloudsql -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

其他错误包括:

  1. 在错误的端口监听。在您的日志中,您将看到数据库侦听的端口。 postgres 的默认值是 5432,你的是 3306。

  2. 验证您的 Cloud Build 服务是否具有正确的权限。通过 IAM 仪表板将 [projectnumber]@cloudbuild.gserviceaccount.com 添加为“Cloud SQL 管理员”。

【讨论】:

  • 我将 args 中的路径从 -dir=/workspace 更新为 -dir=/cloudsql 无效。这是你要我做的吗?请查看我的更新以了解更多背景信息。
  • 接下来我要验证代理是否已正确启动 - 您应该会看到日志表明它已启动,或者是否由于某种原因失败。
  • 看起来我在测试阶段(在拉出 python 3.7.6 buster 之后)遇到错误,说明如下。你知道我需要启用什么 API 吗? Status: Downloaded newer image for python:3.7.6-buster docker.io/library/python:3.7.6-buster 2020/02/19 00:23:23 current FDs rlimit set to 1048576, wanted limit is 8500. Nothing to do here. 2020/02/19 00:23:24 errors parsing config: googleapi: Error 403: The client is not authorized to make this request., notAuthorized
  • 您需要将“Cloud SQL 客户端”IAM 角色应用到代理使用的任何服务帐户(可能是 Cloud Build 默认值?)
猜你喜欢
  • 2020-04-23
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
  • 2021-12-25
  • 2018-01-11
  • 2018-02-16
相关资源
最近更新 更多