安排运行 HTTP Cloud Run 函数


本教程介绍了如何在 Cloud Scheduler 中以 HTTP 端点为目标来安排 HTTP Cloud Run 函数。如果您需要函数具有网址端点并响应 HTTP 请求(例如针对 webhook),可使用 HTTP 函数

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

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

  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.

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

创建 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 Console 中,前往 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. 点击 Delete(删除),然后确认删除操作。

  4. 在 Google Cloud 控制台中,转到 Cloud Run 页面。

    转到 Cloud Run

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

  6. 点击 Delete(删除),然后确认删除。

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

    转到“服务账号”

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

  9. 点击 Delete(删除),然后确认删除操作。

后续步骤