以命名空間模式安裝 Config Connector
本頁面說明如何在命名空間模式下安裝 Config Connector。
以命名空間模式安裝是 Config Connector 安裝作業的擴充功能。命名空間模式支援管理多個專案,每個專案都有自己的 Google Cloud 身分。
事前準備
在將 Config Connector 設定為在命名空間模式下執行前,請確認您已安裝 Config Connector。
設定 Config Connector 以命名空間模式執行
如要啟用命名空間模式,請完成下列步驟:
將以下 YAML 資訊清單複製到名為
configconnector.yaml
的檔案:apiVersion: core.cnrm.cloud.google.com/v1beta1 kind: ConfigConnector metadata: # the name is restricted to ensure that there is only ConfigConnector resource installed in your cluster name: configconnector.core.cnrm.cloud.google.com spec: mode: namespaced stateIntoSpec: Absent
使用
kubectl apply
將設定套用至叢集:kubectl apply -f configconnector.yaml
設定 Config Connector 以管理命名空間中的資源
在後續章節中,您安裝 Config Connector 的 Google Cloud 專案稱為「主專案」或 HOST_PROJECT_ID。您管理資源的其他專案稱為「受管專案」或 MANAGED_PROJECT_ID。如果您只想使用 Config Connector 在叢集所在專案中建立 Google Cloud 資源,這兩個專案可以是相同的專案。
建立命名空間
如果您已經有用來整理Google Cloud 資源的命名空間,可以略過這個步驟。
執行下列指令,使用 kubectl
建立新的命名空間:
kubectl create namespace NAMESPACE
將 NAMESPACE 替換為命名空間的名稱。
設定 IAM 政策以連線至 Google Cloud API
如要讓 Config Connector 使用 GKE 適用的工作負載身分聯盟管理您的 Google Cloud 資源,請建立 IAM 政策。
如要設定政策繫結,請建立 IAM 允許政策,參照在叢集中安裝 Config Connector 時自動建立的 Kubernetes 服務帳戶:
gcloud projects add-iam-policy-binding projects/PROJECT_ID \
--role=roles/iam.workloadIdentityUser \
--member=principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/cnrm-system/cnrm-controller-manager \
--condition=None
更改下列內容:
PROJECT_ID
:您的 Google Cloud 專案 ID。PROJECT_NUMBER
:您的數字 Google Cloud專案編號。NAMESPACE
:Config Connector 服務帳戶的 Kubernetes 命名空間名稱。如果您未明確建立命名空間,這個值會是default
。
建立 ConfigConnectorContext
如要建立 Google Cloud 資源,您需要在要使用的命名空間中新增 ConfigConnectorContext
物件,藉此設定 Config Connector 來監控命名空間。
如要建立 ConfigConnectorContext
,請完成下列步驟:
將以下 YAML 資訊清單複製到名為
configconnectorcontext.yaml
的檔案:apiVersion: core.cnrm.cloud.google.com/v1beta1 kind: ConfigConnectorContext metadata: # you can only have one ConfigConnectorContext per namespace name: configconnectorcontext.core.cnrm.cloud.google.com namespace: NAMESPACE spec: googleServiceAccount: "NAMESPACE_GSA@HOST_PROJECT_ID.iam.gserviceaccount.com" stateIntoSpec: Absent
更改下列內容:
- NAMESPACE 改為您的命名空間名稱。
- NAMESPACE_GSA 與已綁定至您的命名空間的 Google 服務帳戶名稱。
- 將 HOST_PROJECT_ID 改成主機專案的 ID。
使用
kubectl
將檔案套用至叢集:kubectl apply -f configconnectorcontext.yaml
執行下列指令,確認 Config Connector Operator 已使用
kubectl
為您的命名空間建立 Kubernetes 服務帳戶:kubectl get serviceaccount/cnrm-controller-manager-NAMESPACE -n cnrm-system
請將 NAMESPACE 替換成您的命名空間名稱。
執行下列指令,確認 Config Connector 控制器 Pod 是否已為您的命名空間搭配
kubectl
執行:kubectl wait -n cnrm-system \ --for=condition=Ready pod \ -l cnrm.cloud.google.com/component=cnrm-controller-manager \ -l cnrm.cloud.google.com/scoped-namespace=NAMESPACE
請將 NAMESPACE 替換成您的命名空間名稱。
如果 Config Connector 控制器正在執行,輸出內容會類似以下:
cnrm-controller-manager-abcdefghijk-0 condition met.
設定 Config Connector 不再管理命名空間中的資源
如要設定 Config Connector 不再管理命名空間,請移除命名空間中的所有 Config Connector 資源,並刪除命名空間中的 ConfigConnectorContext
。
移除命名空間中的 Config Connector 資源
如要完成 ConfigConnectorContext
的移除作業,請從命名空間中移除所有 Config Connector 資源。
如要探索命名空間中的所有 Config Connector 資源,請為每個 Config Connector 自訂資源定義列出所有資源。
kubectl get gcp -n NAMESPACE
請將 NAMESPACE 替換成您的命名空間名稱。
如要移除所有 Config Connector 資源,請針對上一個步驟輸出的每個資源,發出刪除指令。
kubectl delete -n NAMESPACE KIND NAME
更改下列內容:
NAMESPACE
:命名空間名稱KIND
:在先前步驟中發現的資源類型NAME
:上一個步驟中發現的資源名稱
移除 ConfigConnectorContext
如要設定 Config Connector 不再管理命名空間中的 Config Connector 資源,請刪除命名空間中的 ConfigConnectorContext
。
kubectl delete -n NAMESPACE ConfigConnectorContext configconnectorcontext.core.cnrm.cloud.google.com
請將 NAMESPACE 替換成您的命名空間名稱。
您必須先從命名空間中移除所有 Config Connector 資源,才能完成 ConfigConnectorContext
的刪除作業。
解除安裝 Config Connector
請勿使用下列步驟解除安裝 Config Controller 叢集。
使用 kubectl delete
移除 Config Connector CRD 和控制器元件:
kubectl delete ConfigConnectorContext --all -A –wait=false
kubectl delete ConfigConnector configconnector.core.cnrm.cloud.google.com \
--wait=true
如要解除安裝 Config Connector 運算子,請執行下列指令:
kubectl delete -f operator-system/configconnector-operator.yaml --wait=true
後續步驟
- 開始使用 Config Connector。
- 瞭解 Config Connector 的最佳做法。