使用 GKE 插件进行安装


本页面介绍如何使用 Config Connector 插件在 Google Kubernetes Engine (GKE) 集群上安装 Config Connector。

如需详细了解每种安装选项及其优缺点,请参阅选择安装类型

准备工作

在开始之前,请确保您已执行以下任务:

  • 启用 Google Kubernetes Engine API。
  • 启用 Google Kubernetes Engine API
  • 如果您要使用 Google Cloud CLI 执行此任务,请安装初始化 gcloud CLI。 如果您之前安装了 gcloud CLI,请运行 gcloud components update 以获取最新版本。

安装 Config Connector 插件

通过创建新 GKE 集群或在现有集群上启用 Config Connector 插件,您就可以使用该插件。安装 Config Connector 插件后,使用 Google 服务账号和命名空间配置 Config Connector 安装。

要求

Config Connector 插件有以下要求:

设置 GKE 集群

您可以在新集群或现有集群上使用 Config Connector 插件。

创建启用了 Config Connector 插件的新集群

您可以使用 gcloud CLI 或Google Cloud 控制台创建 GKE 集群。

gcloud

如需使用 Google Cloud CLI 创建集群,请运行以下命令:

gcloud container clusters create CLUSTER_NAME \
    --release-channel CHANNEL \
    --addons ConfigConnector \
    --workload-pool=PROJECT_ID.svc.id.goog \
    --logging=SYSTEM \
    --monitoring=SYSTEM

请替换以下内容:

  • CLUSTER_NAME 替换为您的 GKE 集群的名称。
  • CHANNEL 替换为 GKE 发布渠道,支持 rapidregular
  • PROJECT_ID 替换为您的 Google Cloud 项目 ID。

Google Cloud 控制台

如需使用 Google Cloud 控制台创建集群,请执行以下步骤:

  1. 访问 Google Cloud 控制台中的 Google Kubernetes Engine 菜单。

    访问 Google Kubernetes Engine 菜单

  2. 点击创建。此时会显示创建一个 Kubernetes 集群页面。

  3. 为集群指定名称

  4. 选择支持的主版本

  5. 根据需要配置集群的其余部分。

  6. 在导航窗格的集群下,点击安全

  7. 选中启用 Workload Identity 复选框。

  8. 在左侧的导航窗格中,点击集群下的功能

  9. 选中启用 Config Connector 复选框。

  10. 点击创建

创建集群后,请转至创建身份

在现有集群上启用 Config Connector 插件

您可以使用 gcloud 或 Google Cloud 控制台在现有 GKE 集群上启用 Config Connector 插件。

前提条件

在现有集群上启用 Config Connector 插件需要满足以下前提条件:

  • 您需要一个满足 Config Connector 插件要求的集群。
  • 在要安装 Config Connector 的集群上设置适用于 GKE 的工作负载身份联合

如需为节点池启用 Workload Identity Federation for GKE,请使用 gcloud 命令行工具:

gcloud container node-pools update NODE_POOL \
    --workload-metadata=GKE_METADATA \
    --cluster CLUSTER_NAME

替换以下内容:

  • NODE_POOL 替换为您的节点池的名称
  • CLUSTER_NAME 替换为您的集群的名称
启用 Config Connector 插件

您可以使用 Google Cloud CLI 或 Google Cloud 控制台在现有 GKE 集群中启用 Config Connector 插件。

gcloud

如需在现有 GKE 集群中启用 Config Connector 插件,请使用 Google Cloud CLI:

gcloud container clusters update CLUSTER_NAME \
    --update-addons ConfigConnector=ENABLED

CLUSTER_NAME 替换为 GKE 集群的名称。

Google Cloud 控制台

  1. 访问 Google Cloud 控制台中的 Google Kubernetes Engine 菜单。

    访问 Google Kubernetes Engine 菜单

  2. 选择要在其中安装 Config Connector 的集群。此时会显示集群详情页面。

  3. 功能部分下,找到 Config Connector 行,然后点击 修改

  4. 选中启用 Config Connector 复选框,然后点击保存更改以更新您的集群。

创建身份

