在 Azure Kubernetes Service (AKS) 中使用 Pod 安全性許可

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 原則

  1. 使用 kubectl create namespace 命令來建立兩個命名空間。

    kubectl create namespace test-restricted
    kubectl create namespace test-privileged
    
  2. 使用 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 時,產生使用者面向的警告。

  3. 使用 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
    
  4. 使用 kubectl get pods 命令,確認 test-restricted 命名空間中沒有任何 Pod 正在執行。

    kubectl get pods --namespace test-restricted
    

    下列輸出範例顯示 test-restricted 命名空間中沒有任何 Pod 正在執行:

    No resources found in test-restricted namespace.
    
  5. 使用 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
    
  6. 使用 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
    
  7. 使用 test-restricted 命令來移除 test-privileged 和 kubectl delete 命名空間。

    kubectl delete namespace test-restricted test-privileged
    

下一步

在本文中,您已了解如何在 AKS 叢集中啟用 Pod Security Admission。 如需 Pod 安全性許可的詳細資訊,請參閱使用命名空間標籤強制執行 Pod 安全性標準。 若要深入了解 Pod 安全性許可所使用的 Pod 安全性標準,請參閱 Pod 安全性標準。