简体   繁体   English

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

[英]Run DB migrations on cloud build connecting to cloud sql using private IP

I am trying to setup db migrations for a Nodejs app on cloud build connecting to cloud sql with a private IP via cloud sql proxy.我正在尝试为云构建上的 Nodejs 应用程序设置数据库迁移,通过云 sql 代理连接到云 sql,私有 IP。 Cloud SQL connection always fail from cloud build.云 SQL 云构建连接总是失败。

Currently I am running migration manually from a compute engine.目前我正在从计算引擎手动运行迁移。

I followed this SO to setup the build steps.我按照这个 SO 来设置构建步骤。 Run node.js database migrations on Google Cloud SQL during Google Cloud Build 在 Google Cloud Build 期间在 Google Cloud SQL 上运行 node.js 数据库迁移

cloudbuild.yaml 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"

Error:错误:

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 roles:云构建角色:

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 is enabled too. CLOUD SQL ADMIN API 也已启用。

Versions:版本:

NPM libs:
  "pg": "8.0.3"
  "knex": "0.21.1"

The Cloud SQL Private IP feature uses internal IP addresses hosted in a VPC network , which are only accessible from other resources within the same VPC network. Cloud SQL 私有 IP 功能使用托管在VPC 网络中的内部 IP 地址,这些地址只能从同一 VPC 网络中的其他资源访问。

Since Cloud Build does not support VPC Networks, it is not possible to connect from Cloud Build to the private IP of a Cloud SQL instance.由于 Cloud Build 不支持 VPC 网络,因此无法从 Cloud Build 连接到 Cloud SQL 实例的私有 IP。

You might want to take a look at the official Cloud SQL documentation regarding this topic to choose another alternative that suits your use case.您可能需要查看有关此主题的官方 Cloud SQL 文档,以选择适合您用例的另一种替代方案。

Connecting to public cloud sql连接公有云 sql

I use docker-compose & cloud sql proxy .我使用 docker-compose 和云 sql代理

  1. setup docker-compose for cloud build, here . 在此处设置 docker-compose 以进行云构建。

  2. create service account (json file).创建服务帐户(json 文件)。

  3. docker-compose file: docker-compose 文件:

 version: '3.7' services: app: build: context: . dockerfile: Dockerfile restart: "no" links: - database tty: true volumes: - app:/var/www/html env_file: -./.env depends_on: - database database: image: gcr.io/cloudsql-docker/gce-proxy restart: on-failure command: - "/cloud_sql_proxy" - "-instances=<INSTANCE_CONNECTION_NAME>=tcp:0.0.0.0:3306" - "-credential_file=/config/sql_proxy.json" volumes: -./sql_proxy.json:/config/sql_proxy.json:ro volumes: app:

  1. cloudbuild.yml cloudbuild.yml

 - name: 'gcr.io/$PROJECT_ID/docker-compose' id: Compose-build-cloudProxy args: ['build'] - name: 'gcr.io/$PROJECT_ID/docker-compose' id: Compose-up-cloudProxy args: ['up', '--timeout', '1', '--no-build', '-d'] - name: 'bash' id: Warm-up-cloudProxy args: ['sleep', '5s'] - name: 'gcr.io/cloud-builders/docker' id: Artisan-Migrate args: ['exec', '-i', 'workspace_app_1', 'php', 'artisan', 'migrate'] - name: 'gcr.io/$PROJECT_ID/docker-compose' id: Compose-down-cloudProxy args: ['down', '-v']

build-success.png构建成功.png

I had the same issue as I am using AlloyDB and I was able to resolve it by setting up a worker pool under cloud build and I gave VPC access to the worker pool and the VPC has access to a serverless VPC that has access to AlloyDB so my migrations there were successful.我在使用 AlloyDB 时遇到了同样的问题,我能够通过在云构建下设置工作池来解决它,我授予 VPC 访问工作池的权限,并且 VPC 可以访问可以访问 AlloyDB 的无服务器 VPC 所以我在那里的迁移是成功的。

https://cloud.google.com/build/docs/private-pools/private-pools-overview https://cloud.google.com/build/docs/private-pools/private-pools-overview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GCP Cloud Functions 使用私有 IP 连接到云 sql - GCP Cloud Functions connecting to cloud sql with private IP 从GCE VM应用程序连接到Cloud SQL专用IP - Connecting to Cloud SQL private IP from GCE VM application 通过GCE或GKE使用私有IP连接到Cloud SQL - Connecting to Cloud SQL with Private IP from GCE or GKE Datastream 无法使用专用连接连接到具有专用 IP 的 Cloud SQL - Datastream can not connect to Cloud SQL with private IP using private connectivity 将静态 IP 云函数连接到 Cloud SQL - Connecting Static IP Cloud Functions to Cloud SQL 启用私有 IP 并关闭公共 IP 后无法从 Cloud Run 连接到 Cloud SQL - Cannot connect to Cloud SQL from Cloud Run after enabling private IP and turning off public iP 使用 Sequelize 将 Cloud Run Node 应用程序连接到 Cloud SQL - Connecting Cloud Run Node app to Cloud SQL using Sequelize 在 Google Cloud Build 期间在 Google Cloud SQL 上运行 node.js 数据库迁移 - Run node.js database migrations on Google Cloud SQL during Google Cloud Build Cloud SQL 实例未通过 Cloud Run 连接 - Cloud SQL instance not connecting via Cloud Run 从具有私有和公共 IP 的虚拟机连接到私有 IP 上的 Google Cloud SQL 实例失败 - Connecting to Google Cloud SQL instance on private IP from a VM with both private and public IPs fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM