快速入门:部署到 Cloud Run

本页面介绍了如何使用 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.

    Go to project selector

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

  4. Make sure that you have the following role or roles on the project: Cloud Run Admin, Cloud Run Developer, Logs Viewer

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往 IAM
    2. 选择项目。
    3. 点击 授予访问权限
    4. 新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。

    5. 选择角色列表中,选择一个角色。
    6. 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
    7. 点击 Save(保存)。
  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  7. Make sure that you have the following role or roles on the project: Cloud Run Admin, Cloud Run Developer, Logs Viewer

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往 IAM
    2. 选择项目。
    3. 点击 授予访问权限
    4. 新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。

    5. 选择角色列表中,选择一个角色。
    6. 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
    7. 点击 Save(保存)。
  8. 确保您已向服务身份授予 Service Account User 角色。默认情况下,服务身份是 Compute Engine 默认服务账号。

    授予角色

    如需授予服务身份资源的访问权限,请使用 gcloud iam service-accounts add-iam-policy-binding 命令,并将突出显示的变量替换为适当的值:

          gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_EMAIL \
              --member="PRINCIPAL" \
              --role="roles/iam.serviceAccountUser"
          

    替换以下内容:

    • SERVICE_ACCOUNT_EMAIL:您用作服务身份的服务账号邮箱,例如:
      • Compute Engine 默认服务账号:PROJECT_NUMBER-compute@developer.gserviceaccount.com
      • 您创建的服务账号:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
    • PRINCIPAL:用户标识符。这通常是 Google 账号的邮箱。
  9. 部署示例容器

    如需部署容器,请按以下步骤操作:

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

      转到 Cloud Run

    2. 从菜单中选择服务,然后点击部署容器

    3. 创建服务表单中,确保选中从现有容器映像部署一个修订版本选项。

    4. 创建服务页面的容器映像网址中,点击使用示例容器进行测试

    5. 服务名称字段中,输入服务名称或使用默认值。

    6. 区域列表中,使用默认值 us-central1,或选择要在其中部署容器的区域

    7. 身份验证部分中,选择允许公开访问

    8. 点击创建,然后等待容器部署到 Cloud Run。部署后,容器的网址将显示在文本网址:旁边。

    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 信息中心内查看区域。

    Cloud Run 会根据需求自动扩缩容器实例,您只需为处理请求期间所消耗的 CPU、内存和网络付费。

    后续步骤

    如需了解如何通过代码源构建容器,以及如何将容器推送到 Artifact Registry 并进行部署,请参阅以下内容: