共用方式為


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

Pod 安全性許可 (PSA) 使用標籤以在命名空間中所執行的 Pod 上強制執行 Pod 安全性標準原則。 AKS 預設會啟用 Pod 安全性許可。 如需 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 安全性許可原則

  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-restrictedtest-privileged 命名空間來封鎖執行中 Pod,並在嘗試執行任何不符合所設定原則的 Pod 時,產生使用者面向的警告。

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

    kubectl get pods --namespace test-restricted
    

    下列範例輸出會顯示 test-restricted 命名空間中未執行任何 Pod:

    No resources found in test-restricted namespace.
    
  5. 使用 kubectl apply 命令,以嘗試將 Pod 部署至 test-privileged 命名空間。 目前,應該會成功部署 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
    

    下列範例輸出會顯示 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
    
  7. 使用 kubectl delete 命令來移除 test-restrictedtest-privileged 命名空間。

    kubectl delete namespace test-restricted test-privileged
    

下一步

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