如何检查最新的 Cloud 运行 修订版是否已准备好提供服务

How to check if the latest Cloud Run revision is ready to serve

我使用 Cloud 运行 有一段时间了,整个用户体验简直太棒了!

目前我正在使用 Cloud Build 部署容器映像,将映像推送到 GCR,然后创建一个新的 Cloud 运行 修订版。 现在我想在最新版本成功部署到云 运行 后调用脚本从 CDN 清除缓存,但是 $ gcloud run deploy 命令无法告诉您流量是否开始指向最新版本.

是否有任何我可以订阅的命令或事件来确保没有流量指向旧版本,以便我可以安全地清除所有缓存?

您可以使用 gcloud run revisions list 获取所有修订的列表:

$ gcloud run revisions list --service helloworld

   REVISION          ACTIVE  SERVICE     DEPLOYED                 DEPLOYED BY
✔  helloworld-00009  yes     helloworld  2019-08-17 02:09:01 UTC  email@email.com
✔  helloworld-00008          helloworld  2019-08-17 01:59:38 UTC  email@email.com
✔  helloworld-00007          helloworld  2019-08-13 22:58:18 UTC  email@email.com
✔  helloworld-00006          helloworld  2019-08-13 22:51:18 UTC  email@email.com
✔  helloworld-00005          helloworld  2019-08-13 22:46:14 UTC  email@email.com
✔  helloworld-00004          helloworld  2019-08-13 22:41:44 UTC  email@email.com
✔  helloworld-00003          helloworld  2019-08-13 22:39:16 UTC  email@email.com
✔  helloworld-00002          helloworld  2019-08-13 22:36:06 UTC  email@email.com
✔  helloworld-00001          helloworld  2019-08-13 22:30:03 UTC  email@email.com

您还可以使用 gcloud run revisions describe 获取有关特定修订的详细信息,其中将包含一个 status 字段。例如,一个活动修订:

$ gcloud run revisions describe helloworld-00009
...
status:
  conditions:
  - lastTransitionTime: '2019-08-17T02:09:07.871Z'
    status: 'True'
    type: Ready
  - lastTransitionTime: '2019-08-17T02:09:14.027Z'
    status: 'True'
    type: Active
  - lastTransitionTime: '2019-08-17T02:09:07.871Z'
    status: 'True'
    type: ContainerHealthy
  - lastTransitionTime: '2019-08-17T02:09:05.483Z'
    status: 'True'
    type: ResourcesAvailable

还有一个不活跃的修订版:

$ gcloud run revisions describe helloworld-00008
...
status:
  conditions:
  - lastTransitionTime: '2019-08-17T01:59:45.713Z'
    status: 'True'
    type: Ready
  - lastTransitionTime: '2019-08-17T02:39:46.975Z'
    message: Revision retired.
    reason: Retired
    status: 'False'
    type: Active
  - lastTransitionTime: '2019-08-17T01:59:45.713Z'
    status: 'True'
    type: ContainerHealthy
  - lastTransitionTime: '2019-08-17T01:59:43.142Z'
    status: 'True'
    type: ResourcesAvailable

您需要特别检查 type: Active 条件。

这一切都可以通过云 运行 REST API 获得:https://cloud.google.com/run/docs/reference/rest/v1/namespaces.revisions

默认情况下,流量会路由到最新版本。您可以在日志中看到这一点。

Deploying container to Cloud Run service [SERVICE_NAME] in project [YOUR_PROJECT] region [YOUR_REGION]
✓ Deploying... Done.                                                           
  ✓ Creating Revision...
  ✓ Routing traffic...
Done.
Service [SERVICE_NAME] revision [SERVICE_NAME-00012-yic] has been deployed and is serving 100 percent of traffic at https://SERVICE_NAME-vqg64v3fcq-uc.a.run.app

如果想确定,可以显式调用update traffic命令

gcloud run services update-traffic --platform=managed --region=YOUR_REGION --to-latest YOUR_SERVICE

@Dustin 的回答是正确的,但是 "status" 消息是 Route 配置的间接结果,因为这些内容是单独更新的(您可能会看到它们之间有几秒钟的延迟)。如果您不介意,状态消息仍会告诉您修订已停止轮换。

直接使用 API 对象回答这个具体问题(强调我的问题):

Is there any command or the event that I can subscribe to to make sure no traffic is pointing to the old revision?

您需要查看 API 上的 Route 个对象。这是一个 Knative API(它在 Cloud 运行 上可用)但它没有 gcloud 命令:https://cloud.google.com/run/docs/reference/rest/v1/namespaces.routes

例如,假设您在云 运行 服务上进行了 50%-50% 的流量拆分。执行此操作时,您会发现 Service 对象(您可以在云控制台 → 云 运行 → YAML 选项卡上看到)具有以下 spec.traffic 字段:

spec:
  traffic:
  - revisionName: hello-00002-mob
    percent: 50
  - revisionName: hello-00001-vat
    percent: 50

这是"desired configuration",但它实际上可能无法明确反映状态。更改此字段将去更新 Route 对象——它决定了流量的分配方式。

查看隐藏的 Route 对象(很遗憾,我必须在这里使用 curl,因为没有 gcloud 命令:)

TOKEN="$(gcloud auth print-access-token)"

curl -vH "Authorization: Bearer $TOKEN" \
    https://us-central1-run.googleapis.com/apis/serving.knative.dev/v1/namespaces/GCP_PROJECT/routes/SERVICE_NAME

此命令将显示输出:

  "spec": {
    "traffic": [
      {
        "revisionName": "hello-00002-mob",
        "percent": 50
      },
      {
        "revisionName": "hello-00001-vat",
        "percent": 50
      }
    ]
  },

(您可能会注意到它与服务的 spec.traffic 相同——因为它是从那里复制的)可以明确地告诉您哪些修订当前正在为该特定服务提供流量。