设置您的环境

在安排和运行 cron 作业之前,您必须为 Cloud Scheduler 设置 Google Cloud项目和环境。

启用 Cloud Scheduler API

按照以下说明创建或选择 Google Cloud 项目,并启用 Cloud Scheduler API。

控制台

  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. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Scheduler API.

    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 API

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

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

  7. Enable the Cloud Scheduler API.

    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 API

gcloud

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

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

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

    gcloud init
  5. 创建或选择 Google Cloud 项目

    选择或创建项目所需的角色

    • 选择项目:选择项目不需要特定的 IAM 角色,您可以选择已获授角色的任何项目。
    • 创建项目:如需创建项目,您需要拥有 Project Creator 角色 (roles/resourcemanager.projectCreator),该角色包含 resourcemanager.projects.create 权限。了解如何授予角色
    • 创建 Google Cloud 项目:

      gcloud projects create PROJECT_ID

      PROJECT_ID 替换为您要创建的 Google Cloud 项目的名称。

    • 选择您创建的 Google Cloud 项目:

      gcloud config set project PROJECT_ID

      PROJECT_ID 替换为您的 Google Cloud 项目名称。

  6. 验证是否已为您的 Google Cloud 项目启用结算功能

  7. 启用 Cloud Scheduler API:

    启用 API 所需的角色

    如需启用 API,您需要拥有 Service Usage Admin IAM 角色 (roles/serviceusage.serviceUsageAdmin),该角色包含 serviceusage.services.enable 权限。了解如何授予角色

    gcloud services enable cloudscheduler.googleapis.com
  8. 安装 Google Cloud CLI。

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

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

    gcloud init
  11. 创建或选择 Google Cloud 项目

    选择或创建项目所需的角色

    • 选择项目:选择项目不需要特定的 IAM 角色,您可以选择已获授角色的任何项目。
    • 创建项目:如需创建项目,您需要拥有 Project Creator 角色 (roles/resourcemanager.projectCreator),该角色包含 resourcemanager.projects.create 权限。了解如何授予角色
    • 创建 Google Cloud 项目:

      gcloud projects create PROJECT_ID

      PROJECT_ID 替换为您要创建的 Google Cloud 项目的名称。

    • 选择您创建的 Google Cloud 项目:

      gcloud config set project PROJECT_ID

      PROJECT_ID 替换为您的 Google Cloud 项目名称。

  12. 验证是否已为您的 Google Cloud 项目启用结算功能

  13. 启用 Cloud Scheduler API:

    启用 API 所需的角色

    如需启用 API,您需要拥有 Service Usage Admin IAM 角色 (roles/serviceusage.serviceUsageAdmin),该角色包含 serviceusage.services.enable 权限。了解如何授予角色

    gcloud services enable cloudscheduler.googleapis.com

设置身份验证

如何向 Cloud Scheduler 进行身份验证取决于您用于访问 API 的界面以及运行代码的环境。

如需了解详情,请参阅向 Cloud Scheduler 进行身份验证对 HTTP 目标使用身份验证

添加 App Engine 应用

如果您要使用 App Engine HTTP 目标,则必须向项目中添加 App Engine 应用。此应用既是 Cloud Scheduler 服务的位置,也是作业处理程序本身的位置。作业处理程序会执行与 Cloud Scheduler 作业相关联的工作,并返回确认响应。

如果您希望使用在项目中的 App Engine 作业处理程序,则应选择 HTTP 目标。在这种情况下,您无需在项目中添加 App Engine 应用。

控制台

  1. 在 Google Cloud 控制台中,前往 App Engine 页面。

    转到 App Engine

  2. Welcome to App Engine(欢迎使用 App Engine)对话框中,执行以下操作之一:

    • 如果您已创建 App Engine 应用,并且系统显示您的 App Engine 应用已创建消息,则可以跳过本部分中的其余步骤。

    • 如果您尚未创建 App Engine 应用,请点击创建应用,然后继续执行本部分中的其余步骤。

  3. 为您的应用选择一个区域

    请注意,europe-westus-central 在 Cloud Scheduler 命令中分别称为 europe-west1us-central1

    为 App Engine 应用设置区域后,您无法更改该区域

  4. 不选择服务账号;系统会使用默认的 App Engine 服务账号。

  5. 点击下一步

    应用已配置并创建。此过程可能需要几分钟的时间。

  6. 请勿下载 Cloud SDK,而是点击以后再执行此操作

    您应该会看到一条消息,告知您 App Engine 应用已创建

gcloud

  1. 如需创建 App Engine 应用,请运行 gcloud app create 命令:

    gcloud app create --region=REGION
    

    REGION 替换为应用运行的位置。请注意,europe-westus-central 在 Cloud Scheduler 命令中分别称为 europe-west1us-central1

    为 App Engine 应用设置区域后,您无法更改该区域

  2. 您可以使用以下命令检查现有应用的区域:

    gcloud app describe
    

    locationId 表示区域。例如:locationId: us-central