你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
在 Azure Kubernetes 服务 (AKS) 中使用 Pod 安全许可
Pod 安全准入 (PSA) 使用标签对命名空间中运行的 Pod 强制实施 Pod 安全标准策略。 默认情况下,AKS 启用 Pod 安全许可处于启用状态。 有关 Pod 安全准入和 Pod 安全标准的详细信息,请参阅使用命名空间标签强制实施 Pod 安全标准和 Pod 安全标准。
Pod 安全许可是单个群集实现的内置策略解决方案。 如果想使用企业级策略,建议使用 Azure Policy。
开始之前
- Azure 订阅。 如果没有 Azure 订阅,可以创建一个免费帐户。
- 已安装 Azure CLI。
- 运行 Kubernetes 1.23 或更高版本的现有 AKS 群集。
为群集中的命名空间启用 Pod 安全许可
为单个命名空间启用 PSA
使用
kubectl label
命令为群集中的单个命名空间启用 PSA,并使用要强制执行的策略值设置pod-security.kubernetes.io/enforce
标签。 以下示例为 NAMESPACE 命名空间启用restricted
策略。kubectl label --overwrite ns NAMESPACE pod-security.kubernetes.io/enforce=restricted
为所有命名空间启用 PSA
使用
kubectl label
命令为群集中的所有命名空间启用 PSA,并使用要强制执行的策略值设置pod-security.kubernetes.io/warn
标签。 以下示例为群集中的所有命名空间启用baseline
策略。 如果任何 Pod 部署到不符合基线策略的命名空间,此策略会生成面向用户的警告。kubectl label --overwrite ns --all pod-security.kubernetes.io/warn=baseline
使用部署强制执行 Pod 安全许可策略
使用
kubectl create namespace
命令创建两个命名空间。kubectl create namespace test-restricted kubectl create namespace test-privileged
使用
kubectl label
命令为每个命名空间启用 PSA 策略,其中一个具有restricted
策略,另一个具有baseline
策略。kubectl label --overwrite ns test-restricted pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/warn=restricted kubectl label --overwrite ns test-privileged pod-security.kubernetes.io/enforce=privileged pod-security.kubernetes.io/warn=privileged
这会将
test-restricted
和test-privileged
命名空间配置为阻止运行 Pod,并在任何不符合配置策略的 pod 尝试运行时生成面向用户的警告。尝试使用
kubectl apply
命令将 Pod 部署到测试test-restricted
命名空间。 此命令会导致错误,因为测试test-restricted
命名空间被配置为阻止不符合restricted
策略的 Pod。kubectl apply --namespace test-restricted -f https://raw.githubusercontent.com/Azure-Samples/azure-voting-app-redis/master/azure-vote-all-in-one-redis.yaml
以下示例输出显示一条警告,指出 Pod 违反了配置的策略:
... Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "azure-vote-back" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "azure-vote-back" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "azure-vote-back" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "azure-vote-back" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost") deployment.apps/azure-vote-back created service/azure-vote-back created Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "azure-vote-front" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "azure-vote-front" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "azure-vote-front" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "azure-vote-front" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost") deployment.apps/azure-vote-front created service/azure-vote-front created
使用
kubectl get pods
命令确认test-restricted
命名空间中没有正在运行的 Pod。kubectl get pods --namespace test-restricted
以下示例输出显示
test-restricted
命名空间中没有正在运行的 Pod:No resources found in test-restricted namespace.
尝试使用
kubectl apply
命令将 Pod 部署到测试test-privileged
命名空间。 这次,Pod 应该会成功部署,因为test-privileged
命名空间配置为允许违反privileged
CELVE 的 Pod。kubectl apply --namespace test-privileged -f https://raw.githubusercontent.com/Azure-Samples/azure-voting-app-redis/master/azure-vote-all-in-one-redis.yaml
以下示例输出显示 Pod 已成功部署:
deployment.apps/azure-vote-back created service/azure-vote-back created deployment.apps/azure-vote-front created service/azure-vote-front created
使用
kubectl get pods
命令确认test-privileged
命名空间中有正在运行的 Pod。kubectl get pods --namespace test-privileged
以下示例输出显示
test-privileged
命名空间中有两个正在运行的 Pod:NAME READY STATUS RESTARTS AGE azure-vote-back-6fcdc5cbd5-svbdf 1/1 Running 0 2m29s azure-vote-front-5f4b8d498-tqzwv 1/1 Running 0 2m28s
使用
kubectl delete
命令删除test-restricted
和test-privileged
命名空间。kubectl delete namespace test-restricted test-privileged
后续步骤
在本文中,你学习了如何为 AKS 群集启用 Pod 安全许可。 有关 Pod 安全许可的详细信息,请参阅使用命名空间标签强制执行 Pod 安全标准 有关 Pod 安全许可使用的 Pod 安全标准的详细信息,请参阅 Pod 安全标准。