快速入门:使用 Google Cloud 控制台部署 Cloud Run 函数
本页面介绍了如何使用 Google Cloud 控制台部署 HTTP 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 Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs.
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 Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs.
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 Source Developer (
roles/run.sourceDeveloper) 角色 -
服务身份的 Service Account User (
roles/iam.serviceAccountUser) 角色 -
项目的 Logs Viewer (
roles/logging.viewer) 角色 在 Google Cloud 控制台中,前往 Cloud Run 页面:
点击编写函数。
在服务名称字段中,输入用于描述函数的服务名称,例如
my-nodejs-http-function。服务名称只能以字母开头,最多可以包含 49 个字符(包括字母、数字或连字符)。服务名称不能以连字符结尾,并且在每个区域和项目中必须唯一。服务名称一旦指定便无法更改,并且公开显示。在区域列表中,使用默认值
europe-west1,或选择要在其中部署函数的区域。在运行时列表中,使用默认值,或选择一个运行时版本。
在身份验证部分中,选择允许公开访问。如果您没有选择此选项的权限(Cloud Run Admin 角色),系��会部署服务并要求进行身份验证。
点击创建,然后等待 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.
如需使用 Google Cloud CLI 将示例函数部署到 Cloud Run,请参阅快速入门:使用 gcloud CLI 将函数部署到 Cloud Run。
如需使用 Google Cloud 控制台和 Google Cloud CLI 部署函数,请参阅部署函数。
如需查看和删除现有函数,请参阅管理服务修订版本。
如需在您自己的工具链中构建函数容器并将其部署到 Cloud Run,请参阅构建函数。
如需使用 Eventarc 创建触发器,请参阅使用 Eventarc 创建触发器。
所需的角色
如需获得完成本教程所需的权限,请让您的管理员为您授予以下 IAM 角色:
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
授予 Cloud Build 服务账号对项目的访问权限
除非您替换此行为,否则 Cloud Build 会自动使用 Compute Engine 默认服务账号作为默认 Cloud Build 服务账号来构建源代码和 Cloud Run 资源。
为了让 Cloud Build 能够构建来源,请向 Cloud Build 服务账号授予项目的 Cloud Run Builder (roles/run.builder) 角色:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:SERVICE_ACCOUNT_EMAIL_ADDRESS \ --role=roles/run.builder
将 PROJECT_ID 替换为您的 Google Cloud项目 ID,并将 SERVICE_ACCOUNT_EMAIL_ADDRESS 替换为 Cloud Build 服务账号的邮箱。如果您使用 Compute Engine 默认服务账号作为 Cloud Build 服务账号,则服务账号邮箱应采用以下格式:
PROJECT_NUMBER-compute@developer.gserviceaccount.com
将 PROJECT_NUMBER 替换为您的 Google Cloud项目编号。
如需详细了解如何查找项目 ID 和项目编号,请参阅创建和管理项目。
授予 Cloud Run Builder 角色之后,需要几分钟时间才能完成传播。
部署该函数
如需部署函数,请按以下步骤操作:
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 services delete SERVICE --region REGION
替换以下内容:
删除测试项目
删除 Google Cloud 项目后,系统即会停止对该项目中的所有资源计费。如需释放项目中的所有 Google Cloud 资源,请按照以下步骤操作: