本教學課程說明如何提升叢集和應用程式的安全性。假設您是平台管理員,貴機構使用 Cloud Service Mesh 管理線上商店的應用程式。這套工具可協助您監控及管理可靠的服務網格。您有責任確保網格和應用程式安全無虞。
您可以使用 Policy Controller 和 Config Sync,防止設定錯誤並自動驗證 Cloud Service Mesh 政策。Policy Controller 能對叢集強制執行完全程式化的政策。Policy Controller 也隨附預設的限制範本庫,可以與 Cloud Service Mesh 安全性套裝組合搭配使用,檢查您的網格安全漏洞機制與最佳做法是否符合法規。Config Sync 會持續使用一組核心 Kubernetes 宣告式設定檔來協調叢集狀態。同時使用 Policy Controller 和 Config Sync,即可持續對 Cloud Service Mesh 政策設定強制執行限制。
下圖概述本教學課程中,Cloud Service Mesh、Policy Controller 和 Config Sync 如何共同運作,管理及保護您在本教學課程中使用的Ingress 閘道和 Online Boutique 範例應用程式:
目標
- 建立 Google Kubernetes Engine (GKE) 叢集,並將叢集註冊至機群。
- 在叢集上安裝 Policy Controller、Config Sync 和 Cloud Service Mesh。
- 設定 Config Sync,同步處理多個存放區
- 按照最佳做法,使用 Config Sync 部署設定、應用程式和 Istio 資源。
- 使用 Config Sync 部署叢集設定、Online Boutique 範例應用程式和 Ingress 閘道。
- 善用 Policy Controller 的Cloud Service Mesh 政策套裝組合,強制執行下列安全性最佳做法:
- 確保網格中的所有工作負載都已自動注入 Sidecar。
- 加密網格中的所有流量。
- 確保網格中的所有工作負載都具有精細的存取控管機制。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
- GKE.
- GKE Enterprise. The billing for GKE Enterprise includes billing for the Cloud Service Mesh, Config Sync, and Policy Controller.
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
-
在 Google Cloud 控制台的專案選擇器頁面中,選取或建立 Google Cloud 專案。
選取或建立專案所需的角色
- 選取專案:選取專案時,不需要具備特定 IAM 角色,只要您已獲授角色,即可選取任何專案。
-
建立專案:如要建立專案,您需要「專案建立者」角色 (
roles/resourcemanager.projectCreator),其中包含resourcemanager.projects.create權限。瞭解如何授予角色。
準備環境
在本節中,您將準備環境,以便安裝 Cloud Service Mesh、Policy Controller 和 Config Sync:
-
在 Google Cloud 控制台中啟用 Cloud Shell。
升級至最新版 Google Cloud CLI:
gcloud components update如要儲存您在本教學課程中建立的檔案,請建立目錄:
mkdir ~/asm-acm-tutorial-dir為簡化本教學課程的其餘部分,請建立下列環境變數:
PROJECT_ID=PROJECT_ID gcloud config set project $PROJECT_ID CLUSTER=asm-acm-tutorial CLUSTER_ZONE=us-east4-a MEMBERSHIP=asm-acm-tutorial PROJECT_NUMBER=$(gcloud projects describe ${PROJECT_ID} --format='get(projectNumber)')將
PROJECT_ID替換為您要在本教學課程中使用的專案 ID。如果系統提示您為 Cloud Shell 提供授權,請點選「授權」完成作業。
啟用本教學課程所需的 API:
gcloud
gcloud services enable \ mesh.googleapis.com \ anthos.googleapis.comConfig Connector
本教學課程包含 Config Connector 資源。您可以使用這些資源完成與「
gcloud」分頁中相同的任務。如要使用這些資源,請安裝 Config Connector,並以最適合您環境的方式套用資源。使用下列
Services資訊清單:apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1 kind: Service metadata: annotations: cnrm.cloud.google.com/deletion-policy: "abandon" cnrm.cloud.google.com/disable-dependent-services: "false" name: mesh.googleapis.com spec: resourceID: mesh.googleapis.com projectRef: external: PROJECT_ID --- apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1 kind: Service metadata: annotations: cnrm.cloud.google.com/deletion-policy: "abandon" cnrm.cloud.google.com/disable-dependent-services: "false" name: anthos.googleapis.com spec: resourceID: anthos.googleapis.com projectRef: external: PROJECT_ID這項作業可能需要一分鐘以上才能完成。
設定 GKE 叢集
在本節中,您將建立 GKE 叢集,然後將其註冊至機群。「機群」是Google Cloud 按照邏輯整理叢集和其他資源的概念,可讓您使用及管理多叢集功能,並在不同系統中套用一致的政策。
您在本節中建立的叢集,就是安裝 Cloud Service Mesh、Policy Controller 和 Config Sync 的叢集。您也會在這個叢集中部署 Online Boutique 範例應用程式。
如要設定叢集,請完成下列步驟:
建立 GKE 叢集:
gcloud
gcloud container clusters create ${CLUSTER} \ --zone ${CLUSTER_ZONE} \ --machine-type=e2-standard-4 \ --num-nodes 4 \ --workload-pool ${PROJECT_ID}.svc.id.goog \ --labels mesh_id=proj-${PROJECT_NUMBER}Config Connector
使用下列
ContainerCluster和ContainerNodePool資訊清單:apiVersion: container.cnrm.cloud.google.com/v1beta1 kind: ContainerNodePool metadata: annotations: cnrm.cloud.google.com/project-id: PROJECT_ID name: asm-acm-tutorial spec: clusterRef: name: asm-acm-tutorial location: us-east4-a nodeConfig: machineType: e2-standard-4 nodeCount: 4 --- apiVersion: container.cnrm.cloud.google.com/v1beta1 kind: ContainerCluster metadata: annotations: cnrm.cloud.google.com/project-id: PROJECT_ID cnrm.cloud.google.com/remove-default-node-pool: "true" labels: mesh_id: proj-PROJECT_NUMBER name: asm-acm-tutorial spec: location: us-east4-a initialNodeCount: 1 workloadIdentityConfig: workloadPool: PROJECT_ID.svc.id.goog將
PROJECT_NUMBER替換為先前擷取的PROJECT_NUMBER環境變數值。這項作業可能需要五分鐘以上才能完成。
為確保 GKE 叢集建立成功,請說明其狀態:
gcloud container clusters list \ --zone ${CLUSTER_ZONE} \ --project ${PROJECT_ID}輸出結果會與下列內容相似:
NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS asm-acm-tutorial us-east4-a 1.23.12-gke.100 35.186.179.30 e2-standard-4 1.23.12-gke.100 3 RUNNING連線至 GKE 叢集:
gcloud container clusters get-credentials ${CLUSTER} \ --zone ${CLUSTER_ZONE} \ --project ${PROJECT_ID}將叢集註冊至機群:
gcloud
gcloud container fleet memberships register ${MEMBERSHIP} \ --project ${PROJECT_ID} \ --gke-cluster ${CLUSTER_ZONE}/${CLUSTER} \ --enable-workload-identity輸出結果會與下列內容相似:
kubeconfig entry generated for asm-acm-tutorial. Waiting for membership to be created...done. Created a new membership [projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial] for the cluster [asm-acm-tutorial] Generating the Connect Agent manifest... Deploying the Connect Agent on cluster [asm-acm-tutorial] in namespace [gke-connect]... Deployed the Connect Agent on cluster [asm-acm-tutorial] in namespace [gke-connect]. Finished registering the cluster [asm-acm-tutorial] with the Fleet.Config Connector
使用下列
GKEHubMembership資訊清單:apiVersion: gkehub.cnrm.cloud.google.com/v1beta1 kind: GKEHubMembership metadata: annotations: cnrm.cloud.google.com/project-id: PROJECT_ID name: asm-acm-tutorial spec: location: global authority: issuer: https://container.googleapis.com/v1/projects/PROJECT_ID/locations/us-east4-a/clusters/asm-acm-tutorial endpoint: gkeCluster: resourceRef: name: asm-acm-tutorial為確保 GKE 叢集註冊成功,請說明其狀態:
gcloud container fleet memberships list輸出結果會與下列內容相似:
NAME EXTERNAL_ID LOCATION asm-acm-tutorial 0e12258c-8831-4d81-b5c0-5e7099a468cc global
探索存放區
在下方的安裝部分,您會套用資訊清單 acm-config.yaml 檔案。這個資訊清單會將叢集設定為從範例存放區的 asm-acm-tutorial 資料夾同步。這個資料夾包含完成本教學課程其餘部分所需的所有設定檔。
為簡化本教學課程,您會使用 sed 指令更新 acm-config.yaml。透過 acm-config.yaml 檔案,Config Sync 會部署本教學課程每個步驟所需的資訊清單。更新單一檔案可讓您專注於保護叢集、網格和應用程式的概念和流程,不必重複操作檔案和重複執行 git 指令。
如要使用 Config Sync 的同步處理多個存放區功能,請使用下列資源:
root-sync做為RootSync存放區,包含叢集中的所有設定,包括RepoSyncs、Constraints、ClusterRole、RoleBindings,以及某些系統命名空間 (例如istio-system) 中包含的資源。ingress-gateway,做為第一個RepoSync,包含部署 Ingress 閘道所需的所有資源,並在本教學課程中逐步確保安全。online-boutique,做為第二個RepoSync,包含部署 Online Boutique 應用程式所需的所有資源,並在本教學課程中逐步確保這些資源的安全。
安裝 Policy Controller、Config Sync 和代管型 Cloud Service Mesh
建立及註冊叢集後,您可以在叢集上安裝 Config Sync、Policy Controller 和 Cloud Service Mesh,並將叢集設定為從預設 RootSync 的設定檔同步:
啟用
ConfigManagement運算子,管理 Config Sync 和 Policy Controller:gcloud
gcloud beta container fleet config-management enableConfig Connector
使用下列
GKEHubFeature資訊清單:apiVersion: gkehub.cnrm.cloud.google.com/v1beta1 kind: GKEHubFeature metadata: name: configmanagement spec: projectRef: external: PROJECT_ID location: global resourceID: configmanagement在機群中啟用 Cloud Service Mesh。
gcloud
gcloud container fleet mesh enableConfig Connector
使用下列
GKEHubFeature資訊清單:apiVersion: gkehub.cnrm.cloud.google.com/v1beta1 kind: GKEHubFeature metadata: name: servicemesh spec: projectRef: external: PROJECT_ID location: global resourceID: servicemesh啟用 Cloud Service Mesh 自動管理功能,讓 Google 採用代管型 Cloud Service Mesh 的建議設定:
gcloud
gcloud container fleet mesh update \ --management automatic \ --memberships ${MEMBERSHIP}Config Connector
使用下列
GKEHubFeatureMembership資訊清單:apiVersion: gkehub.cnrm.cloud.google.com/v1beta1 kind: GKEHubFeatureMembership metadata: name: servicemesh-membership spec: projectRef: external: PROJECT_ID location: global membershipRef: name: asm-acm-tutorial featureRef: name: servicemesh mesh: management: MANAGEMENT_AUTOMATIC啟用 Config Sync 和 Policy Controller:
gcloud
將下列資訊清單儲存為
~/asm-acm-tutorial-dir目錄中的acm-config.yaml:applySpecVersion: 1 spec: configSync: enabled: true policyDir: asm-acm-tutorial/root-sync/init secretType: none sourceFormat: unstructured syncRepo: https://github.com/GoogleCloudPlatform/anthos-config-management-samples syncBranch: main policyController: enabled: true referentialRulesEnabled: true templateLibraryInstalled: true如要進一步瞭解 Google Cloud CLI 設定欄位,請參閱「gcloud apply spec fields」。
套用檔案:
gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yamlConfig Connector
使用下列
GKEHubFeatureMembership資訊清單:apiVersion: gkehub.cnrm.cloud.google.com/v1beta1 kind: GKEHubFeatureMembership metadata: name: configmanagement-membership spec: projectRef: external: PROJECT_ID location: global membershipRef: name: asm-acm-tutorial featureRef: name: configmanagement configmanagement: configSync: sourceFormat: unstructured git: policyDir: asm-acm-tutorial/root-sync/init secretType: none syncBranch: main syncRepo: https://github.com/GoogleCloudPlatform/anthos-config-management-samples policyController: enabled: true referentialRulesEnabled: true templateLibraryInstalled: true叢集已安裝 Policy Controller 和 Config Sync。接著,Config Sync 會開始將預設
RootSync的所有設定同步至叢集。這些設定檔會安裝及設定下列主要元件:設定 Online Boutique 應用程式和 Ingress 閘道的
RepoSync物件會同步處理:由於
RepoSync調解器需要額外權限才能建立 Istio 資源,因此系統也會將ClusterRole和兩個RoleBinding物件套用至叢集,以授予這些權限:
為確保 Policy Controller 和 Config Sync 安裝成功,請檢查狀態:
gcloud beta container fleet config-management status輸出結果會與下列內容相似:
Name: asm-acm-tutorial Status: SYNCED Last_Synced_Token: 4b3384d Sync_Branch: main Last_Synced_Time: 2022-05-04T21:32:58Z Policy_Controller: INSTALLED如果
Status或Policy_Controller列中顯示PENDING或NOT_INSTALLED,請稍候幾分鐘,然後再次執行gcloud beta container fleet config-management status。為確保順利安裝 Cloud Service Mesh,請說明其狀態:
gcloud container fleet mesh describe輸出結果會與下列內容相似:
createTime: '2022-09-13T23:12:56.477042921Z' membershipSpecs: projects/PROJECT_NUMBER/locations/global/memberships/asm-acm-tutorial: mesh: management: MANAGEMENT_AUTOMATIC membershipStates: projects/PROJECT_NUMBER/locations/global/memberships/asm-acm-tutorial: servicemesh: controlPlaneManagement: details: - code: REVISION_READY details: 'Ready: asm-managed' state: ACTIVE dataPlaneManagement: details: - code: OK details: Service is running. state: ACTIVE state: code: OK description: |- Revision(s) ready for use: asm-managed. All Canonical Services have been reconciled successfully. updateTime: '2022-09-14T00:19:10.571552206Z' name: projects/PROJECT_ID/locations/global/features/servicemesh resourceState: state: ACTIVE spec: {} state: state: {} updateTime: '2022-09-14T00:19:14.135113118Z'如果看到
state.code: ERROR而不是state.code: OK,請稍候幾分鐘,然後再次執行gcloud container fleet mesh describe。繼續進行本教學課程之前,請先確認servicemesh.controlPlaneManagement.details.code欄位是否具有REVISION_READY值。
部署 Ingress 閘道和範例應用程式
在本節中,您將部署 Online Boutique 範例應用程式和輸入閘道,以管理輸入流量。
部署 Online Boutique 範例應用程式和 Ingress 閘道。
下列指令使用
sed更新acm-config.yaml資訊清單,讓 Config Sync 部署您需要的資源,以部署 Ingress 閘道和範例應用程式。sed -i "s,root-sync/init,root-sync/deployments,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml請注意,這個步驟可能需要幾分鐘才能完成。
查看
RootSync和兩個RepoSyncs的 Config Sync 狀態:gcloud alpha anthos config sync repo describe輸出內容類似如下:
getting 3 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "95a30c052566357afb9db3d7f6153d9c0f219c03", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/deployments@main", "status": "SYNCED" }, { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "95a30c052566357afb9db3d7f6153d9c0f219c03", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/ingress-gateway/deployments@main", "status": "SYNCED" }, { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "95a30c052566357afb9db3d7f6153d9c0f219c03", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/online-boutique/deployments@main", "status": "SYNCED" } ]如果看到
status: RECONCILING而不是status: SYNCED,請稍候幾分鐘,然後再次執行gcloud alpha anthos config sync repo describe。如要只查看一個存放區的資訊,可以使用
--sync-name和--sync-namespace旗標。如要詳細查看受管理資源,請新增--managed-resources旗標。 詳情請參閱「查看多個叢集的 Config Sync 狀態」。等待系統佈建 Ingress Gateway 的公開 IP 位址:
until kubectl -n asm-ingress get svc asm-ingressgateway -o jsonpath='{.status.loadBalancer}' | grep "ingress"; do : ; done取得 Ingress 閘道的公開 IP 位址:
EXTERNAL_IP=$(kubectl get svc asm-ingressgateway -n asm-ingress -o jsonpath="{.status.loadBalancer.ingress[*].ip}")在瀏覽器中前往 IP 位址,確認 Online Boutique 應用程式已成功部署:
echo http://${EXTERNAL_IP}
強制執行政策以保護網格
在下列各節中,您將利用 Policy Controller 建立限制,強制執行Cloud Service Mesh 政策套裝組合中的政策。
強制插入補充 Proxy
在本節中,您將強制執行政策,確保網格中的所有工作負載都已啟用自動 Sidecar 插入。
如要強制插入補充 Proxy,請套用限制。
下列指令會使用
sed更新acm-config.yaml資訊清單,讓 Config Sync 部署相關聯的資源。sed -i "s,root-sync/deployments,root-sync/enforce-sidecar-injection,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml上述指令會部署下列資源:
K8sRequiredLabelsConstraint,要求網格中的任何Namespace都必須包含特定的Cloud Service Mesh Sidecar Proxy 插入標籤:AsmSidecarInjectionConstraint,禁止網格中的任何Pod略過 Istio Proxy 補充植入功能:
查看
RootSync的 Config Sync 狀態:gcloud alpha anthos config sync repo describe \ --sync-name root-sync \ --sync-namespace config-management-system輸出內容類似如下:
getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/enforce-sidecar-injection@main", "status": "SYNCED" } ]如果看到
status: RECONCILING而不是status: SYNCED,請稍候幾分鐘,然後再次執行gcloud alpha anthos config sync repo describe。確認
Constraints是否已建立:kubectl get constraintsPolicy Controller 可能需要幾分鐘才能評估這些限制。如果
TOTAL-VIOLATIONS欄中沒有顯示值,請稍候並再次執行kubectl get constraints。輸出內容類似如下:
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS podsidecarinjectionannotation.constraints.gatekeeper.sh/pod-sidecar-injection-annotation deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8srequiredlabels.constraints.gatekeeper.sh/namespace-sidecar-injection-label deny 0由於我們已正確設定
Namespaces和Pods,因此有這些0TOTAL-VIOLATIONS的Constraints。如要查看這些
Constraints的運作情形,請嘗試在叢集中建立Namespace,但不要使用label或annotation:kubectl create namespace test輸出內容會與下列錯誤類似:
Error from server (Forbidden): admission webhook "validation.gatekeeper.sh" denied the request: [namespace-sidecar-injection-label] you must provide labels: {"istio-injection"}
強制執行流量加密
在本節中,您將強制執行政策,確保網格中的所有流量都經過加密。
如要強制執行流量加密,請套用限制。
下列指令會使用
sed更新acm-config.yaml資訊清單,讓 Config Sync 部署相關聯的資源。sed -i "s,root-sync/enforce-sidecar-injection,root-sync/enforce-strict-mtls,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml上述指令會部署下列資源:
AsmPeerAuthnMeshStrictMtlsConstraint,用於在PeerAuthentication命名空間中強制執行網格層級的 mTLS:istio-system參照限制:
gatekeeper-system命名空間中的Config。這項參照限制可讓AsmPeerAuthnMeshStrictMtlsConstraint參照定義中的另一個物件 (例如在istio-systemNamespace中搜尋任何PeerAuthentication):DestinationRuleTLSEnabledConstraint,禁止在 IstioDestinationRules中停用所有主機和主機子集的 TLS:AsmPeerAuthnStrictMtlsConstraint,強制所有PeerAuthentications不得覆寫 mTLS:STRICT
查看
RootSync的 Config Sync 狀態:gcloud alpha anthos config sync repo describe \ --sync-name root-sync \ --sync-namespace config-management-system輸出內容類似如下:
getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/enforce-strict-mtls@main", "status": "SYNCED" } ]如果看到
status: RECONCILING而不是status: SYNCED,請稍候幾分鐘,然後再次執行gcloud alpha anthos config sync repo describe。執行下列指令,進一步瞭解
PeerAuthentication違規情形:kubectl get asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/mesh-level-strict-mtls -ojsonpath='{.status.violations}' | jq輸出內容類似如下:
[ { "enforcementAction": "deny", "group": "constraints.gatekeeper.sh", "kind": "AsmPeerAuthnMeshStrictMtls", "message": "Root namespace <istio-system> does not have a strict mTLS PeerAuthentication", "name": "mesh-level-strict-mtls", "version": "v1beta1" } ]如要修正問題,請在
istio-system中部署PeerAuthentication。如要禁止網格中的所有服務接受純文字流量,請設定全網格PeerAuthentication政策,並將 mTLS 模式設為STRICT。部署政策後,控制層會自動佈建 TLS 憑證,讓工作負載彼此驗證。下列指令會使用
sed更新acm-config.yaml資訊清單,讓 Config Sync 部署相關聯的資源。sed -i "s,root-sync/enforce-strict-mtls,root-sync/fix-strict-mtls,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml上述指令會在
istio-system命名空間中部署下列STRICTmTLSPeerAuthentication。這會將 mTLSSTRICT套用至整個網格:查看
RootSync的 Config Sync 狀態:gcloud alpha anthos config sync repo describe \ --sync-name root-sync \ --sync-namespace config-management-system輸出內容類似如下:
getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/fix-strict-mtls@main", "status": "SYNCED" } ]如果看到
status: RECONCILING而不是status: SYNCED,請稍候幾分鐘,然後再次執行gcloud alpha anthos config sync repo describe。確認
Constraints是否已建立:kubectl get constraints請注意,政策控管器可能需要幾分鐘才能評估這些
Constraints。請稍候片刻,然後再次執行kubectl get constraints指令,直到每行在「TOTAL-VIOLATIONS」欄下方都有值。輸出內容類似如下:
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8srequiredlabels.constraints.gatekeeper.sh/namespace-sidecar-injection-label deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/mesh-level-strict-mtls deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS destinationruletlsenabled.constraints.gatekeeper.sh/destination-rule-tls-enabled deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmpeerauthnstrictmtls.constraints.gatekeeper.sh/peerauthentication-strict-mtls deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmsidecarinjection.constraints.gatekeeper.sh/pod-sidecar-injection-annotation deny 0
強制執行精細的存取控管機制
在本節中,您會強制執行政策,確保網格中的所有工作負載都具有精細的存取控管。
如要強制執行精細的存取控管,請套用限制。
下列指令會使用
sed更新acm-config.yaml資訊清單,讓 Config Sync 部署相關聯的資源。sed -i "s,root-sync/fix-strict-mtls,root-sync/enforce-authorization-policies,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml上述指令會部署下列資源:
AsmAuthzPolicyDefaultDenyConstraint,會在istio-system命名空間中強制執行網格層級的預設拒絕AuthorizationPolicy:AsmAuthzPolicyEnforceSourcePrincipalsConstraint,可強制規定任何AuthorizationPolicies都會定義精細的來源主體 (「*」除外)。只有asm-ingress命名空間中的 Ingress 閘道是這項規則的例外狀況,可接收來自使用者的流量,並將流量重新導向至 Online Boutique 的frontend應用程式。
查看
RootSync的 Config Sync 狀態:gcloud alpha anthos config sync repo describe \ --sync-name root-sync \ --sync-namespace config-management-system輸出內容類似如下:
getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/enforce-authorization-policies@main", "status": "SYNCED" } ]如果看到
status: RECONCILING而不是status: SYNCED,請稍候幾分鐘,然後再次執行gcloud alpha anthos config sync repo describe。執行下列指令,進一步瞭解相關違規事項:
kubectl get asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/default-deny-authorization-policies -ojsonpath='{.status.violations}' | jq輸出內容類似如下:
[ { "enforcementAction": "deny", "group": "constraints.gatekeeper.sh", "kind": "AsmAuthzPolicyDefaultDeny", "message": "Root namespace <istio-system> does not have a default deny AuthorizationPolicy", "name": "default-deny-authorization-policies", "version": "v1beta1" } ]如要修正問題,請在
istio-system命名空間中部署AuthorizationPolicy。下列指令會使用
sed更新acm-config.yaml資訊清單,讓 Config Sync 部署相關聯的資源。sed -i "s,root-sync/enforce-authorization-policies,root-sync/fix-default-deny-authorization-policy,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml上述指令會在
istio-system命名空間中部署下列「全部拒絕」AuthorizationPolicy:查看
RootSync的 Config Sync 狀態:gcloud alpha anthos config sync repo describe \ --sync-name root-sync \ --sync-namespace config-management-system輸出內容類似如下:
getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/fix-default-deny-authorization-policy@main", "status": "SYNCED" } ]如果看到
status: RECONCILING而不是status: SYNCED,請稍候幾分鐘,然後再次執行gcloud alpha anthos config sync repo describe。確認
Constraints是否已建立:kubectl get constraints請注意,政策控管器可能需要幾分鐘才能評估這些
Constraints。請稍候片刻,然後再次執行kubectl get constraints指令,直到每行在「TOTAL-VIOLATIONS」欄下方都有值。輸出內容類似如下:
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmsidecarinjection.constraints.gatekeeper.sh/pod-sidecar-injection-annotation deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8srequiredlabels.constraints.gatekeeper.sh/namespace-sidecar-injection-label deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/default-deny-authorization-policies deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/mesh-level-strict-mtls deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS destinationruletlsenabled.constraints.gatekeeper.sh/destination-rule-tls-enabled deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmpeerauthnstrictmtls.constraints.gatekeeper.sh/peerauthentication-strict-mtls deny 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmauthzpolicyenforcesourceprincipals.constraints.gatekeeper.sh/authz-source-principals-not-all deny 0在瀏覽器中前往 Online Boutique 應用程式:
echo http://${EXTERNAL_IP}您應該會收到
RBAC: access denied錯誤,確認預設拒絕AuthorizationPolicy已套用至整個網狀網路。如要修正這個問題,請在
asm-ingress和onlineboutique命名空間中部署更精細的AuthorizationPolicies。下列指令會使用
sed更新acm-config.yaml資訊清單,讓 Config Sync 部署相關聯的資源。sed -i "s,root-sync/fix-default-deny-authorization-policy,root-sync/deploy-authorization-policies,g" ~/asm-acm-tutorial-dir/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml上述指令會部署下列資源:
命名空間中的 :
AuthorizationPolicyasm-ingressonlineboutique命名空間中的每個應用程式都有一個AuthorizationPolicy,以下是cartservice應用程式的範例:每個應用程式在
asm-ingress和onlineboutique命名空間中都有一個ServiceAccount,因此每個應用程式在AuthorizationPolicies中評估為principal時,都有專屬身分。以下是cartservice應用程式的範例:
查看
RootSync和兩個RepoSyncs的 Config Sync 狀態:gcloud alpha anthos config sync repo describe輸出內容類似如下:
getting 3 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial [ { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/deploy-authorization-policies@main", "status": "SYNCED" }, { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/ingress-gateway/authorization-policies@main", "status": "SYNCED" }, { "clusters": [ "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial" ], "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884", "errors": [], "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/online-boutique/authorization-policies@main", "status": "SYNCED" } ]如果看到
status: RECONCILING而不是status: SYNCED,請稍候幾分鐘,然後再次執行gcloud alpha anthos config sync repo describe。如要只查看一個存放區的資訊,可以使用
--sync-name和--sync-namespace旗標。如要詳細查看受管理資源,可以新增--managed-resources旗標。詳情請參閱「查看多個叢集的 Config Sync 狀態」。透過瀏覽器再次前往 Online Boutique 應用程式:
echo http://${EXTERNAL_IP}稍候片刻後,網站應該就能正常運作。
查看 GKE Enterprise 安全性功能的狀態
您可以在 Google Cloud 控制台中查看 GKE Enterprise 安全性功能的狀態,包括驗證和授權政策。
前往 Google Cloud 控制台的「GKE Enterprise Security」頁面。
「政策摘要」會顯示應用程式安全防護的狀態,包括服務存取控管 (
AuthorizationPolicies) 和 mTLS。按一下「政策稽核」,即可查看叢集和兩個命名空間 (
asm-ingress和onlineboutique) 的工作負載政策狀態。「服務存取控管」和「雙向傳輸層安全標準狀態」資訊卡提供高階總覽。

「工作負載」清單會顯示每個工作負載的服務存取控管和雙向傳輸層安全標準狀態。

您現在已透過 Policy Controller 和 Config Sync 保護叢集和網格。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
刪除 Google Cloud 專案:
gcloud projects delete PROJECT_ID
刪除個別資源
刪除個別資源的步驟如下:
從機群取消註冊叢集:
gcloud
gcloud container fleet memberships unregister ${CLUSTER} \ --project=${PROJECT_ID} \ --gke-cluster=${CLUSTER_ZONE}/${CLUSTER}輸出結果會與下列內容相似:
kubeconfig entry generated for asm-acm-tutorial. Waiting for membership to be deleted...done. Deleting membership CR in the cluster...done. Deleting namespace [gke-connect] in the cluster...done.Config Connector
kubectl delete -f ~/asm-acm-tutorial-dir/fleet-membership.yaml刪除叢集:
gcloud
gcloud container clusters delete ${CLUSTER} \ --zone ${CLUSTER_ZONE}在系統提示時按下
y。這個指令可能需要五分鐘以上才能完成。輸出結果會與下列內容相似:
Deleting cluster asm-acm-tutorial...done. Deleted [https://container.googleapis.com/v1/projects/PROJECT_ID/zones/us-east4-a/clusters/asm-acm-tutorial].Config Connector
kubectl delete -f ~/asm-acm-tutorial-dir/container-cluster.yaml刪除您建立的檔案:
rm -r ~/asm-acm-tutorial-dir
後續步驟
- 瞭解 Cloud Service Mesh 安全性最佳做法。
- 進一步瞭解 Cloud Service Mesh 政策套裝組合。
- 探索 Policy Controller 的限制範本庫。
- 查看 Google Cloud 的參考架構、圖表和最佳做法。 歡迎瀏覽我們的 Cloud Architecture Center。