在使用私有IP连接到云SQL的云构建上运行数据库迁移

问题描述 投票:0回答:1

我正在尝试为通过Cloud sql代理使用私有IP连接到Cloud sql的云构建上的Nodejs应用程序设置数据库迁移。Cloud SQL连接始终因云构建而失败。

当前,我正在从计算引擎手动运行迁移。

我遵循了此SO的步骤来设置构建步骤。Run node.js database migrations on Google Cloud SQL during Google Cloud Build

cloudbuild.yaml

steps:
  - name: node:12-slim
    args: ["npm", "install"]
    env:
      - "NODE_ENV=${_NODE_ENV}"
  - 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"
  - name: node:12
    timeout: 100s
    entrypoint: sh
    args:
      - -c
      - "(/workspace/cloud_sql_proxy -dir=/workspace -instances=my-project-id:asia-south1:postgres-master=tcp:5432 & sleep 3) && npm run migrate"
    env:
      - "NODE_ENV=${_NODE_ENV}"
      - "DB_NAME=${_DB_NAME}"
      - "DB_PASS=${_DB_PASS}"
      - "DB_USER=${_DB_USER}"
      - "DB_HOST=${_DB_HOST}"
      - "DB_PORT=${_DB_PORT}"
  - name: "gcr.io/cloud-builders/gcloud"
    entrypoint: "bash"
    args:
      [
        "-c",
        "gcloud secrets versions access latest --secret=backend-api-env > credentials.yaml",
      ]
  - name: "gcr.io/cloud-builders/gcloud"
    args: ["app", "deploy", "--stop-previous-version", "-v", "$SHORT_SHA"]
timeout: "600s"

错误:

KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
Step #2:     at Client_PG.acquireConnection (/workspace/node_modules/knex/lib/client.js:349:26)

云构建角色:

Cloud Build Service Account
Cloud SQL Admin
Compute Network User
Service Account User
Secret Manager Secret Accessor
Serverless VPC Access Admin

也启用了CLOUD SQL ADMIN API。

版本:

NPM libs:
  "pg": "8.0.3"
  "knex": "0.21.1"
google-cloud-sql google-cloud-build cloud-sql-proxy private-network
1个回答
0
投票

Cloud SQL专用IP功能使用VPC network中托管的内部IP地址,只能从同一VPC网络中的其他资源进行访问。

由于Cloud Build不支持VPC网络,因此无法从Cloud Build连接到Cloud SQL实例的私有IP。

您可能希望查看有关此主题的official Cloud SQL documentation,以选择另一个适合您的用例的替代方法。

© www.soinside.com 2019 - 2024. All rights reserved.