Config Connector 通过使用 Identity and Access Management (IAM) 服务账号进行身份验证以及使用 Workload Identity Federation for GKE 将 IAM 服务账号与 Kubernetes 服务账号绑定来创建和管理 Google Cloud 资源。

如需创建身份,请完成以下步骤:

  1. 创建 IAM 服务账号。 如果您希望使用现有服务账号,则可以使用该账号并跳过此步骤:

    gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
    

    SERVICE_ACCOUNT_NAME 替换为您的服务账号的名称。

    如需详细了解如何创建服务账号,请参阅创建和管理服务账号

  2. 向 IAM 服务账号授予对项目的提升权限:

    gcloud projects add-iam-policy-binding PROJECT_ID \
      --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \
      --role="roles/editor"
    

    PROJECT_ID 替换为您的项目 ID。

  3. 在 IAM 服务账号与 Config Connector 运行的预定义 Kubernetes 服务账号之间创建 IAM 政策绑定:

    gcloud iam service-accounts add-iam-policy-binding \
    SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \
      --member="serviceAccount:PROJECT_ID.svc.id.goog[cnrm-system/cnrm-controller-manager]" \
      --role="roles/iam.workloadIdentityUser"
    

配置 Config Connector

要完成安装,请为 ConfigConnector CustomResource 创建配置文件,然后使用 kubectl apply 命令应用该配置文件。Config Connector Operator 在集群中安装Google Cloud 资源 CRD 和 Config Connector 组件。

如需将操作员配置为集群模式,请完成以下步骤:

  1. 将以下 YAML 文件复制到名为 configconnector.yaml 的文件中:
    # configconnector.yaml
    apiVersion: core.cnrm.cloud.google.com/v1beta1
    kind: ConfigConnector
    metadata:
      # the name is restricted to ensure that there is only one
      # ConfigConnector resource installed in your cluster
      name: configconnector.core.cnrm.cloud.google.com
    spec:
      mode: cluster
      googleServiceAccount: "SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com"
      # Setting `stateIntoSpec` to `Absent` is recommended. It means setting `cnrm.cloud.google.com/state-into-spec`
      # annotation to `absent` for all Config Connector resources created in the cluster in the future.
      # It prevents Config Connector from populating unspecified fields into the spec.
      stateIntoSpec: Absent
    替换以下内容:
    • SERVICE_ACCOUNT_NAME 替换为您的服务账号的名称。
    • PROJECT_ID 替换为您的项目 ID。
  2. 使用 kubectl apply 将配置应用到集群:
      kubectl apply -f configconnector.yaml

指定创建资源的位置

Config Connector 可以按项目、文件夹或组织整理资源,这与使用 Google Cloud整理资源的方式相同。

在使用 Config Connector 创建资源之前,必须先配置创建资源的位置。为了确定创建资源的位置,Config Connector 会在资源配置或现有命名空间中使用注释。如需了解详情,请参阅整理资源

如果您没有可用于此目的的命名空间,请使用 kubectl 创建一个命名空间。
kubectl create namespace NAMESPACE

NAMESPACE 替换为您的命名空间名称。 例如 config-connector

选择一个标签页以便选择 Config Connector 创建资源的位置。

项目

如需在特定项目中创建资源,请运行以下命令:

    kubectl annotate namespace \
    NAMESPACE cnrm.cloud.google.com/project-id=PROJECT_ID

请替换以下内容:

  • NAMESPACE 替换为您的命名空间名称。
  • PROJECT_ID 替换为您的 Google Cloud 项目 ID。

文件夹

如需在特定文件夹中创建资源,请运行以下命令:

    kubectl annotate namespace \
    NAMESPACE cnrm.cloud.google.com/folder-id=FOLDER_ID

请替换以下内容:

  • NAMESPACE 替换为您的命名空间名称。
  • FOLDER_ID 替换为您的 Google Cloud 文件夹 ID。

组织

如需在特定组织中创建资源,请运行以下命令:

    kubectl annotate namespace \
    NAMESPACE cnrm.cloud.google.com/organization-id=ORGANIZATION_ID

