在整个舰队中部署资源

本教程介绍了如何使用舰队软件包在集群舰队中部署 Kubernetes 资源清单。使用 Config Sync 的舰队软件包等 GitOps 工具可帮助您在大量集群中扩容配置管理。

在此教程中,您需要完成以下任务:

  • 将 Git 代码库连接到 Cloud Build
  • 创建集群并将其注册到舰队
  • 将 Config Sync 作为舰队默认设置进行安装
  • 将代码库中的资源部署到集群舰队

准备工作

  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. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the GKE Enterprise, Config Delivery (fleet packages), Cloud Build, Developer Connect APIs:

    gcloud services enable anthos.googleapis.com  configdelivery.googleapis.com  cloudbuild.googleapis.com  developerconnect.googleapis.com
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  12. Make sure that billing is enabled for your Google Cloud project.

  13. Enable the GKE Enterprise, Config Delivery (fleet packages), Cloud Build, Developer Connect APIs:

    gcloud services enable anthos.googleapis.com  configdelivery.googleapis.com  cloudbuild.googleapis.com  developerconnect.googleapis.com
  14. 创建或有权访问 GitHub 账号。
  15. 费用

    在本文档中,您将使用 Google Cloud的以下收费组件:

    您可使用价格计算器根据您的预计使用情况来估算费用。 新 Google Cloud 用户可能有资格申请免费试用

连接到 Git

Git 代码库包含您要在舰队中部署的资源。如需使用舰队软件包部署这些资源,您必须将 Git 代码库连接到 Cloud Build。

创建 Git 代码库

本教程使用 GitHub 作为 Git 提供方。如需创建新的 GitHub 代码库,请完成以下步骤:

  1. 在网络浏览器中,打开 GitHub

  2. 如果需要,请登录您的 GitHub 账号。如果您有权访问 GitHub 上的其他组织或团队,请确保您使用个人账号创建代码库。

  3. 在工具栏中,选择添加 ,然后点击新建代码库

  4. 输入 fleet-package-tutorial 作为代码库的名称。

  5. 保持公开处于选中状态作为代码库的公开范围。

  6. 选择创建代码库

将代码库连接到 Cloud Build

Config Sync 的舰队软件包服务使用 Cloud Build 从 Git 代码库同步和提取 Kubernetes 资源。

如需将 GitHub 代码库连接到 Cloud Build,请完成以下步骤:

  1. 在 Google Cloud 控制台中打开 Cloud Build 页面,然后选择代码库

    打开“代码库”页面

  2. 确保您位于第 2 代代码库页面。如有需要,请选择查看代码库(第 2 代)

  3. 点击创建主机连接

  4. 区域菜单中,选择 us-central1 (Iowa) 作为区域。

  5. 名称字段中,输入 fleet-package-quickstart-connection 作为连接的名称。

  6. 点击连接

  7. 如果您是第一次将 Cloud Build 关联到您的 GitHub 账号,请完成以下步骤:

    1. 接受对 GitHub OAuth 令牌的请求。该令牌会存储在 Secret Manager 中,供 Cloud Build GitHub 连接使用。点击继续
    2. 将 Cloud Build 安装到您的 GitHub 代码库中。选择在新账号中安装
    3. 在打开的新 GitHub 窗口中,选择您之前创建 Cymbal Bank 分支所用的 GitHub 账号。在生产环境中,您可以选择您已向其委托访问权限的其他账号或代码库。
    4. 按照任意身份验证提示在 GitHub 中确认您的身份。
    5. 在用于 Cloud Build 代码库访问的 GitHub 窗口中,选择 Only select repositories
    6. 从列出代码库的下拉菜单中,选择 fleet-package-tutorial 代码库。
    7. 点击保存
  8. 在 Google Cloud 控制台的 Cloud Build 页面中,点击关联代码库以将新的 Git 代码库连接到 Cloud Build。

  9. 连接菜单中,选择 fleet-package-quickstart-connection

  10. 代码库菜单中,选择 fleet-package-tutorial 代码库。

  11. 选择关联

设置舰队

在本部分中,您将通过创建集群、将其注册到舰队以及将 Config Sync 作为舰队软件包安装来设置舰队。

创建集群

为了演示如何使用舰队软件包在多个集群中部署资源,本教程介绍了如何创建两个集群。

如需创建两个集群并将其注册到项目的舰队,请执行以下操作:

  1. 创建 GKE 集群:

    gcloud container clusters create-auto cluster1 \
      --project=PROJECT_ID \
      --region=REGION \
      --fleet-project=PROJECT_ID \
      --release-channel=rapid
    

    替换以下内容:

    • PROJECT_ID 替换为您的项目 ID。
    • REGION 替换为要在其中创建集群的区域,例如 us-central1
  2. 创建另一个 GKE 集群:

    gcloud container clusters create-auto cluster2 \
      --project=PROJECT_ID \
      --region=REGION \
      --fleet-project=PROJECT_ID \
      --release-channel=rapid
    

将 Config Sync 作为舰队默认设置进行安装

如需使用舰队软件包服务,必须在两个集群上都安装 Config Sync。通过完成以下步骤,您可以一次性在多个集群以及未来注册到舰队的任何集群上安装 Config Sync:

  1. 在 Google Cloud 控制台中,前往 Feature Manager 页面。

    前往 Feature Manager

  2. Config Sync 窗格中,点击配置

  3. 点击自定义舰队设置。在随即显示的对话框中,选择要安装的 Config Sync 版本。

  4. 点击配置

  5. 配置舰队设置确认对话框中,点击确认。如果您之前未启用 Config Sync,则点击确认还会启用 anthosconfigmanagement.googleapis.com API。

  6. 舰队中的集群表中,选择两个集群,然后点击同步到舰队设置。这会在两个集群上安装 Config Sync 以及您配置的设置。

    集群可能需要几分钟时间才能同步。当 Config Sync 显示为已安装时,您可以继续执行后续步骤。

