安排运行 HTTP Cloud Run 函数


本教程介绍了如何通过在 Cloud Scheduler 中指定 HTTP 端点来安排 HTTP Cloud Run 函数的运行时间。如果您需要函数具有网址端点并响应 HTTP 请求(例如针对 webhook),可使用 HTTP 函数

或者,如果您创建的是 CloudEvents 函数,请使用 Eventarc 事件触发器来安排函数响应项目中的事件。Eventarc 可以触发 Pub/Sub 主题上的消息、Cloud Storage 存储桶中的更改等。如需了解详情,请参阅 Cloud Run 函数的类型

Cloud Scheduler 的常见用例之一是安排 Cloud Run 函数的执行。在此教程中,您将学习以下操作:

  1. 创建简单的 HTTP Cloud Run 函数。
  2. 创建 Cloud Scheduler 作业。
  3. 运行 Cloud Scheduler 作业。
  4. 验证 Cloud Scheduler 作业是否会触发 Cloud Run 函数。

费用

在本文档中,您将使用 Google Cloud的以下收费组件:

您可使用价格计算器根据您的预计使用情况来估算费用。

新 Google Cloud 用户可能有资格申请免费试用

准备工作

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.

    Enable the APIs

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.

      To grant a role, find the Select a role list, then select the role.

      To grant additional roles, click Add another role and add each additional role.

    6. Click Continue.
    7. Click Done to finish creating the service account.

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.

    Enable the APIs

  9. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.

      To grant a role, find the Select a role list, then select the role.

      To grant additional roles, click Add another role and add each additional role.

    6. Click Continue.
    7. Click Done to finish creating the service account.

  10. 请注意,您必须授予 Cloud Run Invoker 角色,因为在 Cloud Run 函数中,您可以通过管理底层 Cloud Run 服务来获取调用权限。

    所需的角色

    如需获得完成本教程所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:

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

    您也可以通过自定义角色或其他预定义角色来获取所需的权限。

    授予角色

    控制台

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

      前往 IAM
    2. 选择相应项目。
    3. 点击 授予访问权限
    4. 新的主账号字段中,输入您的用户标识符。 这通常是用于部署 Cloud Run 服务的 Google 账号邮箱。

    5. 选择角色列表中,选择一个角色。
    6. 如需授予其他角色,请点击 添加其他角色,然后依次添加所需角色。
    7. 点击保存

    gcloud

    如需向您在项目中的账号授予所需的 IAM 角色,请执行以下操作:

         gcloud projects add-iam-policy-binding PROJECT_ID \
             --member=PRINCIPAL \
             --role=ROLE
         

    您需要进行如下替换:

    • PROJECT_NUMBER 替换为您的 Google Cloud 项目编号。
    • PROJECT_ID 替换为您的 Google Cloud 项目 ID。
    • PRINCIPAL 替换为要为其添加绑定的账号。这通常是用于部署 Cloud Run 服务的 Google 账号邮箱。
    • ROLE 替换为要添加到部署者账号的角色。

创建 HTTP Cloud Run 函数

创建由 Cloud Scheduler 触发的 HTTP 函数。

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

    转到 Cloud Run

  2. 点击编写函数

  3. 配置部分中,执行以下操作:

    1. 服务名称字段中,输入函数的名称。
    2. 区域列表中,选择一个区域。
    3. 请勿修改默认的运行时语言。
    4. 对于身份验证,请选择需要进行身份验证

      如果您通过网域限制组织政策来限制项目的未经身份验证的调用,则您需要按照测试专用服务中的说明访问已部署的服务。

      如需了解详情,请参阅进行身份验证以便调用

  4. 接受其他默认值,然后点击创建

  5. 复制函数的 HTTP 网址。

创建 Cloud Scheduler 作业

创建一个使用 HTTP 触发操作的 Cloud Scheduler 作业。

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

    前往 Cloud Scheduler

  2. 点击 创建作业

  3. 输入作业的名称。

  4. 区域列表中,选择一个区域。

  5. 使用 unix-cron 格式指定作业的频率

    30 16 * * 7
    

    如需了解详情,请参阅 Cron 作业格式和时区

  6. 时区列表中,选择一个时区。

  7. 点击继续

  8. 目标类型列表中,选择 HTTP

  9. 网址字段中,输入您之前复制的函数的 HTTP 网址。

  10. 身份验证标头列表中,选择添加 OIDC 令牌

  11. 服务账号列表中,选择您之前创建的服务账号。

  12. 受众群体字段中,输入您之前复制的函数的 HTTP 网址。

  13. 点击创建

您已创建了一个 Cron 作业,该作业会在每周日的 16:30 运行您的 Cloud Run 函数。

运行 Cloud Scheduler 作业

现在,您可以运行创建的作业了。

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

    前往 Cloud Scheduler

  2. 选中您创建的作业对应的复选框,然后点击强制运行

    首次调用时,在项目中创建的第一个作业可能需要几分钟才能完成配置并运行。

  3. 作业运行后,上次执行的状态应显示为 Success

验证 Cloud Run functions 中的结果

您可以验证 Cloud Run 函数是否已成功被 cron 作业触发并执行。

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

    转到 Cloud Run

  2. 依次选择过滤服务 > 部署类型 > 函数。这会过滤服务列表,以仅显示函数。

  3. 点击函数名称。

    系统会打开服务详情页面,并显示服务指标。

  4. 点击日志标签页。

    您应该会看到类似 POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAME 的日志条目

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

删除项目

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

删除教程资源

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

    前往 Cloud Scheduler

  2. 选择作业旁边的复选框。

  3. 点击 删除并确认您要删除此存储分区。

  4. 在 Google Cloud 控制台中,前往 Cloud Run 页面。

    转到 Cloud Run

  5. 选中相应服务旁边的复选框。

  6. 点击 删除并确认您要删除此存储分区。

  7. 在 Google Cloud 控制台中,前往服务账号页面。

    转到“服务账号”

  8. 选中您创建的服务账号旁边的复选框。

  9. 点击 删除并确认您要删除此存储分区。

后续步骤