Pod Security Admission (PSA) 會使用標籤,對在命名空間中執行的 Pod 強制執行 Pod 安全性標準原則。 在 AKS 中,Pod Security Admission 預設為啟用。 如需 Pod 安全性許可和 Pod 安全性標準的詳細資訊,請參閱使用命名空間標籤強制執行 Pod 安全性標準和 Pod 安全性標準。
Pod 安全性許可為適用於單一叢集實作之內建原則解決方案。 如果您想要使用企業級原則,則建議您使用 Azure 原則。
開始之前
- 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 Security Admission 原則
使用
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 時,產生使用者面向的警告。使用
test-restricted命令,以嘗試將 Pod 部署至kubectl apply命名空間。 此命令會導致錯誤,因為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.使用
test-privileged命令,以嘗試將 Pod 部署至kubectl apply命名空間。 這次,Pod 應該可以成功部署,因為test-privileged命名空間已設定為允許部署違反privileged原則的 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下列範例輸出顯示有兩個 Pod 正在
test-privileged命名空間中執行: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使用
test-restricted命令來移除test-privileged和kubectl delete命名空間。kubectl delete namespace test-restricted test-privileged
下一步
在本文中,您已了解如何在 AKS 叢集中啟用 Pod Security Admission。 如需 Pod 安全性許可的詳細資訊,請參閱使用命名空間標籤強制執行 Pod 安全性標準。 若要深入了解 Pod 安全性許可所使用的 Pod 安全性標準,請參閱 Pod 安全性標準。