快速入门:使用 Google Cloud 控制台部署 Cloud Run 函数

本页面介绍了如何使用 Google Cloud 控制台部署 HTTP Cloud Run 函数。

准备工作

  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.

    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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

  4. Verify that billing is enabled for your Google Cloud project.

  5. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  6. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

  8. Verify that billing is enabled for your Google Cloud project.

  9. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  10. 如果您通过网域限制组织政策来限制项目的未经身份验证的调用,则您需要按照测试专用服务中的说明访问已部署的服务。

  11. 查看 Cloud Run 价格或使用价格计算器估算费用。
  12. 所需的角色

    如需获得完成本教程所需的权限,请让您的管理员为您授予以下 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 角色之后,需要几分钟时间才能完成传播

    部署该函数

    如需部署函数,请按以下步骤操作:

    1. 在 Google Cloud 控制台中,前往 Cloud Run 页面:

      转到 Cloud Run

    2. 点击编写函数

    3. 服务名称字段中,输入用于描述函数的服务名称,例如 my-nodejs-http-function。服务名称只能以字母开头,最多可以包含 49 个字符(包括字母、数字或连字符)。服务名称不能以连字符结尾,并且在每个区域和项目中必须唯一。服务名称一旦指定便无法更改,并且公开显示。

    4. 区域列表中,使用默认值 europe-west1,或选择要在其中部署函数的区域

    5. 运行时列表中,使用默认值,或选择一个运行时版本。

    6. 身份验证部分中,选择允许公开访问。如果您没有选择此选项的权限(Cloud Run Admin 角色),系统会部署服务并要求进行身份验证。

    7. 点击创建,然后等待 Cloud Run 使用占位符修订版创建服务。控制台会将您重定向到来源标签页,您可以在其中查看函数的源代码。

    8. 来源标签页中,点击保存并重新部署。(可选)点击显示载荷,以查看传入的请求对象。

    9. 部署后,容器的网址将显示在文本网址:旁边。如需查看函数的输出,请点击 复制到剪贴板以复制其网址,然后将网址粘贴到浏览器的地址栏中。

    Cloud Run 位置

    Cloud Run 是区域级的,这意味着运行 Cloud Run 服务的基础架构位于特定区域,并且由 Google 代管,以便在该区域内的所有可用区以冗余方式提供。

    选择用于运行 Cloud Run 服务的区域时,主要考虑该区域能否满足您的延迟时间、可用性或耐用性要求。通常,您可以选择距离用户最近的区域,但除此之外,您还应该考虑 Cloud Run 服务使用的其他 Google Cloud产品的位置。跨多个位置使用 Google Cloud 产品可能会影响服务的延迟时间和费用。

    Cloud Run 可在以下区域使用:

    基于层级 1 价格

    基于层级 2 价格

    • 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(拉斯维加斯)

    如果您已创建 Cloud Run 服务,可在Google Cloud 控制台的 Cloud Run 信息中心内查看区域。

    清理

    为避免您的 Google Cloud 账号产生额外费用,请删除您在本快速入门中部署的所有资源。

    删除仓库

    当部署的服务未在使用时,Cloud Run 不会向您收取费用。不过,您可能仍需要支付将容器映像存储在 Artifact Registry 中而产生的相关费用。如需删除 Artifact Registry 仓库,请按照 Artifact Registry 文档中删除仓库的步骤操作。

    删除服务

    Cloud Run 服务在收到请求之前不会产生费用。如需删除 Cloud Run 服务,请按照以下步骤之一操作:

    控制台

    要删除服务,请执行以下操作:

    1. 在 Google Cloud 控制台中,前往 Cloud Run 服务页面:

      转到 Cloud Run

    2. 在服务列表中找到要删除的服务,然后点击该服务对应的复选框以将其选中。

    3. 点击删除。这将删除服务的所有修订版本。

    gcloud

    要删除作业,请运行以下命令:

    gcloud run services delete SERVICE --region REGION

    替换以下内容:

    • SERVICE:服务的名称。
    • REGION:服务的 Google Cloud 区域。

    删除测试项目

    删除 Google Cloud 项目后,系统即会停止对该项目中的所有资源计费。如需释放项目中的所有 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.

    后续步骤