快速入門導覽課程:從 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. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
    8. 建立叢集

      在本節中,您將建立可用於本教學課程的叢集。 雖然在實際使用情況中,您可能會管理多個叢集,但為了簡化本教學課程,您只會建立及管理一個叢集。

      如要建立叢集,請完成下列步驟:

      1. 啟用 Google Kubernetes Engine API。

        前往 GKE

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

        前往「Google Kubernetes Engine」

      3. 按一下「 Create」(建立)

      4. 在「Autopilot」部分,選取「Configure」(設定)

      5. 在「Cluster basics」(叢集基本資訊) 專區的「Name」(名稱) 欄位中,輸入 cs-cluster。建議在所有其他欄位保留預設值。

      6. 按一下「Create」(建立)。系統會將您帶往「Kubernetes clusters」(Kubernetes 叢集) 頁面。叢集會在幾分鐘內建立完成。在叢集旁的「狀態」欄看到綠色勾號,就表示叢集已準備就緒。

      設定叢集

      現在您已擁有叢集,可以設定 Config Sync,從 Git 存放區同步設定檔。

      如要在 Google Cloud 控制台中設定 Config Sync,請完成下列步驟:

      1. 前往 Google Cloud 控制台的「Features」(功能) 區段下「Config」(設定) 頁面。

        前往「設定」

      2. 按一下「安裝 Config Sync」
      3. 選取要使用的 Config Sync 版本。
      4. 在「安裝選項」下方,選取「在個別叢集上安裝 Config Sync」
      5. 在「Available clusters」(可用叢集) 表格中,選取 cs-cluster 並按一下「Install Config Sync」(安裝 Config Sync)。 在「設定」分頁中,幾分鐘後應該會看到 cs-cluster 的狀態為「已啟用」
      6. Config Sync 資訊主頁上,按一下「Deploy cluster package」(部署叢集套件)
      7. 在「Select clusters for package deployment」(選取要部署套件的叢集) 表格中,選取 cs-cluster,然後按一下「Continue」(繼續)
      8. 保留選取的「Package hosted on Git」(Git 上的套件),然後按一下「Continue」(繼續)
      9. 在「Package name」(套件名稱) 欄位中,輸入 sample-repository
      10. 在「Repository URL」(存放區網址) 欄位中,輸入 https://github.com/GoogleCloudPlatform/anthos-config-management-samples
      11. 在「Path」(路徑) 欄位中輸入 config-sync-quickstart/multirepo/root
      12. 所有其他欄位則保留預設值。
      13. 按一下「Deploy Package」(部署套件)

        幾分鐘後,cs-cluster 的「Sync status」(同步處理狀態) 欄中應該會顯示「Synced」(同步處理完成)

      Config Sync 與存放區同步後,會持續使用存放區中的設定來協調叢集狀態。

      查看套裝組合詳細資料

      如要查看 Config Sync 管理的物件,請前往「Packages」(套件)分頁,然後按一下 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. 系統提示您進行確認時,再按一下 [Delete] (刪除)

      後續步驟