本教程介绍了如何通过在 Cloud Scheduler 中指定 HTTP 端点来安排 HTTP Cloud Run 函数的运行时间。如果您需要函数具有网址端点并响应 HTTP 请求(例如针对 webhook),可使用 HTTP 函数。
或者,如果您创建的是 CloudEvents 函数,请使用 Eventarc 事件触发器来安排函数响应项目中的事件。Eventarc 可以触发 Pub/Sub 主题上的消息、Cloud Storage 存储桶中的更改等。如需了解详情,请参阅 Cloud Run 函数的类型。
Cloud Scheduler 的常见用例之一是安排 Cloud Run 函数的执行。在此教程中,您将学习以下操作:
费用
在本文档中,您将使用 Google Cloud的以下收费组件:
您可使用价格计算器根据您的预计使用情况来估算费用。
准备工作
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
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. - Click Continue.
-
Click Done to finish creating the service account.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
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. - Click Continue.
-
Click Done to finish creating the service account.
-
-
Cloud Run Source Developer (
roles/run.sourceDeveloper
) -
Cloud Scheduler Admin (
roles/cloudscheduler.admin
) -
Logs View Accessor (
roles/logging.viewAccessor
) -
Project IAM Admin (
roles/resourcemanager.projectIamAdmin
) -
Service Account Admin (
roles/iam.serviceAccountAdmin
) -
Service Account User (
roles/iam.serviceAccountUser
) -
在 Google Cloud 控制台中,前往 IAM 页面。
前往 IAM - 选择相应项目。
- 点击 授予访问权限。
-
在新的主账号字段中,输入您的用户标识符。 这通常是用于部署 Cloud Run 服务的 Google 账号邮箱。
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后依次添加所需角色。
- 点击保存。
- 将 PROJECT_NUMBER 替换为您的 Google Cloud 项目编号。
- 将 PROJECT_ID 替换为您的 Google Cloud 项目 ID。
- 将 PRINCIPAL 替换为要为其添加绑定的账号。这通常是用于部署 Cloud Run 服务的 Google 账号邮箱。
- 将 ROLE 替换为要添加到部署者账号的角色。
请注意,您必须授予 Cloud Run Invoker 角色,因为在 Cloud Run 函数中,您可以通过管理底层 Cloud Run 服务来获取调用权限。
所需的角色
如需获得完成本教程所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
授予角色
控制台
gcloud
如需向您在项目中的账号授予所需的 IAM 角色,请执行以下操作:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=PRINCIPAL \ --role=ROLE
您需要进行如下替换:
创建 HTTP Cloud Run 函数
创建由 Cloud Scheduler 触发的 HTTP 函数。
在 Google Cloud 控制台中,前往 Cloud Run 页面。
点击编写函数。
在配置部分中,执行以下操作:
- 在服务名称字段中,输入函数的名称。
- 在区域列表中,选择一个区域。
- 请勿修改默认的运行时语言。
对于身份验证,请选择需要进行身份验证。
如果您通过网域限制组织政策来限制项目的未经身份验证的调用,则您需要按照测试专用服务中的说明访问已部署的服务。
如需了解详情,请参阅进行身份验证以便调用。
接受其他默认值,然后点击创建。
复制函数的 HTTP 网址。
创建 Cloud Scheduler 作业
创建一个使用 HTTP 触发操作的 Cloud Scheduler 作业。
在 Google Cloud 控制台中,前往 Cloud Scheduler 页面。
点击
创建作业。输入作业的名称。
在区域列表中,选择一个区域。
使用 unix-cron 格式指定作业的频率:
30 16 * * 7
如需了解详情,请参阅 Cron 作业格式和时区。
在时区列表中,选择一个时区。
点击继续。
在目标类型列表中,选择 HTTP。
在网址字段中,输入您之前复制的函数的 HTTP 网址。
在身份验证标头列表中,选择添加 OIDC 令牌。
在服务账号列表中,选择您之前创建的服务账号。
在受众群体字段中,输入您之前复制的函数的 HTTP 网址。
点击创建。
您已创建了一个 Cron 作业,该作业会在每周日的 16:30 运行您的 Cloud Run 函数。
运行 Cloud Scheduler 作业
现在,您可以运行创建的作业了。
在 Google Cloud 控制台中,前往 Cloud Scheduler 页面。
选中您创建的作业对应的复选框,然后点击强制运行。
首次调用时,在项目中创建的第一个作业可能需要几分钟才能完成配置并运行。
作业运行后,上次执行的状态应显示为
Success
。
验证 Cloud Run functions 中的结果
您可以验证 Cloud Run 函数是否已成功被 cron 作业触发并执行。
在 Google Cloud 控制台中,前往 Cloud Run 页面。
依次选择过滤服务 > 部署类型 > 函数。这会过滤服务列表,以仅显示函数。
点击函数名称。
系统会打开服务详情页面,并显示服务指标。
点击日志标签页。
您应该会看到类似
POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAME
的日志条目
清理
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
删除教程资源
在 Google Cloud 控制台中,前往 Cloud Scheduler 页面。
选择作业旁边的复选框。
点击
删除并确认您要删除此存储分区。在 Google Cloud 控制台中,前往 Cloud Run 页面。
选中相应服务旁边的复选框。
点击
删除并确认您要删除此存储分区。在 Google Cloud 控制台中,前往服务账号页面。
选中您创建的服务账号旁边的复选框。
点击
删除并确认您要删除此存储分区。
后续步骤
- 使用 Cloud Scheduler 按计划触发 Cloud Run
- 编写 CloudEvents 函数,以安排函数响应项目中的事件。