为 Cloud Build 设置服务账号

如需创建服务账号并向 Cloud Build 授予所需的权限,请完成以下步骤:

  1. 创建服务账号:

    gcloud iam service-accounts create "quickstart-service-account"
    
  2. 通过为 Resource Bundle Publisher 角色添加 IAM 政策绑定,向服务账号授予从 Git 代码库提取资源的权限:

    gcloud projects add-iam-policy-binding PROJECT_ID \
       --member="serviceAccount:quickstart-service-account@PROJECT_ID.iam.gserviceaccount.com" \
       --role='roles/configdelivery.resourceBundlePublisher'
    

    如果系统提示,请选择 None 作为政策的条件。

  3. 通过为 Logs Writer 角色添加 IAM 政策绑定,向服务账号授予写入日志的权限:

    gcloud projects add-iam-policy-binding PROJECT_ID \
       --member="serviceAccount:quickstart-service-account@PROJECT_ID.iam.gserviceaccount.com" \
       --role='roles/logging.logWriter'
    

    如果系统提示,请选择 None 作为政策的条件。

在整个舰队中部署资源

在本教程中,您将向 Git 代码库添加包含 nginx 部署的 Kubernetes 清单,发布版本,然后创建舰队软件包来部署 nginx 应用。

将 Kubernetes 清单提交到代码库

如需将资源添加到 GitHub 并发布版本,请完成以下步骤:

  1. 在 GitHub 代码库的网络浏览器窗口中,点击添加文件,然后点击创建新文件

  2. 将文件命名为 deployment.yaml,然后将以下内容粘贴到其中:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - image: nginx:1.14.2
            name: nginx
            ports:
            - containerPort: 80
    
  3. 点击提交更改...

  4. 在确认对话框中,保持 Commit directly to the main branch 处于选中状态,然后点击提交更改

  5. 在代码库的主页面上,从边栏中选择版本

  6. 在页面顶部,选择 Draft a new release

  7. 选择 Choose a tag 菜单,然后输入 v1.0.0 作为标记。点击 Create new tag

  8. 点击 Publish release

使用舰队软件包将资源部署到集群

如需部署新资源,请创建新的舰队软件包:

  1. 此舰队软件包不包含选择器字段,因此针对舰队中的所有集群。这也意味着,未来添加到舰队的任何集群都会自动添加 nginx 部署。

    在 Cloud Shell 中,创建一个名为 fleet-package.yaml 的文件,其中包含以下内容:

    resourceBundleSelector:
      cloudBuildRepository:
        name: projects/PROJECT_ID/locations/us-central1/connections/fleet-package-quickstart-connection/repositories/REPOSITORY_NAME
        tag: v1.0.0
        serviceAccount: projects/PROJECT_ID/serviceAccounts/quickstart-service-account@PROJECT_ID.iam.gserviceaccount.com
        path:
    target:
      fleet:
        project: projects/PROJECT_ID
    rolloutStrategy:
      rolling:
        maxConcurrent: 1
    

    REPOSITORY_NAME 替换为 Cloud Build 中的代码库名称。其格式通常为 USERNAME-REPOSITORY_NAME

  2. 创建舰队软件包以开始部署:

    gcloud alpha container fleet packages create fp-nginx \
       --source=fleet-package.yaml \
       --project=PROJECT_ID
    
  3. 验证是否已创建舰队软件包:

    gcloud alpha container fleet packages list
    

    您可以点击所提供的链接以查看 Cloud Build 作业的流式日志。

    舰队软件包开始在舰队中发布 Kubernetes 资源。

  4. 在 Google Cloud 控制台的 Google Kubernetes Engine 页面中,前往工作负载页面,以查看在所有 GKE 集群上部署的工作负载的汇总视图:

    打开“工作负载”页面

    工作负载可能需要几分钟才能变为可用状态。当 Autopilot 针对新部署调整资源请求时,可能还会发生可用性错误。

    请注意,由于在舰队软件包定义中将 maxConcurrent: 设置为 1,因此舰队软件包 API 会等到 nginx-deployment 完全部署在在一个集群上,才会开始部署到第二个集群。如果您将发布策略更改为 maxConcurrent: 2 或更高版本,则资源会同时部署到这两个集群。

    几分钟后,您会在两个集群上看到 nginx-deployment 的两个新工作负载。您可能需要刷新页面。

您可以使用舰队软件包继续探索不同部署策略。 例如,您可以向舰队添加新集群,以观察工作负载是否会自动部署到这一新集群。如需详细了解部署策略和变体,请参阅部署舰队软件包

清理

为避免因本页面中使用的资源导致您的 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.

您可以通过完成以下步骤删除代码库:

  1. 在 Cymbal Bank 的 GitHub 分支的网络浏览器窗口中,点击代码库名称下方的设置

  2. 在“常规设置”页面(默认处于选中状态)上,前往“危险区域”部分,然后点击删除此代码库

  3. 点击 I want to delete this repository

  4. 阅读警告,然后点击 I have read and understand these effects

  5. 如需验证您删除的代码库是否正确,请在文本字段中输入代码库的名称。

  6. 点击删除此代码库

后续步骤