如何使用 cloudbuild.yaml 中的 gcloud run delete 从所有区域删除服务

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

我在多个区域的云运行上部署我的服务。

我的

cloudbuild.yaml
这样做:

  1. 树立新形象
  2. 将新镜像推送到 ArtifactRegistry
  3. TODO:停止所有地区的服务
  4. 运行数据库迁移
  5. 在 europe-west9 部署服务
  6. 在 europe-west8 部署服务

在步骤 3 中,我想确保我的服务在当前部署的所有区域中停止,因为我将在数据库上执行迁移。

删除服务的命令是

gcloud run services delete <service-name> --platform=managed --region=europe-west9
。 (文档
在我的 cloudbuild.yaml 中,它看起来像这样:

  - id: "Stop all services"
    name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: gcloud
    args: [
      'run', 'services', 'delete', "${_SERVICE_NAME}", '--platform=managed', "--region=europe-west9",
    ]

我需要一种方法来删除所有区域的服务。

我想做:

  1. 使用
    gcloud run regions list
    文档)获取所有区域。
  2. 迭代结果并在每个区域运行
    gcloud run services delete .....--region=...

但是我有两个问题:

  1. gcloud run regions list
    将返回 Cloud Run 中可用的所有区域。所以我需要过滤我的
    ${_SERVICE_NAME}
    ,但我不明白如何使用
    --filter
    来实现此目的。
  2. 我不知道如何在我的cloudbuild.yaml中“
    迭代
    ”。

完整(简化)cloudbuild.yaml:

steps:
  # - Build new image
  - id: "Build"
    name: "gcr.io/cloud-builders/docker"
    entrypoint: 'bash'
    args: [
      '-c',
      "docker build -t ${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA ."
    ]

  # - Push new image to artifact registry
  - id: "Push"
    name: "gcr.io/cloud-builders/docker"
    args: ["push", "${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA"]

  # - Stop all existing Cloud Run services (Paris, Milan, annd maybe more)
  - id: "Stop all services"
    name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: gcloud
    args: [
      'run', 'services', 'delete', "${_SERVICE_NAME}", '--platform=managed', "--region=europe-west9",
    ]
  
  # - Apply database migrations
  - id: "apply migrations"
    name: "gcr.io/google-appengine/exec-wrapper"
    entrypoint: "bash"
    args:
      [
        "-c",
        "/buildstep/execute.sh -i ${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA -s ${_CLOUD_SQL_CONNECTION_NAME} -- bundle exec rails db:migrate"
      ]


  # - Launch paris instance
  - id: "Run Paris"
    name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: gcloud
    args: [
      'run', 'deploy', "${_SERVICE_NAME}",
      '--image', '${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA',
      '--region', 'europe-west9',
    ]

  # - Launch Milan instance
  - id: "Run Milan"
    name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: gcloud
    args: [
      'run', 'deploy', "${_SERVICE_NAME}",
      '--image', '${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA',
      '--region', 'europe-west8',
    ]

options:
  logging: CLOUD_LOGGING_ONLY

images:
  - "${_ARTIFACT_REGISTRY_REPOSITORY}/${_SERVICE_NAME}:$SHORT_SHA"
google-cloud-platform gcloud google-cloud-run google-cloud-build cloudbuild.yaml
1个回答
0
投票

根据 @DazWilkin 和 @guillaumeblaquiere 的评论发布为社区 wiki:

可能有更好的方法,但您可以使用

cloud.googleapis.com/location
.metadata.name
 枚举服务的 
using gcloud run services list --project=${PROJECT} --filter="metadata.name=${_SERVICE_NAME}" --format="value(metadata.labels['cloud.googleapis.com/location'])"

标签

然后,您可以简单地在

$(gcloud run services list ...); do gcloud run services delete ... --region=${REGION} ...; done

中输入REGION

遗憾的是,删除服务不会立即停止 Cloud Run 实例。我建议您等待 Cloud Run 服务超时(默认为 3 分钟),以确保所有当前连接和进程都已停止。如果您使用 CPU Always ON 功能,请再等待 15 分钟(除了超时之外)。在迁移之前,数据库快照也是一个好主意,以防需要回滚。

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