我在多个区域的云运行上部署我的服务。
我的
cloudbuild.yaml
这样做:
在步骤 3 中,我想确保我的服务在当前部署的所有区域中停止,因为我将在数据库上执行迁移。
删除服务的命令是
gcloud run services delete <service-name> --platform=managed --region=europe-west9
。 (文档) - 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",
]
我需要一种方法来删除所有区域的服务。
我想做:
gcloud run regions list
(文档)获取所有区域。gcloud run services delete .....--region=...
。但是我有两个问题:
gcloud run regions list
将返回 Cloud Run 中可用的所有区域。所以我需要过滤我的 ${_SERVICE_NAME}
,但我不明白如何使用 --filter
来实现此目的。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"
根据 @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'])"
标签然后,您可以简单地在
中输入REGION$(gcloud run services list ...); do gcloud run services delete ... --region=${REGION} ...; done
遗憾的是,删除服务不会立即停止 Cloud Run 实例。我建议您等待 Cloud Run 服务超时(默认为 3 分钟),以确保所有当前连接和进程都已停止。如果您使用 CPU Always ON 功能,请再等待 15 分钟(除了超时之外)。在迁移之前,数据库快照也是一个好主意,以防需要回滚。