本教學課程將說明如何改善叢集和應用程式的安全性。假設您是平台管理員,貴機構使用 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 如何在本教學課程中合作,管理及保護入口網關和您在本教學課程中使用的 Online Boutique 範例應用程式:
目標
- 建立 Google Kubernetes Engine (GKE) 叢集,並將叢集註冊至機群。
- 在叢集上安裝 Policy Controller、Config Sync 和 Cloud Service Mesh。
- 設定 Config Sync 同步處理多個存放區
- 運用最佳做法,透過 Config Sync 部署設定、應用程式和 Istio 資源。
- 使用 Config Sync 部署叢集設定、Online Boutique 範例應用程式和入口閘道。
- 利用 Policy Controller 的 Cloud Service Mesh 政策套件,強制執行下列安全性最佳做法:
- 請確認網格中的所有工作負載都已自動注入附屬程式。
- 加密網格中的所有流量。
- 確保網格中的所有工作負載都具有精細的存取權控制。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
- GKE.
- GKE Enterprise. The billing for GKE Enterprise includes billing for the Cloud Service Mesh, Config Sync, and Policy Controller.
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。
事前準備
-
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.
-
Make sure that billing is enabled for your Google Cloud project.
準備環境
在本節中,您將準備環境,以便安裝 Cloud Service Mesh、Policy Controller 和 Config Sync:
-
In the Google Cloud console, activate 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.com
Config 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 就會部署本教學課程各個步驟所需的資訊清單。更新單一檔案可讓您專注於叢集、Mesh 和應用程式的安全性概念和流程,不必反覆操作檔案和執行 git
指令。
如要利用 Config Sync 的同步多個存放區功能,請使用下列資源:
root-sync
是RootSync
存放區,其中包含叢集中的所有設定,包括RepoSyncs
、Constraints
、ClusterRole
、RoleBindings
,以及istio-system
等某些系統命名空間中的資源。ingress-gateway
是第一個RepoSync
,包含部署入口網關所需的所有資源,並在本教學課程中逐步強化安全性。online-boutique
是第二個RepoSync
,包含部署線上精品店應用程式所需的所有資源,並在本教學課程中逐步強化應用程式的安全性。
安裝 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 enable
Config 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 enable
Config 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 套用規格欄位」一文。
套用檔案:
gcloud beta container fleet config-management apply \ --membership ${MEMBERSHIP} \ --config ~/asm-acm-tutorial-dir/acm-config.yaml
Config 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
的所有設定同步至叢集。這些設定會安裝及設定下列重要元件:系統會同步處理用於設定線上精品店應用程式和入口網站的
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
值。
部署入口網站閘道和範例應用程式
在本節中,您將部署 Online Boutique 範例應用程式和輸入閘道,以便管理輸入流量。
部署 Online Boutique 範例應用程式和入口網關。
下列指令會使用
sed
更新acm-config.yaml
資訊清單,讓 Config Sync 部署部署入口網站閘道和範例應用程式所需的資源。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 位址,確認是否已成功部署線上精品店應用程式:
echo http://${EXTERNAL_IP}
強制執行政策以保護網格
在後續章節中,您將利用Policy Controller 建立限制,藉此強制執行 Cloud Service Mesh 政策套件中的政策。
強制執行補充 Proxy 的插入作業
在本節中,您將套用政策,確保網格中的所有工作負載都已啟用自動側邊車注入。
如要強制執行補充 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
上述指令會部署下列資源:
K8sRequiredLabels
Constraint
,要求網格中的任何Namespace
都包含特定的 Cloud Service Mesh 補充 Proxy 注入標籤:AsmSidecarInjection
Constraint
,可禁止網格中的任何Pod
略過 Istio 代理程式補充作業:
查看
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 constraints
政策控制器可能需要幾分鐘才能評估這些限制條件。如果您在
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
,因此這些Constraints
會有0
TOTAL-VIOLATIONS
。如要查看這些
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
上述指令會部署下列資源:
在
istio-system
命名空間中強制執行網格層級 mTLSPeerAuthentication
的AsmPeerAuthnMeshStrictMtls
Constraint
:gatekeeper-system
命名空間中的參照限制Config
。這項參照限制可讓AsmPeerAuthnMeshStrictMtls
Constraint
在其定義中參照其他物件 (例如搜尋istio-system
Namespace
中的任何PeerAuthentication
):DestinationRuleTLSEnabled
Constraint
,可禁止為 IstioDestinationRules
中的所有主機和主機子集停用 TLS:AsmPeerAuthnStrictMtls
Constraint
,可強制執行所有PeerAuthentications
皆無法覆寫STRICT
mTLS:
查看
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
命名空間中部署下列STRICT
mTLSPeerAuthentication
。這會將 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
上述指令會部署下列資源:
AsmAuthzPolicyDefaultDeny
Constraint
,會在istio-system
命名空間中強制執行網格層級預設拒絕AuthorizationPolicy
:AsmAuthzPolicyEnforceSourcePrincipals
Constraint
,強制任何AuthorizationPolicies
都定義精細的來源主體 (除了「*」)。只有asm-ingress
命名空間中的輸入閘道例外,可接收來自使用者的流量,並將流量重新導向至線上精品店的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
命名空間中部署以下的 deny-allAuthorizationPolicy
:查看
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
上述指令會部署下列資源:
asm-ingress
命名空間中的AuthorizationPolicy
:onlineboutique
命名空間中每個應用程式都有一個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
) 的工作負載政策狀態。「服務存取權控管」和「雙向傳輸層安全標準狀態」資訊卡可提供概略的總覽。
「工作負載」清單會顯示每個工作負載的服務存取權控管和 mTLS 狀態。
您現在已透過 Policy Controller 和 Config Sync 保護叢集和網格。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。
刪除專案
Delete a Google Cloud project:
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.
設定連接器
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].
設定連接器
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 的參考架構、圖表和最佳做法。歡迎瀏覽我們的雲端架構中心。