使用指标分析应用

本快速入门介绍如何使用 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 服务定义以增加服务的实例数,并创建新版本。

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

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud的新用户, 请创建一个账号,以评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
  2. 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

  3. 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.

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

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

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  6. 安装 Google Cloud CLI。

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

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

    gcloud init
  9. 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

  10. 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.

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

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

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  13. 安装 Google Cloud CLI。

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

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

    gcloud init

所需的角色

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

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

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

详细了解 Cloud Deploy 服务帐号。

部署 Cloud Run 服务

本快速入门使用需要 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 服务的清单,并且您还将定义用于定义 Cloud Run 本身的 service.yaml 文件。

  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:
    -