请替换以下内容:

  • NAMESPACE 替换为您的命名空间名称。
  • ORGANIZATION_ID 替换为您的 Google Cloud 组织 ID。

为命名空间添加注释时,Config Connector 将在相应的项目、文件夹或组织中创建资源。如需详细了解 Config Connector 如何使用 Kubernetes 命名空间,请参阅 Kubernetes 命名空间和 Google Cloud 项目

验证安装

Config Connector 在命名空间 cnrm-system 中运行其所有组件。您可以通过运行以下命令来验证 Pod 是否准备就绪:

kubectl wait -n cnrm-system \
      --for=condition=Ready pod --all

如果正确安装了 Config Connector,则输出类似于以下内容:

pod/cnrm-controller-manager-0 condition met

升级 Config Connector

集群的 Config Connector 插件升级由 Google Cloud管理。

无论何时进行升级,集群中的资源都会保留。

您可以了解 Google Cloud 如何管理 Config Connector 插件升级,也可以跳过此部分,直接了解如何获取最新版本的 Config Connector

Google Cloud 如何管理 Config Connector 插件升级

GKE 集群获得的 Config Connector 版本完全取决于集群的 GKE 次要版本。例如:

GKE 次要版本 Config Connector 版本
1.20 1.69.0
1.21 1.69.0
1.22 1.71.0
1.23 1.82.0
1.24 1.89.0

随着版本的发布, Google Cloud 会将最新的 GKE 次要版本重新映射到新发布的 Config Connector 版本。对于足够新以至于能通过常规或稳定发布渠道获取的所有 GKE 次要版本,都会执行此操作。

由于集群获得的 Config Connector 版本取决于集群的 GKE 次要版本,因此在以下两种情况下,集群的 Config Connector 插件会由 Google Cloud自动升级:

  1. 集群升级到与较新 Config Connector 版本对应的新的 GKE 次要版本。

  2. 集群所用的 GKE 次要版本足够新,无法在常规或稳定发布渠道中使用,并且Google Cloud 将该 GKE 次要版本重新映射到新的 Config Connector 版本。

当 Google Cloud 重新映射 GKE 次要版本并将现有集群升级到新的 Config Connector 版本时,我们称之为“向该 GKE 次要版本推出新的 Config Connector 版本”。

Google Cloud 不会向足够旧的 GKE 次要版本推出新的 Config Connector 版本,以便在常规或稳定发布渠道版本中提供,除非在检测到广泛问题的紧急情况下。

如何获取最新版本的 Config Connector

通过 Config Connector 插件安装的 Config Connector 版本通常会落后最多 12 个月或更长时间。如果您需要最新版本的 Config Connector,建议您改用 Config Controller自行手动安装 Config Connector 并执行升级。每种方案的迁移说明如下:

  • 如需改用 Config Controller,请按照 Config Controller 迁移说明将现有的 Config Connector 资源迁移到 Config Controller 实例。

  • 如需手动安装 Config Connector,请按照说明从 Config Connector 插件切换到手动安装

卸载 Config Connector

如需卸载 Config Connector,请完成以下步骤:

  1. 使用 kubectl delete 移除 Config Connector CRD 和控制器组件:

    kubectl delete ConfigConnector configconnector.core.cnrm.cloud.google.com --wait=true
    
  2. 使用 gcloud CLI 或 Google Cloud 控制台在集群中停用 Config Connector 插件:

    gcloud

    要使用 gcloud 停用 Config Connector 插件,请运行以下命令:

    gcloud container clusters update CLUSTER_NAME --update-addons ConfigConnector=DISABLED
    

    CLUSTER_NAME 替换为安装了 Config Connector 插件的集群的名称。

    Cloud 控制台

    如需从 Google Cloud 控制台中停用 Config Connector 插件,请执行以下步骤。

    1. 前往 Google Cloud 控制台上的“Google Kubernetes Engine”集群页面,然后选择要更新的集群。

      访问 Google Kubernetes Engine 菜单

    2. 点击修改。系统会显示“修改集群”屏幕。

    3. 点击插件

    4. 选择 Config Connector,然后选择已停用

    5. 点击保存以更新您的集群。

后续步骤