使用指标分析应用

本快速入门介绍如何使用 Cloud Deploy 根据 Google Cloud Observability 中的指标分析已部署的 Cloud Run 服务,以确保应用按预期运行。

在本快速入门中,您将执行以下操作:

  1. 创建并部署一个 Cloud Run 服务。

    在这种情况下,您不会使用 Cloud Deploy 来部署它。

  2. 在 Google Cloud Observability 中创建拨测

    此检查会监控您的 Cloud Run 服务,确保其正常运行。

  3. 在 Cloud Monitoring 中创建提醒政策。

    默认情况下,Cloud Deploy 分析可以使用 Google Cloud Observability 中的多种类型的指标。如果正常运行时间检查表明存在问题,此政策会创建提醒。

  4. 创建 Skaffold 配置以标识 Cloud Run 服务。

  5. 定义 Cloud Deploy 交付流水线和目标。

    此流水线仅包含一个阶段,仅使用一个目标,并包含分析作业的定义。

  6. 创建会自动部署到目标的版本。

    应用部署完毕后,分析会作为发布中的作业运行。

    服务应成功部署到目标,但由于提醒政策会生成提醒,因此发布应失败。

  7. 更改 Cloud Run 服务定义以增加服务的实例数量,并创建新版本。

    这次,服务将成功部署,并且发布将成功完成。

