快速入门:部署到 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.
-
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.
- 查看 Cloud Run 价格或使用价格计算器估算费用。
-
服务的 Cloud Run Developer (
roles/run.developer) -
服务身份的 Service Account User (
roles/iam.serviceAccountUser) 角色 -
项目的 Logs Viewer (
roles/logging.viewer) 角色 在 Google Cloud 控制台中,前往 Cloud Run 页面:
从菜单中选择服务,然后点击部署容器。
在创建服务表单中,确保选中从现有容器映像部署一个修订版本选项。
在创建服务页面的容器映像网址中,点击使用示例容器进行测试。
在服务名称字段中,输入服务名称或使用默认值。
在区域列表中,使用默认值
us-central1,或选择要在其中部署容器的区域。在身份验证部分中,选择允许公开访问。
点击创建,然后等待容器部署到 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 服务页面:
在服务列表中找到要删除的服务,然后点击该服务对应的复选框以将其选中。
点击删除。这将删除服务的所有修订版本。
- SERVICE:服务的名称。
- REGION:服务的 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.
所需的角色
如需获得完成本教程所需的权限,请让您的管理员为您授予以下 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 信息中心内查看区域。
Cloud Run 会根据需求自动扩缩容器实例,您只需为处理请求期间所消耗的 CPU、内存和网络付费。
清理
为避免您的 Google Cloud 账号产生额外费用,请删除您在本快速入门中部署的所有资源。
删除仓库
当部署的服务未在使用时,Cloud Run 不会向您收取费用。不过,您可能仍需要支付将容器映像存储在 Artifact Registry 中而产生的相关费用。如需删除 Artifact Registry 仓库,请按照 Artifact Registry 文档中删除仓库的步骤操作。
删除服务
Cloud Run 服务在收到请求之前不会产生费用。如需删除 Cloud Run 服务,请按照以下步骤之一操作:
控制台
要删除服务,请执行以下操作:
gcloud
要删除作业,请运行以下命令:
gcloud run services delete SERVICE --region REGION
替换以下内容:
删除测试项目
删除 Google Cloud 项目后,系统即会停止对该项目中的所有资源计费。如需释放项目中的所有 Google Cloud 资源,请按照以下步骤操作:
后续步骤
如需了解如何通过代码源构建容器,以及如何将容器推送到 Artifact Registry 并进行部署,请参阅以下内容: