快速入门:从代码库同步配置

在本教程中,您将创建一个 Google Kubernetes Engine (GKE) Enterprise 版本集群,并使用 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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  6. Make sure that you have the following role or roles on the project: GKE Hub Admin

    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(保存)。

创建集群

在本部分中,您将创建一个集群,供本教程使用。在实际场景中,您可能会管理多个集群,但为了简化本教程,您只需创建和管理一个集群。

如需创建集群,请完成以下步骤:

  1. 启用 Google Kubernetes Engine (GKE) Enterprise 版本 API。这对于 Config Sync 至关重要。

    前往 Google Kubernetes Engine (GKE) Enterprise 版本

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

    转到 Google Kubernetes Engine

  3. 点击 创建

  4. Autopilot 部分中,选择配置

  5. 集群基本信息部分的名称字段中输入 cs-cluster

  6. 集群层级部分中,选择企业层级。将其他所有字段保留为建议的默认值。

  7. 点击创建。您将转到 Kubernetes 集群页面。创建集群可能需要几分钟时间。 如果您在集群旁边的状态列中看到绿色对勾标记,则表示该集群已准备就绪。

配置集群

现在您已创建了一个集群,接下来可以配置 Config Sync 以同步到示例代码库的 config-sync-quickstart 目录中的配置。

如需在 Google Cloud 控制台中配置 Config Sync,请完成以下步骤:

  1. 在 Google Cloud 控制台中,转到功能部分下的配置页面。

    转到“配置”

  2. 点击 安装 Config Sync
  3. 选择自动升级,使 Config Sync 可以自动升级版本。
  4. 安装选项下,选择 Install Config Sync on individual clusters(在个别集群上安装 Config Sync)。
  5. 可用集群表中,选择 cs-cluster,然后点击 Install Config Sync(安装 Config Sync)。在设置标签页中,您应该会在几分钟后看到 cs-cluster 的状态为已启用
  6. Config Sync 信息中心上,点击部署集群软件包
  7. 选择要部署软件包的集群表中,选择 cs-cluster,然后点击继续
  8. 保留托管在 Git 上的软件包,然后点击继续
  9. 软件包名称字段中,输入 sample-repository
  10. 代码库网址字段中,输入 https://github.com/GoogleCloudPlatform/anthos-config-management-samples
  11. 路径字段中,输入 config-sync-quickstart/multirepo/root
  12. 其他所有字段保留默认值。
  13. 点击部署软件包

    几分钟后,您应该会在 cs-cluster同步状态列中看到已同步

现在 Config Sync 已同步到代码库,因此它会持续协调集群的状态与代码库中的配置。

查看软件包详细信息

如需查看由 Config Sync 管理的所有对象,请在软件包标签页中点击 cs-cluster,然后查看软件包详细信息页面。在此页面中,您可以简要了解每个软件包同步的所有资源,以及资源类型和资源同步到的命名空间等详细信息。

您可以转到 GitHub 中代码库的 /config-sync-quickstart/multirepo/ 文件夹,探索引发系统创建这些命名空间的配置。 config-sync-quickstart 目录包含 ClusterRole、CustomResourceDefinition、Rolebinding、Namespace 和 RepoSync 配置。它还包含用于监控的 Prometheus Operator 配置。

清理

  1. 转到 Google Cloud 控制台中的 GKE 菜单。

    转到 GKE

  2. cs-cluster 旁边,点击 操作,然后点击 删除

  3. 当系统提示您确认时,再次点击删除

后续步骤