快速入门:将 Angular SSR 服务部署到 Cloud Run

了解如何创建简单的 Hello World 应用,将该应用打包到容器映像中,然后将该容器映像上传到 Artifact Registry 并部署到 Cloud Run。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. Install the Google Cloud CLI.
  5. To initialize the gcloud CLI, run the following command:

    gcloud init
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. 确保您的 Google Cloud 项目已启用结算功能

  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. 要为 Cloud Run 服务设置默认项目,请使用以下命令:
     gcloud config set project PROJECT_ID
    PROJECT_ID 替换为您为此快速入门创建的项目的名称。
  11. 如果您尚未安装 Node.js 20 或更高版本,请安装 Node.js
  12. 如果您通过网域限制组织政策来限制项目的未经身份验证的调用,则您需要按照测试专用服务中的说明访问已部署的服务。

  13. 启用 Cloud Run Admin API:

    gcloud services enable run.googleapis.com
    

    启用 Cloud Run Admin API 后,系统会自动创建 Compute Engine 默认服务账号。

  14. 为了让 Cloud Build 能够构建来源,请运行以下命令,将 Cloud Build Service Account 角色授予给 Compute Engine 默认服务账号:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \
        --role=roles/cloudbuild.builds.builder
    

    PROJECT_NUMBER 替换为您的 Google Cloud 项目编号,并将 PROJECT_ID 替换为 Google Cloud 项目 ID。

    您可以在 Google Cloud 控制台的欢迎页面上找到项目编号和项目 ID。

编写示例服务

如需创建和部署 Angular SSR 服务,请按以下步骤操作:

  1. 要创建名为 helloworld 的新 Angular SSR 项目,请使用以下命令:

    npx @angular/cli@17.3.6 new helloworld --ssr
    

    当系统提示您接受默认值时,请按 Enter

  2. 转到 helloworld 目录:

    cd helloworld
    

您的应用已编写完毕,可以进行部署。

从源代码部署到 Cloud Run

重要提示:本快速入门假定您在快速入门中使用的项目中拥有所有者或编辑者角色。否则,请参阅 Cloud Run Source Developer 角色,了解从源代码部署 Cloud Run 资源所需的权限。

从源代码部署会自动从源代码构建容器映像并进行部署。

如需从源代码部署,请执行以下操作:

  1. 在源代码目录中,使用以下命令从源代码进行部署:

    gcloud run deploy

    如果系统提示您启用 API,请回复 y 进行启用。

    1. 当系统提示您输入源代码位置时,请按 Enter 键部署当前文件夹。

    2. 当系统提示您输入服务名称时,请按 Enter 键接受默认名称,例如 helloworld

    3. 如果系统提示您对项目启用相关 API(例如 Artifact Registry API),请按 y 进行响应。

    4. 当系统提示您输入区域时:请选择您选择的区域,例如 us-central1

    5. 如果系统提示您在指定区域中创建仓库,请按 y 进行响应。

    6. 如果系统提示您允许未经过身份验证的调用:回复 y。如果有网域限制组织政策阻止此提示,您可能不会看到此提示;如需了解详情,请参阅准备工作部分。

    然后等待部署完成。成功完成时,命令行会显示服务网址。

  2. 在 Web 浏览器中打开该服务网址,访问部署的服务。

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-south1(印度孟买)
  • 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。Cloud Run 会在需要处理收到的请求时自动横向扩容您的容器映像,并在需要处理的请求数量减少时自动横向缩容您的容器映像。您只需为在处理请求期间消耗的 CPU、内存和网络付费。

清理

移除测试项目

虽然 Cloud Run 不会对未在使用中的服务计费,但您可能仍然需要支付将容器映像存储在 Artifact Registry 中而产生的相关费用。为避免产生费用,您可以删除容器映像或删除 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.

后续步骤

如需详细了解如何使用代码源构建容器并推送到仓库,请参阅:

如需为服务器渲染的 Angular 应用自动集成 GitHub 并使用 CDN 进行优化部署,请考虑使用 Firebase App Hosting