【问题标题】:How to check if the latest Cloud Run revision is ready to serve如何检查最新的 Cloud Run 修订版是否已准备好投放
【发布时间】:2020-05-07 12:28:41
【问题描述】:

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

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

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

【问题讨论】:

    标签: google-cloud-platform cdn google-cloud-pubsub google-cloud-build google-cloud-run


    【解决方案1】:

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

    直接使用 API 对象回答这个特定问题(重点是我的):

    是否有我可以订阅的命令或事件确保没有流量指向旧版本

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

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

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

    这是“所需配置”,但实际上可能无法明确反映状态。更改此字段将更新 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
          }
        ]
      },
    

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

    【讨论】:

    • 谢谢艾哈迈德! > 这是“所需配置”,但它实际上可能无法明确反映状态 是的,这正是我在运行$ gcloud run services describe 命令时担心的问题。 (就像我为我的 k8s 服务所做的那样)检查 Route 对象可能是解决这个问题的完美解决方案 :)
    • ?? 如果它解决了您的问题,请不要忘记将问题标记为完成。
    【解决方案2】:

    您可以使用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 条件。

    这也可以通过 Cloud Run REST API 获得:https://cloud.google.com/run/docs/reference/rest/v1/namespaces.revisions

    【讨论】:

      【解决方案3】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 2023-02-09
        • 2014-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-20
        • 1970-01-01
        • 2018-03-12
        相关资源
        最近更新 更多