准备工作

  • 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  • In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  • If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  • Verify that billing is enabled for your Google Cloud project.

  • Enable the Cloud Deploy, Cloud Build, Cloud Run, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  • 安装 Google Cloud CLI。

  • 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  • 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  • In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  • If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  • Verify that billing is enabled for your Google Cloud project.

  • Enable the Cloud Deploy, Cloud Build, Cloud Run, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  • 安装 Google Cloud CLI。

  • 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  • 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  • 所需的角色

    如需确保 Cloud Deploy 服务账号具有运行 Cloud Deploy 操作和部署到 Cloud Run 所需的权限,请让您的管理员向 Cloud Deploy 服务账号授予项目的以下 IAM 角色:

    如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

    您的管理员还可以通过自定义角色或其他预定义角色向 Cloud Deploy 服务账号授予所需的权限。

    详细了解 Cloud Deploy 服务账号。

    部署 Cloud Run 服务

    本快速入门使用的提醒政策要求 Cloud Run 已存在。因此,我们在此处部署一个,并在后面的部分中使用相同的服务名称定义一个 service.yaml

    运行以下命令以创建初始服务:

    gcloud run deploy my-analysis-run-service \
      --image=us-docker.pkg.dev/cloudrun/container/hello@sha256:95ade4b17adcd07623b0a0c68359e344fe54e65d0cb01b989e24c39f2fcd296a \
      --project=PROJECT_ID \
      --region=us-central1 \
      --allow-unauthenticated
    

    PROJECT_ID 替换为您的项目 ID。

    创建 Google Cloud Observability 拨测

    此正常运行时间检查会监控正在运行的服务,以确认该服务是否在运行。 在后面的部分中,您将创建一个 Google Cloud Observability 提醒政策,该政策会在您的服务没有至少一个正在运行的可用实例时生成提醒。

    1. 如需创建拨测,请运行以下命令:

      gcloud monitoring uptime create my-analysis-run-service-cloud-run-uptime-check \
        --resource-type=cloud-run-revision \
        --resource-labels="project_id=PROJECT_ID,location=us-central1,service_name=my-analysis-run-service" \
        --project=PROJECT_ID \
        --protocol=https \
        --path="/" \
        --port=443 \
        --period=1 \
        --timeout=10 \
        --service-agent-auth="oidc-token" \
        --status-classes="2xx"
      
    2. 复制正常运行时间检查 ID。

      您刚刚运行的命令的输出包含该 ID。

    准备 Skaffold 配置和服务定义

    在本快速入门中,您将创建一个 skaffold.yaml 文件,以标识要用于部署示例 Cloud Run 服务的清单,同时您还将定义 service.yaml 文件,该文件用于定义 Cloud Run 本身。

    1. 打开一个终端窗口。

    2. 创建一个新目录,并导航至该目录。

    mkdir deploy-analysis-run-quickstart
    cd deploy-analysis-run-quickstart
    
    1. 创建名为 skaffold.yaml 且包含以下内容的文件:
    apiVersion: skaffold/v4beta7
    kind: Config
    manifests:
      rawYaml:
      - service.yaml
    deploy:
      cloudrun: {}
    

    如需详细了解此配置文件,请参阅 skaffold.yaml 参考文档

    1. 创建一个名为 service.yaml 的文件,其中包含以下内容:
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: my-analysis-run-service
      annotations:
        run.googleapis.com/scalingMode: manual
        run.googleapis.com/manualInstanceCount: 0
    spec:
      template:
        spec:
          containers:
          - image: my-app-image
    

    此文件是 Cloud Run 服务定义,用于部署应用。此处将要部署的容器映像设置为占位符 my-app-image,当您创建发布版本时,该占位符会被替换为具体映像。

    请注意,我们将伸缩模式设置为手动,并将实例数设置为 0。这意味着,已部署的服务将无法接收数据流量,并且不会有任何正在运行的实例,这会触发提醒。接下来,您将配置该提醒。

    创建提醒政策

    本快速入门使用 Google Cloud Observability 提醒政策。当 Cloud Run 服务未能通过您之前创建的正常运行时间检查时,此提醒政策会生成提醒。

    1. deploy-analysis-run-quickstart 目录中,创建一个名为 policy.yaml 的文件,其中包含以下内容:

      displayName: Cloud Run service uptime check
      userLabels:
        policy-for: analysis-run-pipeline
      combiner: OR
      conditions:
      - displayName: Failure of uptime check UPTIME_ID
        conditionThreshold:
          filter: metric.type="monitoring.googleapis.com/uptime_check/check_passed" AND metric.label.check_id="UPTIME_ID" AND resource.type="cloud_run_revision"
          aggregations:
          - alignmentPeriod: 60s
            crossSeriesReducer: REDUCE_COUNT_FALSE
            groupByFields:
            - resource.label.*
            perSeriesAligner: ALIGN_NEXT_OLDER
          comparison: COMPARISON_GT
          duration: 60s
          thresholdValue: 1.0
      

      UPTIME_ID 替换为您之前创建的正常运行时间检查的 ID。

    2. 运行以下命令以创建政策:

      gcloud monitoring policies create \
        --policy-from-file=policy.yaml\
        --project=PROJECT_ID
      

      PROJECT_ID 替换为您的项目 ID。

    3. 从您刚刚运行的命令的输出中复制政策 ID。

      这次,请复制整个资源 ID,包括路径。您将在下一部分中,在交付流水线的分析配置中使用此 ID。

    创建交付流水线和目标

    此交付流水线有一个阶段,适用于一个目标:analysis-staging

    1. deploy-analysis-run-quickstart 目录中,创建包含以下内容的新文件 clouddeploy.yaml

      apiVersion: deploy.cloud.google.com/v1
      kind: DeliveryPipeline
      metadata:
        name: deploy-analysis-demo-app-run
      description: main application pipeline
      serialPipeline:
        stages:
        - targetId: analysis-staging
          profiles: []
          strategy:
            standard:
              analysis:
                duration: 300s
                googleCloud:
                  alertPolicyChecks:
                  - id: check-1
                    alertPolicies:
                    - ALERT_POLICY_ID
      ---
      
      apiVersion: deploy.cloud.google.com/v1
      kind: Target
      metadata:
        name: analysis-staging
      description: staging Run service
      run:
        location: projects/PROJECT_ID/locations/us-central1
      

      在此 YAML 中,将 ALERT_POLICY_ID 替换为您之前运行的 gcloud monitoring policies list 命令的输出,并将 PROJECT_ID 替换为您正在使用的项目的 ID。

    2. 在 Cloud Deploy 服务中注册流水线和目标:

      gcloud deploy apply --file=clouddeploy.yaml --region=us-central1 --project=PROJECT_ID
      

      现在,您已经有了包含一个目标的交付流水线,可以部署应用了。

    3. 确认您的流水线和目标:

      在 Google Cloud 控制台中,导航到 Cloud Deploy 交付流水线页面,以查看可用交付流水线的列表。

      打开“交付流水线”页面

      此时将显示您刚刚创建的交付流水线,并且目标列会列出了一个目标。

       Google Cloud 控制台中的交付流水线页面,其中显示了您的流水线

    创建版本

    版本是表示要部署的更改的中央 Cloud Deploy 资源。交付流水线定义该版本的生命周期。请参阅 Cloud Deploy 服务架构,详细了解该生命周期。

    deploy-analysis-run-quickstart 目录运行以下命令,以创建表示要部署的容器映像的 release 资源:

    gcloud deploy releases create test-release-001 \
      --project=PROJECT_ID \
      --region=us-central1 \
      --delivery-pipeline=deploy-analysis-demo-app-run \
      --images=my-app-image=us-docker.pkg.dev/cloudrun/container/hello@sha256:95ade4b17adcd07623b0a0c68359e344fe54e65d0cb01b989e24c39f2fcd296a
    

    请注意 --images= 标志,您可以使用该标志将服务定义中的占位符 (my-app-image) 替换为特定的、经过 SHA 限定的映像。Google 建议您以这种方式将服务定义模板化,并在创建发布版本时使用 SHA 限定的映像名称。

    与所有版本一样(除非它们包含 --disable-initial-rollout),Cloud Deploy 还会自动创建部署资源。应用会自动部署到为此交付流水线配置的唯一目标。

    在 Google Cloud 控制台中查看结果

    几分钟后,您的发布版本就会部署到目标运行时。您可以查看此信息。

    1. 在 Google Cloud 控制台中,前往 Cloud Deploy 交付流水线页面,以查看您的交付流水线(“deploy-analysis-demo-app-run”)。

      打开“交付流水线”页面

    2. 点击交付流水线的名称(“deploy-analysis-demo-app-run”)。

      流水线可视化显示应用的部署状态。由于流水线中只有一个阶段,因此可视化图表仅显示一个节点。

      显示成功的交付流水线直观呈现

      您的版本列在版本标签页中交付流水线详细信息下。

    3. 交付流水线详情下,点击发布标签页。

    4. 点击发布名称即可查看发布详情。

      分析会列为作业。

       Google Cloud 控制台中的发布

    分析失败

    在应用部署后,我们在交付流水线配置中指定的分析作业会作为此发布的一部分运行。此分析旨在失败,因为提醒政策应触发提醒。由于此分析失败,发布将失败。

    我们会进行更改,然后创建另一个版本,该版本应该会成功。

    更改服务定义并创建新版本

    现在,我们更改服务定义中的实例数量,以便服务有一个正在运行的实例,这样就不会触发提醒。

    1. 修改 Cloud Run 定义文件 service.yaml,将 run.googleapis.com/manualInstanceCount 的值从 0 更改为 1

    2. 从同一 deploy-analysis-run-quickstart 目录运行以下命令,以创建另一个 release

      gcloud deploy releases create test-release-002 \
        --project=PROJECT_ID \
        --region=us-central1 \
        --delivery-pipeline=deploy-analysis-demo-app-run \
        --images=my-app-image=us-docker.pkg.dev/cloudrun/container/hello@sha256:95ade4b17adcd07623b0a0c68359e344fe54e65d0cb01b989e24c39f2fcd296a
      

    这次,发布应该会成功。这是因为您的 Cloud Run 服务现在有一个正在运行的实例,因此正常运行时间检查不会触发提醒政策中的提醒。

    清理

    为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

    1. 删除 Cloud Run 服务:

      gcloud run services delete my-analysis-run-service \
      --region=us-central1 \
      --project=PROJECT_ID
      
    2. 如需删除交付流水线、目标、版本和发布,请从包含交付流水线定义的目录中运行以下命令:

      gcloud deploy delete --file=clouddeploy.yaml \
      --force \
      --region=us-central1 \
      --project=PROJECT_ID
      
    3. 删除 Google Cloud Observability 提醒政策:

      gcloud monitoring policies delete  ALERT_POLICY_ID
      
    4. 删除 Cloud Deploy 创建的 Cloud Storage 存储分区。

      一个以 _clouddeploy 结尾,另一个是 [region].deploy-artifacts.[project].appspot.com

      打开“Cloud Storage 浏览器”页面

    恭喜,您已经完成快速入门!

    后续步骤