本快速入門導覽課程說明如何使用 Config Sync,以 GitOps 方法管理 Google Kubernetes Engine 叢集。透過 GitOps 工作流程,您可以使用 Git 存放區做為叢集設定的單一可靠資料來源。
使用 Config Sync 可享有下列優點:
- 確保一致性:確保所有叢集都採用相同設定,降低設定偏移的風險。
- 提升安全性:集中管理及稽核叢集設定,更輕鬆地強制執行安全政策。
- 提高速度:自動推出設定變更,讓您更快更可靠地部署變更。
在本教學課程中,您將建立 GKE 叢集,並設定 Config Sync 從範例存放區同步處理設定檔。本教學課程可協助您瞭解 Config Sync 的運作方式,並體驗如何使用 Config Sync,以一致且自動化的方式管理叢集。
事前準備
- 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.
-
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 theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
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 theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Make sure that you have the following role or roles on the project: Kubernetes Engine Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
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.
- 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
-
In the Google Cloud console, go to the IAM page.
前往 IAM - 選取專案。
- 按一下「授予存取權」 。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
- 按一下 [Save]。
啟用 Google Kubernetes Engine API。
在 Google Cloud 控制台中,前往「Kubernetes Engine」頁面。
按一下「add_box Create」(建立)。
在「Autopilot」部分,選取「Configure」(設定)。
在「Cluster basics」(叢集基本資訊) 專區的「Name」(名稱) 欄位中,輸入
cs-cluster
。建議在所有其他欄位保留預設值。按一下「Create」(建立)。系統會將您帶往「Kubernetes clusters」(Kubernetes 叢集) 頁面。叢集會在幾分鐘內建立完成。在叢集旁的「狀態」欄看到綠色勾號,就表示叢集已準備就緒。
- 前往 Google Cloud 控制台的「Features」(功能) 區段下「Config」(設定) 頁面。
- 按一下「安裝 Config Sync」add。
- 選取要使用的 Config Sync 版本。
- 在「安裝選項」下方,選取「在個別叢集上安裝 Config Sync」。
- 在「Available clusters」(可用叢集) 表格中,選取
cs-cluster
並按一下「Install Config Sync」(安裝 Config Sync)。 在「設定」分頁中,幾分鐘後應該會看到cs-cluster
的狀態為「已啟用」。 - 在 Config Sync 資訊主頁上,按一下「Deploy cluster package」(部署叢集套件)。
- 在「Select clusters for package deployment」(選取要部署套件的叢集) 表格中,選取
cs-cluster
,然後按一下「Continue」(繼續)。 - 保留選取的「Package hosted on Git」(Git 上的套件),然後按一下「Continue」(繼續)。
- 在「Package name」(套件名稱) 欄位中,輸入
sample-repository
。 - 在「Repository URL」(存放區網址) 欄位中,輸入
https://github.com/GoogleCloudPlatform/anthos-config-management-samples
。 - 在「Path」(路徑) 欄位中輸入
config-sync-quickstart/multirepo/root
。 - 所有其他欄位則保留預設值。
按一下「Deploy Package」(部署套件)。
幾分鐘後,
cs-cluster
的「Sync status」(同步處理狀態) 欄中應該會顯示「Synced」(同步處理完成)。- 命名空間建立:
namespace-gamestore.yaml
等檔案會建立命名空間本身。 - 叢集範圍資源:影響整個叢集的文件包括
ClusterRole
物件等資源,這些物件會新增叢集範圍的角色,授予權限。 - 連結命名空間存放區:在這個多存放區設定中,金鑰檔案是
reposync-gamestore.yaml
檔案。這個RepoSync
物件會告知 Config Sync,要從其他路徑同步處理gamestore
命名空間的設定。在本範例中,RepoSync
物件指向同一存放區內的config-sync-quickstart/multirepo/ namespaces/gamestore
目錄,但在實際情境中,您可以指向不同的 Git 存放區。 前往 Google Cloud 控制台的 GKE 選單。
按一下
cs-cluster
旁的 more_vert「動作」,然後按一下 delete「刪除」。系統提示您進行確認時,再按一下 [Delete] (刪除)。
- 瞭解如何驗證設定。
建立叢集
在本節中,您將建立可用於本教學課程的叢集。 雖然在實際使用情況中,您可能會管理多個叢集,但為了簡化本教學課程,您只會建立及管理一個叢集。
如要建立叢集,請完成下列步驟:
設定叢集
現在您已擁有叢集,可以設定 Config Sync,從 Git 存放區同步設定檔。
如要在 Google Cloud 控制台中設定 Config Sync,請完成下列步驟:
Config Sync 與存放區同步後,會持續使用存放區中的設定來協調叢集狀態。
查看套裝組合詳細資料
如要查看 Config Sync 管理的物件,請前往「Packages」(套件)分頁,然後按一下
cs-cluster
。套件詳細資料頁面會顯示所有已同步資源的總覽,包括資源類型和資源同步的命名空間。(選用) 探索範例存放區
套用至叢集的設定定義於
/config-sync-quickstart/multirepo/
存放區。這個範例展示多存放區設定,其中叢集管理員層級的設定 (以root
存放區表示) 與命名空間層級的設定 (以namespaces
存放區表示) 分開。在本範例中,這些設定會分成不同資料夾。在實際情況中,您可能會將設定儲存在兩個不同的存放區,並設定不同的存取權限。從高層次來看,叢集層級或根存放區通常由中央管理員管理,並包含叢集層級的資源、命名空間設定和政策。命名空間層級的存放區通常包含特定命名空間的設定,且通常由應用程式團隊管理。
您可能會在根存放區中儲存下列主要檔案類型:
命名空間存放區包含
gamestore
命名空間的應用程式專屬設定。舉例來說,configmap-inventory.yaml
檔案包含gamestore
應用程式的目錄資料。這種 GitOps 設定可讓應用程式團隊對自己的部署作業和命名空間範圍資源擁有更多自主權,不必修改中央根存放區。清除所用資源
後續步驟
-