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

本快速入门介绍了如何使用 Config Sync 实现 GitOps 方法来管理 Google Kubernetes Engine 集群。借助 GitOps 工作流,您可以使用 Git 代码库作为集群配置的单一可信来源。

使用 Config Sync 可获得以下好处:

  • 强制保持一致性:有助于确保所有集群具有相同的配置,从而降低配置偏移的风险。
  • 提高安全性:集中管理和审核集群配置,从而更轻松地强制执行安全政策。
  • 提高速度:自动推出配置更改,让您能够更快、更可靠地部署更改。

在本教程中,您将创建一个 GKE 集群,并配置 Config Sync 以从示例代码库同步配置文件。本教程可帮助您了解 Config Sync 的工作原理,并让您体验如何使用 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.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify 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.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

  6. Make sure that you have the following role or roles on the project: Kubernetes Engine 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(保存)。
    8. 创建集群

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

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

      1. 启用 Google Kubernetes Engine API。

        转到 GKE

      2. 在 Google Cloud 控制台中,前往 Kubernetes Engine 页面。

        转到 Google Kubernetes Engine

      3. 点击 创建

      4. Autopilot 部分中,选择配置

      5. 集群基本信息部分的名称字段中输入 cs-cluster。 将其他所有字段保留为建议的默认值。

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

      配置集群

      现在您已拥有一个集群,接下来可以配置 Config Sync 以同步 Git 代码库中的配置文件。

      如需在 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。在软件包详情页面中,您可以简要了解所有已同步的资源,包括资源类型和资源同步到的命名空间。

      (可选)探索示例代码库

      应用到集群的配置在 /config-sync-quickstart/multirepo/ 代码库中定义。此示例演示了多代码库设置,其中集群管理员级配置(由 root 代码库表示)与命名空间级配置(由 namespaces 代码库表示)是分开的。在此示例中,这些配置会拆分到不同的文件夹中。在实际应用场景中,您可以将配置存储在两个具有不同访问权限的不同代码库中。

      从宏观层面来看,集群级代码库或根代码库通常由中央管理员管理,包含集群范围的资源、命名空间配置和政策。命名空间级代码库通常包含特定于各个命名空间的配置,并且通常由应用团队管理。

      您可能会在根代码库中存储以下一些关键类型的文件:

      • 命名空间创建namespace-gamestore.yaml 等文件会创建命名空间本身。
      • 集群范围资源:影响整个集群的文件包括 ClusterRole 对象等资源,这些对象会添加授予权限的集群范围角色。
      • 关联命名空间代码库:在此多代码库设置中,密钥文件为 reposync-gamestore.yaml 文件。此 RepoSync 对象会告知 Config Sync 从其他路径同步 gamestore 命名空间的配置。在此示例中,RepoSync 对象指向同一代码库中的 config-sync-quickstart/multirepo/ namespaces/gamestore 目录,但在实际应用场景中,您可以指向其他 Git 代码库。

      命名空间代码库包含 gamestore 命名空间的特定于应用的配置。例如,configmap-inventory.yaml 文件包含 gamestore 应用的清点数据。这种 GitOps 设置可让应用团队在自己的部署和命名空间范围内的资源方面拥有更多自主权,而无需修改中央根代码库。

      清理

      1. 前往 Google Cloud 控制台中的 GKE 菜单。

        转到 GKE

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

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

      后续步骤