瞭解如何建立預先安裝 Config Connector、Policy Controller 和 Config Sync 的 Config Controller 執行個體。接下來,請完成下列工作,瞭解如何運用執行個體:
- 使用 Config Connector 建立及管理 Google Cloud 資源。
- 建立 Policy Controller 限制,強制執行政策並在部署前偵測政策違規行為。
- 設定 Config Sync,從包含 Google Cloud 資源的範例存放區同步,藉此設定 GitOps。
事前準備
- 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the KRM, GKE, GKE Enterprise, Resource Manager, and Service Usage APIs:
gcloud services enable krmapihosting.googleapis.com
container.googleapis.com anthos.googleapis.com cloudresourcemanager.googleapis.com serviceusage.googleapis.com -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the KRM, GKE, GKE Enterprise, Resource Manager, and Service Usage APIs:
gcloud services enable krmapihosting.googleapis.com
container.googleapis.com anthos.googleapis.com cloudresourcemanager.googleapis.com serviceusage.googleapis.com 在終端機中,建立 Autopilot Config Controller 執行個體:
gcloud anthos config controller create cc-example \ --location=us-central1 \ --full-management
這項作業最多需要 15 分鐘才能完成。
輸出內容如下:
Created instance [cc-example]. Fetching cluster endpoint and auth data. kubeconfig entry generated for krmapihost-cc-example.
查看 Config Controller 執行個體清單,確認執行個體是否已建立:
gcloud anthos config controller list --location=us-central1
輸出內容如下:
NAME LOCATION STATE cc-example us-central1 RUNNING
為服務帳戶電子郵件設定環境變數:
export SA_EMAIL="$(kubectl get ConfigConnectorContext -n config-control \ -o jsonpath='{.items[0].spec.googleServiceAccount}' 2> /dev/null)"
建立政策繫結:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member "serviceAccount:${SA_EMAIL}" \ --role "roles/owner" \ --project PROJECT_ID
將
PROJECT_ID
替換為您的專案 ID。輸出結果會與下列內容相似:
Updated IAM policy for project [PROJECT_ID]. auditConfigs: - auditLogConfigs: - logType: ADMIN_READ - logType: DATA_READ - logType: DATA_WRITE service: gkehub.googleapis.com # Remainder of output omitted
使用 Config Connector 啟用 Pub/Sub API:
使用慣用的文字編輯器建立名為
enable-pubsub.yaml
的檔案,然後將下列 YAML 複製到檔案中:# enable-pubsub.yaml apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1 kind: Service metadata: name: pubsub.googleapis.com namespace: config-control spec: projectRef: external: projects/PROJECT_ID
將
PROJECT_ID
替換為您的專案 ID。如要啟用 Pub/Sub API,請將資訊清單套用至叢集:
kubectl apply -f enable-pubsub.yaml
啟用這項 API 可能需要幾分鐘的時間。
使用 Config Connector 建立 Pub/Sub 主題:
建立名為
pubsub-topic.yaml
的檔案,然後將下列 YAML 複製到檔案中:# pubsub-topic.yaml apiVersion: pubsub.cnrm.cloud.google.com/v1beta1 kind: PubSubTopic metadata: annotations: cnrm.cloud.google.com/project-id: PROJECT_ID labels: label-one: "value-one" name: example-topic namespace: config-control
建立 Pub/Sub 主題:
kubectl apply -f pubsub-topic.yaml
查看 Pub/Sub 主題清單,確認 Config Controller 已在 Google Cloud中建立資源:
gcloud pubsub topics list
輸出結果會與下列內容相似:
--- name: projects/PROJECT_ID/topics/start-instance-event --- labels: label-one: value-one managed-by-cnrm: 'true' name: projects/PROJECT_ID/topics/example-topic
建立名為
bucket-constraint.yaml
的檔案,然後將下列 YAML 複製到該檔案中:# bucket-constraint.yaml apiVersion: constraints.gatekeeper.sh/v1beta1 kind: GCPStorageLocationConstraintV1 metadata: name: storage-only-in-us-central1 spec: match: kinds: - apiGroups: - storage.cnrm.cloud.google.com kinds: - StorageBucket parameters: locations: - us-central1
建立限制:
kubectl apply -f bucket-constraint.yaml
輸出內容如下:
gcpstoragelocationconstraintv1.constraints.gatekeeper.sh/storage-only-in-us-central1 created`
如要證明限制條件正常運作,請嘗試使用 Config Connector 在
asia-southeast1
中建立 StorageBucket 資源:建立名為
asia-storage-bucket.yaml
的檔案,然後將下列 YAML 複製到檔案中:# asia-storage-bucket.yaml apiVersion: storage.cnrm.cloud.google.com/v1beta1 kind: StorageBucket metadata: name: bucket-in-disallowed-location namespace: config-control spec: location: asia-southeast1
嘗試建立 Cloud Storage bucket:
kubectl apply -f asia-storage-bucket.yaml
輸出內容如下:
Error from server (Forbidden): error when creating "STDIN": admission webhook "validation.gatekeeper.sh" denied the request: [storage-only-in-us-central1] Cloud Storage bucket <bucket-in-disallowed-location> uses a disallowed location <asia-southeast1>, allowed locations are ["us-central1"]
如要從 GitHub 同步處理,請設定 Cloud NAT。這是因為您的 Config Controller 執行個體是由私人 Google Kubernetes Engine (GKE) Enterprise 版叢集支援,而私人叢集節點沒有對外網際網路存取權:
建立 Cloud NAT 路由器。您需要這個路由器才能設定 NAT 閘道。
gcloud compute routers create cc-nat-router \ --network default \ --region us-central1
輸出結果會與下列內容相似:
Creating router [cc-nat-router]...done. NAME REGION NETWORK cc-nat-router us-central1 default
在您在上一個步驟中建立的路由器上設定 NAT 閘道:
gcloud compute routers nats create cc-nat-config \ --router-region us-central1 \ --router cc-nat-router \ --nat-all-subnet-ip-ranges \ --auto-allocate-nat-external-ips
輸出內容如下:
Creating NAT [cc-nat-config] in router [cc-nat-router]...done.
如要將 Config Controller 執行個體設定為從範例存放區同步處理,請建立名為
cc-rootsync.yaml
的檔案,然後將下列 YAML 複製到該檔案:# cc-rootsync.yaml apiVersion: configsync.gke.io/v1beta1 kind: RootSync metadata: name: root-sync namespace: config-management-system spec: sourceFormat: unstructured git: repo: https://github.com/GoogleCloudPlatform/anthos-config-management-samples branch: main dir: config-controller-quickstart auth: none
套用設定:
kubectl apply -f cc-rootsync.yaml
執行個體從存放區同步處理後,Config Sync 會建立 Pub/Sub 主題,並套用至 Config Controller 執行個體。
確認 Config Sync 是否將 Git 存放區同步至 Config Controller 執行個體:
nomos status --contexts gke_PROJECT_ID_us-central1_krmapihost-cc-example
輸出結果會與下列內容相似:
*gke_PROJECT_ID_us-central1_krmapihost-cc-example -------------------- <root>:root-sync https://github.com/GoogleCloudPlatform/anthos-config-management-samples/config-controller-quickstart@main SYNCED @ 2023-01-10 18:31:02 +0000 UTC 715b4295d3eac07b057cce2543275c1ee104cad8 Managed resources: NAMESPACE NAME STATUS SOURCEHASH config-control pubsubtopic.pubsub.cnrm.cloud.google.com/pubsub-topic-sample-sync Current 715b429 config-control service.serviceusage.cnrm.cloud.google.com/pubsub.googleapis.com Current 715b429
如果沒有看到這項輸出內容,請稍候幾分鐘後再試一次。
確認 Config Controller 是否已建立資源:
gcloud pubsub topics list
輸出結果會與下列內容相似:
name: projects/PROJECT_ID/topics/start-instance-event --- labels: managed-by-cnrm: 'true' name: projects/PROJECT_ID/topics/sample-topic --- labels: managed-by-cnrm: 'true' name: projects/PROJECT_ID/topics/pubsub-topic-sample-sync --- labels: label-one: value-one managed-by-cnrm: 'true' name: projects/PROJECT_ID/topics/example-topic
在這個輸出內容中,您可以看到在「建立 Config Controller 執行個體」一節中建立的 Pub/Sub,以及透過將執行個體同步至 GitHub 存放區所建立的 Pub/Sub。
刪除 Config Connector
PubSubTopic
資源:kubectl delete -f pubsub-topic.yaml
刪除 Policy Controller 限制:
kubectl delete -f bucket-constraint.yaml
刪除 NAT 路由器:
gcloud compute routers delete cc-nat-router \ --project=PROJECT_ID \ --region=us-central1
在系統提示時按下
y
。刪除 Config Sync 建立的 Pub/Sub 資源:
kubectl delete PubSubTopic pubsub-topic-sample-sync -n config-control
刪除 RootSync 資源:
kubectl delete rootsync root-sync -n config-management-system
使用您偏好的文字編輯器,刪除您建立的所有 YAML 檔案:
enable-pubsub.yaml
pubsub-topic.yaml
bucket-constraint.yaml
asia-storage-bucket.yaml
cc-rootsync.yaml
刪除 Config Controller 執行個體:
gcloud anthos config controller delete --location=us-central1 cc-example
在系統提示時按下
y
。- 瞭解如何設定 Config Controller。
- 進一步瞭解 Config Controller。
建立 Config Controller 執行個體
授予 Config Controller 必要權限
在本節中,您將授予 Config Controller 管理資源的權限: Google Cloud
使用 Config Controller 執行個體
以下各節將介紹如何使用 Config Controller 執行個體。
使用 Config Connector 建立 Google Cloud 資源
有了 Config Controller 例項,您就能運用 Config Connector,使用 Kubernetes 工具和 API 管理許多 Google Cloud 服務和資源。在本節中,您將使用 Config Controller 建立 PubSubTopic
資源。
如要使用 Config Connector 建立 Pub/Sub 主題,請完成下列步驟:
透過 Policy Controller 強制執行政策
透過 Config Controller 執行個體,您可以使用 Policy Controller 和 Policy Controller 限制。安裝 Policy Controller 時,Config Controller 會自動安裝限制範本程式庫。您可以使用這個程式庫中的範本,在 Config Controller 執行個體上強制執行各種常見的安全性和法規遵循控制項。
在本節中,您將使用 GCPStorageLocationConstraintV1
限制範本建立限制。這個範本可讓您限制可建立 Cloud Storage 值區的位置。使用這個範本建立的限制會將位置限制為 us-central1
。您可能會想使用這項限制,確保值區是在提供最佳價格和效能的區域中建立。
如要建立限制:
透過 Config Sync 設定 GitOps
Config Sync 是一項 GitOps 服務,可讓您將 Config Controller 執行個體同步至儲存在 Git 存放區、OCI 映像檔或 Helm 存放區中的設定、政策和Google Cloud 資源。由於 Config Sync 會持續協調 Config Controller 的狀態與來源中的設定,因此您可以確保執行個體具有一致的設定。
在本節中,您會將 Config Controller 執行個體同步至公開 GitHub 存放區。這個存放區包含另一個 PubSubTopic
資源。從這個存放區同步處理執行個體後,系統會自動建立資源並套用至執行個體。如果您想使用 GitOps 工作流程,可以選擇使用 Config Sync 建立資源 (而不是直接套用資源)。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程所用資源的費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID