快速入门:部署到 Cloud Run for Anthos

了解如何创建启用了 Cloud Run for Anthos 的集群,然后将预建的示例容器部署到该集群。

如果您有演示版帐号,则可以按照 Google Cloud Skills Boost 上的快速入门进行操作。

准备工作

  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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  6. 如需确保您拥有最新版本的 Google Cloud CLI,请安装或更新 Google Cloud CLI:

设置命令行环境并启用所需的 API

完成以下步骤,为 Cloud Run for Anthos 设置命令行环境并启用所需的 API:

  1. 配置 Google Cloud CLI 以默认使用 Google Cloud 项目的 ID:

    gcloud config set project PROJECT-ID

    PROJECT-ID 替换为您的项目 ID。

  2. 设置要在其中部署新集群的可用区。您可以使用支持 GKE 的任何可用区。例如:

    gcloud config set compute/zone ZONE

    ZONE 替换为所需的可用区。

  3. 运行 gcloud services 命令以在 Google Cloud 项目中启用以下 API:

    • Google Kubernetes Engine API:创建 GKE 集群。
    • Cloud Build API:构建容器。
    • Container Registry API:将容器发布到 Container Registry。
    gcloud services enable container.googleapis.com containerregistry.googleapis.com cloudbuild.googleapis.com

    这可能需要几秒钟的时间才能完成。启用这些 API 后,命令行会显示如下所示的消息:

    Operation "operations/..." finished successfully.

创建已启用 Cloud Run for Anthos 的 GKE 集群

完成以下步骤,以创建集群并为 Cloud Run for Anthos 启用该集群:

  1. 使用以下命令创建一个新集群:

    gcloud container clusters create CLUSTER_NAME \
      --addons=HttpLoadBalancing,CloudRun \
      --machine-type=e2-standard-4 \
      --num-nodes=4 \
      --enable-stackdriver-kubernetes

    CLUSTER_NAME 替换为您想要使用的集群名称。

  2. 等待集群创建过程完成。在创建过程中,您应该会看到类似如下所示的消息:

    Creating cluster my-cluster...done.
    Created [https://container.googleapis.com/v1beta1/ \
    projects/my-project/zones/us-central1-b/clusters/my-cluster].
    

    其中,my-project 是您的 Google Cloud 项目的 ID,my-cluster 是您刚刚创建的集群。

  3. 将 Cloud Run for Anthos 资源的 gcloud 默认值设置为新集群的名称及其位置。设置这些默认值可以避免在每次使用 Google Cloud CLI 时指定这些值。

    gcloud config set run/platform gke
    gcloud config set run/cluster CLUSTER_NAME
    gcloud config set run/cluster_location ZONE

    CLUSTER_NAMEZONE 替换为您用于创建新集群的值。

部署示例容器

使用 Google Cloud Console 在您的集群中部署示例容器并创建服务:

  1. 在 Google Cloud 控制台中,转到 Cloud Run for Anthos 页面

    转到 Cloud Run for Anthos

  2. 点击创建服务以打开创建服务表单。

  3. 在“可用集群”下拉菜单中,选择刚创建的集群。

  4. 保留 default 作为命名空间的名称。

  5. 输入服务名称,例如 hello

  6. 点击下一步

  7. 选择从现有容器映像部署一个修订版本,然后从演示版容器下拉列表中选择 hello

  8. 点击下一步

  9. 在“连接”下,选择外部,以便调用服务。

  10. 点击创建以将映像部署到 Cloud Run for Anthos,然后等待部署完成。

恭喜!您刚刚将一项服务部署到启用了 Cloud Run for Anthos 的集群。

访问已部署的服务

现在,您已经有了一项正常运行的服务,可以向其发送请求。在本部分中,cURL 用于演示如何访问服务以及验证服务是否正常运行:

  1. 在 Google Cloud 控制台中,转到 Cloud Run for Anthos 页面

    转到 Cloud Run for Anthos

  2. 点击新 Cloud Run for Anthos 服务的名称,以转到其服务详情页面。

  3. 点击服务网址右侧的信息

  4. 点击在 Cloud Shell 中调用,以便在 Cloud Shell 中为服务运行生成的 cURL 命令。

cURL 请求会返回服务正在运行的示例容器的源代码。现在,您已经验证服务正在处理请求!

清理

您可以停用 Cloud Run for Anthos,也可以删除集群和 Cloud Run for Anthos 服务,以避免因运行这些资源而产生费用。

停用 Cloud Run for Anthos

如需停用 Cloud Run for Anthos 并保留您的集群,请执行以下操作:

  1. 在 Google Cloud 控制台中,转到 Google Kubernetes Engine 页面

    转到 Google Kubernetes Engine

  2. 点击要停用 Cloud Run for Anthos 的集群。

  3. 点击修改

  4. 向下滚动到“Anthos 功能”,然后从“Cloud Run for Anthos”下拉列表中选择停用

  5. 点击保存

删除 Cloud Run for Anthos

如需永久删除您的 GKE 集群(包括 Cloud Run for Anthos 服务及其所有资源),请执行以下操作:

  1. 在 Google Cloud 控制台中,转到 Google Kubernetes Engine 页面

    转到 Google Kubernetes Engine

  2. 选择需要删除的集群。

  3. 点击删除,然后在弹出式窗口中再次点击删除

后续步骤

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

如需查看 Cloud Run for Anthos 的架构概览(其中介绍了将 Cloud Run for Anthos 作为插件安装到 Google Kubernetes Engine 集群时发生的更改),请参阅以下内容: