本教學課程說明如何提升叢集和應用程式的安全狀態。假設您是平台管理員,貴機構使用 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。
完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。
事前準備
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify 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 叢集:
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
root-sync
做為RootSync
存放區,包含叢集中的所有設定,包括RepoSyncs
、Constraints
、ClusterRole
、RoleBindings
,以及某些系統命名空間 (例如istio-system
) 中包含的資源。ingress-gateway
,其中包含部署 Ingress 閘道所需的所有資源,並在整個教學課程中逐步確保安全。RepoSync
online-boutique
,做為第二個RepoSync
,包含部署 Online Boutique 應用程式所需的所有資源,並在本教學課程中逐步確保這些資源的安全。啟用
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
的所有設定同步至叢集。這些設定檔會安裝及設定下列主要元件:設定 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
值。部署 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 Gateway 的公開 IP 位址:
EXTERNAL_IP=$(kubectl get svc asm-ingressgateway -n asm-ingress -o jsonpath="{.status.loadBalancer.ingress[*].ip}")
在瀏覽器中前往 IP 位址,確認 Online Boutique 應用程式已成功部署:
echo http://${EXTERNAL_IP}
如要強制插入補充 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 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 constraints
Policy 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
,因此這些Constraints
有0
TOTAL-VIOLATIONS
。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
上述指令會部署下列資源:
AsmPeerAuthnMeshStrictMtls
�,可強制執行命名空間中的網格層級 mTLSPeerAuthentication
: �Constraint
istio-system
參照限制:
gatekeeper-system
命名空間中的Config
。這項參照限制可讓AsmPeerAuthnMeshStrictMtls
Constraint
參照定義中的另一個物件 (例如在istio-system
Namespace
中搜尋任何PeerAuthentication
):DestinationRuleTLSEnabled
Constraint
,禁止在 IstioDestinationRules
中停用所有主機和主機子集的 TLS:AsmPeerAuthnStrictMtls
Constraint
,強制所有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
命名空間中部署下列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
請注意,Policy Controller 可能需要幾分鐘的時間,才能評估這些
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
名稱空間中的 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
請注意,Policy Controller 可能需要幾分鐘的時間,才能評估這些
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
上述指令會部署下列資源:
AuthorizationPolicy
命名空間中的asm-ingress
:onlineboutique
命名空間中的每個應用程式都有一個AuthorizationPolicy
,以下是cartservice
應用程式的範例:每個應用程式在
asm-ingress
和onlineboutique
命名空間中都有一個ServiceAccount
,因此每個應用程式在AuthorizationPolicies
中評估為principal
時,都有專屬身分。ServiceAccount
以下是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}
稍候片刻後,網站應該就能恢復正常運作。
前往 Google Cloud 控制台的「GKE Enterprise Security」頁面。
「政策摘要」會顯示應用程式安全防護狀態,包括服務存取權控管 (
AuthorizationPolicies
) 和 mTLS。按一下「政策稽核」,即可查看叢集和兩個命名空間 (
asm-ingress
和onlineboutique
) 的工作負載政策狀態。「服務存取權控管」和「雙向傳輸層安全標準狀態」資訊卡提供高階總覽。
「工作負載」清單會顯示每個工作負載的服務存取權控管和雙向傳輸層安全標準狀態。
設定 GKE 叢集
在本節中,您將建立 GKE 叢集,然後將其註冊至機群。「機群」是Google Cloud 按照邏輯整理叢集和其他資源的概念,可讓您使用及管理多叢集功能,並在不同系統中套用一致的政策。
您在本節中建立的叢集,就是安裝 Cloud Service Mesh、Policy Controller 和 Config Sync 的叢集。您也會在這個叢集部署 Online Boutique 範例應用程式。
如要設定叢集,請完成下列步驟:
探索存放區
在下方的安裝部分中,您會套用資訊清單 acm-config.yaml
檔案。這個資訊清單會將叢集設定為從範例存放區的 asm-acm-tutorial
資料夾同步。這個資料夾包含完成本教學課程其餘部分所需的所有設定檔。
為簡化本教學課程,您會使用 sed
指令更新 acm-config.yaml
。有了 acm-config.yaml
檔案,Config Sync 就能部署本教學課程每個步驟所需的資訊清單。更新單一檔案可協助您專注於保護叢集、網格和應用程式的概念和流程,不必重複操作檔案和執行 git
指令。
如要使用 Config Sync 的同步處理多個存放區功能,請使用下列資源:
安裝 Policy Controller、Config Sync 和代管型 Cloud Service Mesh
建立及註冊叢集後,您可以在叢集上安裝 Config Sync、Policy Controller 和 Cloud Service Mesh,並將叢集設定為從預設 RootSync
的設定檔同步:
部署 Ingress 閘道和範例應用程式
在本節中,您將部署 Online Boutique 範例應用程式和輸入閘道,以管理輸入流量。
強制執行政策,確保網格安全
在下列各節中,您會利用 Policy Controller 建立限制,強制執行Cloud Service Mesh 政策套裝組合中的政策。
強制插入補充 Proxy
在本節中,您將強制執行政策,確保網格中的所有工作負載都已啟用自動 Sidecar 插入。
強制執行流量加密
在本節中,您將強制執行政策,確保網格中的所有流量都經過加密。
強制執行精細的存取權控管
在本節中,您會強制執行政策,確保網格中的所有工作負載都具有精細的存取權控管機制。
查看 GKE Enterprise 安全性功能的狀態
您可以在 Google Cloud 控制台中查看 GKE Enterprise 安全性功能的狀態,包括驗證和授權政策。
您現在已透過 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 的參考架構、圖表和最佳做法。 歡迎瀏覽我們的雲端架構中心。