在 Cloud Run 中创建和执行作业
本页面介绍如何使用示例容器在 Cloud Run 中创建作业、执行作业以及查看该作业的日志。
准备工作
- 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.
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Run Admin API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Run Admin API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. - 查看 Cloud Run 价格或使用价格计算器估算费用。
-
作业的 Cloud Run Developer (
roles/run.developer) 角色 -
服务身份的 Service Account User (
roles/iam.serviceAccountUser) 角色 -
项目的 Logs Viewer (
roles/logging.viewer) 角色 在 Google Cloud 控制台中,前往 Cloud Run 页面。
从 Cloud Run 导航菜单中选择作业,然后点击部署容器以打开创建作业表单。
点击使用示例容器进行测试。
在区域下拉菜单中,选择要在其中运行作业的区域。
点击创建以创建作业。
在作业页面中,点击您刚刚创建的作业以打开作业详情页面。
点击执行。
作业的日志会在作业开始执行后立即可用。点击可观测性标签页,然后点击日志即可查看。
asia-east1(台湾)asia-northeast1(东京)asia-northeast2(大阪)asia-south1(印度孟买)europe-north1(芬兰)二氧化碳排放量低
europe-north2(斯德哥尔摩)二氧化碳排放量低
europe-southwest1(马德里)二氧化碳排放量低
europe-west1(比利时)二氧化碳排放量低
europe-west4(荷兰)二氧化碳排放量低
europe-west8(米兰)europe-west9(巴黎)二氧化碳排放量低
me-west1(特拉维夫)northamerica-south1(墨西哥)us-central1(爱荷华)二氧化碳排放量低
us-east1(南卡罗来纳)us-east4(北弗吉尼亚)us-east5(哥伦布)us-south1(达拉斯)二氧化碳排放量低
us-west1(俄勒冈)二氧化碳排放量低
africa-south1(约翰内斯堡)asia-east2(香港)asia-northeast3(韩国首尔)asia-southeast1(新加坡)asia-southeast2(雅加达)asia-south2(印度德里)australia-southeast1(悉尼)australia-southeast2(墨尔本)europe-central2(波兰,华沙)europe-west10(柏林)europe-west12(都灵)europe-west2(英国伦敦)二氧化碳排放量低
europe-west3(德国法兰克福)europe-west6(瑞士苏黎世)二氧化碳排放量低
me-central1(多哈)me-central2(达曼)northamerica-northeast1(蒙特利尔)二氧化碳排放量低
northamerica-northeast2(多伦多)二氧化碳排放量低
southamerica-east1(巴西圣保罗)二氧化碳排放量低
southamerica-west1(智利圣地亚哥)二氧化碳排放量低
us-west2(洛杉矶)us-west3(盐湖城)us-west4(拉斯维加斯)在 Google Cloud 控制台中,前往 Cloud Run:
在作业列表中找到要删除的作业,然后点击该作业对应的复选框以将其选中。
点击删除。这会终止所有正在进行的作业执行和所有正在运行的容器实例。
- 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.
所需的角色
如需获得完成本教程所需的权限,请让您的管理员为您授予以下 IAM 角色:
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
创建作业
如需创建作业,请执行以下操作:
该作业现已创建好并可供执行。
执行作业
创建作业后,您便可以执行该作业:
Cloud Run 位置
Cloud Run 是区域级的,这意味着运行 Cloud Run 服务的基础架构位于特定区域,并且由 Google 代管,以便在该区域内的所有可用区以冗余方式提供。
选择用于运行 Cloud Run 服务的区域时,主要考虑该区域能否满足您的延迟时间、可用性或耐用性要求。通常,您可以选择距离用户最近的区域,但除此之外,您还应该考虑 Cloud Run 服务使用的其他 Google Cloud产品的位置。跨多个位置使用 Google Cloud 产品可能会影响服务的延迟时间和费用。
Cloud Run 可在以下区域使用:
基于层级 1 价格
基于层级 2 价格
如果您已创建 Cloud Run 服务,可在Google Cloud 控制台的 Cloud Run 信息中心内查看区域。
清理
为避免您的 Google Cloud 账号产生额外费用,请删除您在本快速入门中部署的所有资源。
删除仓库
Cloud Run 仅针对作业的执行时间收费。不过,您可能仍需要支付将容器映像存储在 Artifact Registry 中而产生的相关费用。如需删除 Artifact Registry 仓库,请按照 Artifact Registry 文档中删除仓库的步骤操作。
删除作业
Cloud Run 作业仅在作业任务执行时产生费用。如需删除 Cloud Run 作业,请按照以下步骤之一操作:
控制台
要删除任务,请执行以下操作:
gcloud
要删除作业,请运行以下命令:
gcloud run jobs delete JOB_NAME
将 JOB_NAME 替换为作业的名称。
删除测试项目
删除 Google Cloud 项目后,系统即会停止对该项目中的所有资源计费。如需释放项目中的所有 Google Cloud 资源,请按照以下步骤操作: