使用 GKE 外掛程式安裝
本頁說明如何使用 GKE 外掛程式,在 Google Kubernetes Engine (GKE) 叢集上安裝 Config Connector。
如要進一步瞭解各個安裝選項的優缺點,請參閱「選擇安裝類型」。
事前準備
開始之前,請務必先完成下列工作:
- 啟用 Google Kubernetes Engine API。 啟用 Google Kubernetes Engine API
- 如要使用 Google Cloud CLI 執行這項工作,請安裝並初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行
gcloud components update指令,取得最新版本。較舊的 gcloud CLI 版本可能不支援執行本文件中的指令。
選取或建立 Google Cloud 專案,以便安裝 Config Connector。
如果您先前手動安裝 Config Connector,請先解除安裝,再安裝 Config Connector 外掛程式:
安裝 Config Connector 外掛程式
如要使用 Config Connector 外掛程式,請建立新的 GKE 叢集,或在現有叢集上啟用該外掛程式。安裝 Config Connector 外掛程式後,請使用 Google 服務帳戶和命名空間設定 Config Connector 安裝作業。
需求條件
Config Connector 外掛程式必須符合下列條件:
您必須使用下列 GKE 版本:
- 1.15.11-gke.5 以上版本
- 1.16.8-gke.8 以上版本
- 1.17.4-gke.5 以上版本
您必須在啟用 Config Connector 的叢集上,啟用工作負載身分集區和 Kubernetes Engine Monitoring。
設定 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 發布管道、
rapid和regular。 - PROJECT_ID 替換為您的 Google Cloud 專案 ID。
Google Cloud 控制台
如要使用 Google Cloud 控制台建立叢集,請執行下列步驟:
前往 Google Cloud 控制台的「Create a Kubernetes cluster」(建立 Kubernetes 叢集) 頁面。
在「叢集基本資訊」部分,指定叢集的「名稱」和位置。
在導覽窗格中,按一下「進階設定」。
在「Workload identity」(工作負載身分) 列中,按一下 edit「Edit configuration」(編輯設定),勾選「Enable Workload Identity」(啟用 Workload Identity) 核取方塊,然後按一下「Save」(儲存)。
在「Config Connector」列中,選取「快速啟用」核取方塊。
選用:視需要設定叢集的其餘部分。
點選「建立」。
建立叢集後,請繼續建立身分。
在現有叢集上啟用 Config Connector 外掛程式
您可以使用 gcloud 或 Google Cloud 控制台,在現有 GKE 叢集上啟用 Config Connector 外掛程式。
必要條件
在現有叢集上啟用 Config Connector 外掛程式時,必須符合下列先決條件:
- 您需要符合 Config Connector 外掛程式需求的叢集。
在要安裝 Config Connector 的叢集上,設定 Workload Identity Federation for 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 控制台
前往 Google Cloud 控制台的 Google Kubernetes Engine 選單。
選取要安裝 Config Connector 的叢集。「叢集詳細資料」頁面隨即顯示。
在「功能」部分下方,找到 Config Connector 資料列,然後按一下「編輯」。
選取「Enable Config Connector」(啟用 Config Connector) 核取方塊,然後按一下「Save Changes」(儲存變更),更新叢集。
建立身分
Config Connector 會透過身分與存取權管理 (IAM) 服務帳戶進行驗證,並使用 Workload Identity Federation for GKE 將 IAM 服務帳戶與 Kubernetes 服務帳戶繫結,藉此建立及管理 Google Cloud 資源。
如要建立身分,請完成下列步驟:
建立 IAM 服務帳戶。如要使用現有服務帳戶,可以略過這個步驟,直接使用該帳戶:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME將 SERVICE_ACCOUNT_NAME 替換成服務帳戶的名稱。
如要進一步瞭解如何建立服務帳戶,請參閱「建立及管理服務帳戶」。
在專案中,為 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。
在 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 元件。
如要將運算子設定為叢集模式,請完成下列步驟:
-
將下列 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。
-
使用
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自動升級:
叢集會升級至新的 GKE 子版本,該版本對應至較新的 Config Connector 版本。
叢集使用的 GKE 子版本較新,不適用於一般或穩定版發布管道,且Google Cloud 將該 GKE 子版本重新對應至新的 Config Connector 版本。
當 Google Cloud 重新對應 GKE 子版本,並將現有叢集升級至新的 Config Connector 版本時,即表示「為該 GKE 子版本推出新的 Config Connector 版本」。
Google Cloud 不會將新的 Config Connector 版本推出至舊版 GKE 次要版本,因為這些版本已在 Regular 或 Stable 發布管道上提供,基於穩定性考量,除非偵測到普遍問題,否則不會推出。
如何取得最新版 Config Connector
為確保您使用的是最新版 Config Connector,建議手動安裝 Config Connector 並升級。手動安裝可直接控管版本,並在最新功能和修正項目推出時立即使用。
如要從 Config Connector 外掛程式切換為手動安裝,請按照這篇文章的操作說明進行。
透過 Config Connector 外掛程式安裝的 Config Connector 版本,通常會落後最多 12 個月以上。雖然 Config Controller 比外掛程式新,但與手動安裝相比,仍可能落後於最新版本。
解除安裝 Config Connector
如要解除安裝 Config Connector,請完成下列步驟:
使用
kubectl delete移除 Config Connector CRD 和控制器元件:kubectl delete ConfigConnector configconnector.core.cnrm.cloud.google.com --wait=true使用 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 外掛程式,請按照下列步驟操作。
前往 Google Cloud 控制台的 Google Kubernetes Engine 叢集頁面,然後選取要更新的叢集。
點選「編輯」,系統會顯示「編輯叢集」畫面。
按一下「外掛程式」。
選取「Config Connector」,然後選擇「已停用」。
按一下「儲存」更新叢集。
後續步驟
- 開始使用 Config Connector。
- 瞭解如何排解 Config Connector 問題。