本教程介绍如何改进集群和应用的安全状况。假设您是平台管理员,您的组织正在使用 Anthos Service Mesh 管理其网店的应用,Anthos Service Mesh 是一套工具,可帮助您监控和管理可靠的服务网格。您需要负责确保网格和应用的安全。
您可以使用 Anthos Config Management 的 Policy Controller 和 Config Sync 防止配置错误并自动验证 Anthos Service Mesh 政策。Policy Controller 支持为您的集群强制执行完全可编程的政策。Policy Controller 还附带一个默认的限制条件模板库,您可以将该库与 Anthos Service Mesh 安全软件包搭配使用,以审核网格安全漏洞的合规性并了解是否符合最佳做法。 Config Sync 使用一组集中式 Kubernetes 声明式配置文件来持续协调集群的状态。通过搭配使用 Policy Controller 和 Config Sync,您可以持续对 Anthos Service Mesh 政策配置实施限制条件。
下图简要展示了本教程中的 Anthos Service Mesh、Policy Controller 和 Config Sync 如何协同工作以管理和保护您在本教程中使用的 Online Boutique 示例应用:
目标
- 创建 Google Kubernetes Engine (GKE) 集群并向舰队注册集群。
- 在集群上安装 Policy Controller、Config Sync 和 Anthos Service Mesh。
- 部署 Online Boutique 示例应用和入站流量网关。
- 利用 Anthos Service Mesh 政策软件包来强制执行以下最佳做法:
- 确保网格中的所有工作负载都具有自动 Sidecar 注入。
- 加密网格中的所有流量。
- 保证网格中的所有工作负载都具有精细的访问权限控制。
费用
本教程使用 Google Cloud 的以下收费组件:
您可使用价格计算器根据您的预计使用情况来估算费用。
完成本教程后,您可以删除所创建的资源以避免继续计费。如需了解详情,请参阅清理。
准备工作
-
在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目。
-
确保您的 Cloud 项目已启用结算功能。了解如何检查项目是否已启用结算功能。
准备环境
在本部分中,您将准备环境,以便安装 Anthos Service Mesh、Policy Controller 和 Config Sync:
- 打开 Cloud Shell 会话。如需打开此会话,请从本页面的右上角点击terminal 激活 Cloud Shell,然后点击确认。页面下方的框架内会打开一个 Cloud Shell 会话。在该 Cloud Shell 会话中完成以下命令。
升级到最新版本的 Google Cloud CLI:
gcloud components update
要存储您在本教程中创建的文件,请使用环境变量创建目录:
WORK_DIR=~/asm-acm-tutorial-dir mkdir $WORK_DIR
如需简化本教程的其余部分,请创建以下环境变量:
PROJECT_ID=PROJECT_ID gcloud config set project $PROJECT_ID CLUSTER=asm-acm-tutorial CLUSTER_ZONE=us-east4-a PROJECT_NUMBER=$(gcloud projects describe ${PROJECT_ID} --format='get(projectNumber)')
将
PROJECT_ID
替换为您要用于本教程的项目 ID。如果系统要求您授权 Cloud Shell,请点击授权以完成操作。
启用本教程所需的 API:
gcloud services enable \ container.googleapis.com \ gkehub.googleapis.com \ mesh.googleapis.com \ anthos.googleapis.com
此操作可能需要超过一分钟才能完成。
设置 GKE 集群
在本部分中,您将创建一个 GKE 集群,然后将其注册到舰队。Environ 是 Google Cloud 的概念,用于以逻辑方式组织集群和其他资源,让您可以使用和管理多集群功能,并在您的所有系统中应用一致的政策。
您在本部分创建的集群是您在上面安装 Anthos Service Mesh、Policy Controller 和 Config Sync 的集群。它还是您部署 Online Boutique 示例应用所在的集群。
如需设置您的集群,请完成以下步骤:
创建 GKE 集群:
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}
此操作可能需要五分钟以上才能完成。
输出内容类似如下:
kubeconfig entry generated for asm-acm-tutorial. NAME: asm-acm-tutorial LOCATION: us-east4-a MASTER_VERSION: 1.21.10-gke.2000 MASTER_IP: 34.85.159.120 MACHINE_TYPE: e2-standard-4 NODE_VERSION: 1.21.10-gke.2000 NUM_NODES: 4 STATUS: RUNNING
向舰队注册集群:
gcloud container fleet memberships register ${CLUSTER} \ --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.
探索代码库
在下面的安装部分中,您将应用 Anthos Config Management 清单 acm-config.yaml
。此清单将集群配置为从 Anthos Config Management 示例代码的 asm-acm-tutorial
文件夹同步。此文件夹包含完成本教程其余部分所需的所有配置文件。
为了简化您在本教程中的学习体验,请使用 sed
命令更新 acm-config.yaml
文件,以部署每个步骤所需的清单。通过更新这个文件,您可以专注于概念以及保护集群、网格和应用的流程,而无需重复操作文件和运行 git add|commit|push
命令。
应用 acm-config.yaml
清单之前,了解代码库的整体结构和内容会很有帮助。
为了利用 Config Sync 同步到多个代码库的功能,asc-acm-tutorial
代码库包含了两个顶级文件夹:root-sync
和 online-boutique
。
root-sync
文件夹
root-sync
文件夹是根代码库。此代码库分为多个子文件夹,每个子文件夹包含本教程不同部分的资源和政策。
有两个文件夹包含用于设置 Online Boutique 示例应用和 Ingress Gateway 的资源:
init
:用于为 Online Boutique 应用设置RepoSync
的资源。deployments
:该资源用于部署 Ingress Gateway 和 Online Boutique 命名空间和应用。
有三个文件夹包含用于部署不同 Anthos Service Mesh 政策的资源:
enforce-sidecar-injection
:该资源用于部署政策,以对Namespace
和Pod
强制执行 Sidecar 注入。enforce-strict-mtls
:该资源用于部署政策,以对整个网格和任何PeerAuthentication
强制执行STRICT
mTLS。enforce-authorization-policies
:该资源用于部署政策,以对整个网格强制执行默认deny
AuthorizationPolicy
,以及对任何AuthorizationPolicies
强制执行精细的来源主体。
其余三个文件夹包含的资源可用于部署解决 Anthos Service Mesh 政策违规问题的资源:
fix-strict-mtls
:该资源用于在istio-system
命名空间中部署默认STRICT
mTLSPeerAuthentication
。fix-default-deny-authorization-policy
:该资源用于在istio-system
命名空间中部署默认deny
AuthorizationPolicy
。deploy-authorization-policies
:该资源用于部署精细的AuthorizationPolicy
资源,以使 Online Boutique 示例应用正常工作。
online-boutique
文件夹
online-boutique
文件夹是一个命名空间代码库。此代码库包含使用 Kustomize 部署 Online Boutique 示例应用所需的资源。
安装 Policy Controller、Config Sync 和代管式 Anthos Service Mesh
现在您已创建并注册集群,接下来可以在集群上安装 Config Sync、Policy Controller 和 Anthos Service Mesh,并将集群配置为从 asc-acm-tutorial
文件夹中的配置同步:
在舰队中启用 Anthos Config Management:
gcloud beta container fleet config-management enable
在舰队中启用 Anthos Service Mesh。
gcloud container fleet mesh enable
启用 Anthos Service Mesh 自动控制平面管理功能,让 Google 应用推荐的代管式 Anthos Service Mesh 配置:
gcloud container fleet mesh update \ --control-plane automatic \ --memberships ${CLUSTER}
如需安装和配置 Config Sync 和 Policy Controller,请创建以下 Anthos Config Management 清单:
cat <<EOF > $WORK_DIR/acm-config.yaml applySpecVersion: 1 spec: policyController: enabled: true templateLibraryInstalled: true referentialRulesEnabled: true configSync: enabled: true sourceFormat: unstructured syncRepo: https://github.com/GoogleCloudPlatform/anthos-config-management-samples syncBranch: main secretType: none policyDir: asm-acm-tutorial/root-sync/init EOF
如需详细了解 Anthos Config Management 配置字段,请参阅 gcloud apply spec 字段。
应用此文件:
gcloud beta container fleet config-management apply \ --membership ${CLUSTER} \ --config $WORK_DIR/acm-config.yaml
应用此文件后,Policy Controller 和 Config Sync 会安装到您的集群上。接下来,Config Sync 会开始将
asm-acm-tutorial
文件夹中的所有配置同步到您的集群。这些配置会安装并配置以下关键组件:用于配置 Online Boutique 应用的
RepoSync
对象已同步:由于
RepoSync
协调器需要其他权限来创建 Istio 资源,因此该代码库还包含集群角色和集群角色绑定以授予这些权限。这是自动应用于集群的集群角色清单:
要确保成功安装 Policy Controller 和 Config Sync,请查看 Anthos Config Management 的状态:
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 Hierarchy_Controller: PENDING
如果您在
Status
或Policy_Controller
行中看到PENDING
或NOT_INSTALLED
,请等待几分钟,然后再次运行gcloud beta container fleet config-management status
。要确保 Anthos Service Mesh 成功安装,请描述其状态:
gcloud container fleet mesh describe
输出内容类似如下:
createTime: '2022-05-05T23:33:44.041608250Z' membershipSpecs: projects/841604900168/locations/global/memberships/asm-acm-tutorial: mesh: controlPlane: AUTOMATIC membershipStates: projects/841604900168/locations/global/memberships/asm-acm-tutorial: servicemesh: controlPlaneManagement: details: - code: REVISION_READY details: 'Ready: asm-managed' state: ACTIVE state: code: OK description: 'Revision(s) ready for use: asm-managed.' updateTime: '2022-05-05T23:45:38.800808838Z' name: projects/PROJECT_ID/locations/global/features/servicemesh resourceState: state: ACTIVE spec: {} state: state: {} updateTime: '2022-05-05T23:45:44.848011023Z'
如果您看到的是
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" $WORK_DIR/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${CLUSTER} \ --config $WORK_DIR/acm-config.yaml
请注意此步骤可能需要几分钟时间才能完成。
查看
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/deployments@main", "status": "SYNCED" } ]
如果您看到的是
status: RECONCILING
而不是status: SYNCED
,请等待几分钟,然后再次运行gcloud alpha anthos config sync repo describe
。如需查看代管资源,您还可以添加
--managed-resources
标志。如需了解详情,请参阅跨多个集群查看 Config Sync 状态。查看
RepoSync
的 Config Sync 状态:gcloud alpha anthos config sync repo describe \ --sync-name repo-sync \ --sync-namespace onlineboutique
输出类似于以下内容:
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//online-boutique/deployments@main:HEAD", "status": "SYNCED" } ]
如果您看到的是
status: RECONCILING
而不是status: SYNCED
,请等待几分钟,然后再次运行gcloud alpha anthos config sync repo describe
。等待入站网关的公共 IP 地址完成预配:
until kubectl -n asm-ingress get svc asm-ingressgateway -o jsonpath='{.status.loadBalancer}' | grep "ingress"; do : ; done
获取入站网关的公共 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 通过创建限制条件从 Anthos Service Mesh 政策包强制执行政策。
强制执行边车代理注入
在本部分中,您将强制执行政策,以确保网格中的所有工作负载都启用了自动边车注入。
如需强制执行边车代理注入,请应用限制条件。
以下命令使用
sed
更新acm-config.yaml
文件,以使 Config Sync 部署关联的资源。sed -i "s,root-sync/deployments,root-sync/enforce-sidecar-injection,g" $WORK_DIR/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${CLUSTER} \ --config $WORK_DIR/acm-config.yaml
上述命令会应用以下资源:
K8sRequiredLabels
Constraint
,要求网格中的任何Namespace
均包含特定的 Anthos Service Mesh 边车代理注入标签: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
的工作方式,请尝试在集群中创建一个不包含label
和annotation
的Namespace
: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" $WORK_DIR/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${CLUSTER} \ --config $WORK_DIR/acm-config.yaml
上述命令会应用以下资源:
AsmPeerAuthnMeshStrictMtls
Constraint
,用于在istio-system
命名空间中强制执行网格级 mTLSPeerAuthentication
: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", "kind": "AsmPeerAuthnMeshStrictMtls", "message": "Root namespace <istio-system> does not have a strict mTLS PeerAuthentication", "name": "mesh-level-strict-mtls" } ]
通过在
istio-system
中部署PeerAuthentication
来解决此问题。如需阻止网格中的所有服务接受明文流量,请设置 mTLS 模式设为STRICT
的网格级PeerAuthentication
政策。部署政策时,控制层面会自动预配 TLS 证书,以便工作负载可以相互进行身份验证。以下命令使用
sed
更新acm-config.yaml
文件,以使 Config Sync 部署关联的资源。sed -i "s,root-sync/enforce-strict-mtls,root-sync/fix-strict-mtls,g" $WORK_DIR/acm-config.yaml gcloud beta container hub config-management apply \ --membership ${CLUSTER} \ --config $WORK_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" $WORK_DIR/acm-config.yaml gcloud beta container fleet config-management apply \ --membership ${CLUSTER} \ --config $WORK_DIR/acm-config.yaml
上述命令会应用以下资源:
AsmAuthzPolicyDefaultDeny
Constraint
,用于在istio-system
命名空间中强制执行网格级默认拒绝AuthorizationPolicy
:AsmAuthzPolicyEnforceSourcePrincipals
Constraint
,强制要求任何AuthorizationPolicies
均定义精细的来源主体(“*”除外)。只有asm-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", "kind": "AsmAuthzPolicyDefaultDeny", "message": "Root namespace <istio-system> does not have a default deny AuthorizationPolicy", "name": "default-deny-authorization-policies" } ]
通过在
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" $WORK_DIR/acm-config.yaml gcloud beta container hub config-management apply \ --membership ${CLUSTER} \ --config $WORK_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" $WORK_DIR/acm-config.yaml gcloud beta container hub config-management apply \ --membership ${CLUSTER} \ --config $WORK_DIR/acm-config.yaml
上述命令会应用以下资源:
asm-ingress
命名空间中的AuthorizationPolicy
:onlineboutique
命名空间中每个应用的AuthorizationPolicy
,下面是cartservice
应用的示例:asm-ingress
和onlineboutique
命名空间中每个应用的ServiceAccount
,以便在AuthorizationPolicies
中将每个应用的唯一身份评估为principal
。下面是cartservice
应用的示例:
查看
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
。查看
RepoSync
的 Config Sync 状态:gcloud alpha anthos config sync repo describe \ --sync-name repo-sync \ --sync-namespace onlineboutique
输出类似于以下内容:
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/online-boutique/authorization-policies@main", "status": "SYNCED" } ]
如果您看到的是
status: RECONCILING
而不是status: SYNCED
,请等待几分钟,然后再次运行gcloud alpha anthos config sync repo describe
。再次通过浏览器访问 Online Boutique 应用:
echo http://${EXTERNAL_IP}
如果等待几分钟,您现在应该会看到网站再次按预期成功运行。
查看 Anthos 安全功能的状态
您可以在 Google Cloud 控制台中查看 Anthos 安全功能的状态,包括身份验证和授权政策。
在控制台中,转到 Anthos 安全性页面。
政策摘要显示应用安全的状态,包括服务访问权限控制 (
AuthorizationPolicies
) 和 mTLS。点击政策审核以查看集群和两个命名空间(
asm-ingress
和onlineboutique
)的工作负载政策状态。服务访问权限控制和 mTLS 状态卡片提供简要概览。
工作负载列表显示每个工作负载的服务访问权限控制和 mTLS 状态。
现在,您已使用 Policy Controller 和 Config Sync 保护了集群和网格。
清理
为避免因本教程中使用的资源导致您的 Google Cloud 帐号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
- 在控制台中,打开管理资源页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击关闭以删除项目。
逐个删除资源
要逐个删除资源,请执行以下操作:
从舰队中取消注册集群:
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.
删除集群:
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].
删除您创建的文件:
rm -r $WORK_DIR
后续步骤
- 了解 Anthos Service Mesh 安全性方面的最佳做法。
- 详细了解 Anthos Service Mesh 政策软件包。
- 探索 Policy Controller 限制条件模板库。
- 探索有关 Google Cloud 的参考架构、图表、教程和最佳做法。查看我们的 Cloud Architecture Center。