Azure Kubernetes Service (AKS) で Pod Security Admission を使用する

Pod Security Admission (PSA) では、名前空間で実行されているポッドに対して Pod セキュリティの標準のポリシーを適用するラベルが使用されます。 AKS では、Pod Security Admission が既定で有効になっています。 Pod Security Admission や Pod Security Standards の詳細については、「名前空間ラベル付きの Pod Security Standards を適用する」および「Pod Security Standards」を参照してください。

ポッド セキュリティ アドミッションは、単一クラスター実装用の組み込みポリシー ソリューションです。 エンタープライズ レベルのポリシーを使用する場合は、Azure ポリシーを使用することをお勧めします。

開始する前に

クラスター内の名前空間に対して Pod Security Admission を有効にする

1 つの名前空間に対して PSA を有効にする

  • kubectl label コマンドを使用してクラスター内の 1 つの名前空間に対して PSA を有効にし、適用するポリシー値を使用して pod-security.kubernetes.io/enforce ラベルを設定します。 次の例では、"restricted" 名前空間に対して ポリシーを有効にします。

    kubectl label --overwrite ns NAMESPACE pod-security.kubernetes.io/enforce=restricted
    

すべての名前空間に対して PSA を有効にする

  • kubectl label コマンドを使用してクラスター内のすべての名前空間に対して PSA を有効にし、適用するポリシー値を使用して pod-security.kubernetes.io/warn ラベルを設定します。 次の例では、クラスター内のすべての名前空間に対して baseline ポリシーを有効にします。 このポリシーでは、"ベースライン" ポリシーを満たしていない名前空間にポッドがデプロイされている場合、ユーザーに対する警告を生成します。

    kubectl label --overwrite ns --all pod-security.kubernetes.io/warn=baseline
    

デプロイに Pod Security Admission ポリシーを適用する

  1. kubectl create namespace コマンドを使用して 2 つの名前空間を作成します。

    kubectl create namespace test-restricted
    kubectl create namespace test-privileged
    
  2. restricted コマンドを使用して、名前空間ごとに PSA ポリシーを有効にします。1 つには baseline ポリシーを使用し、もう 1 つには kubectl label ポリシーを使用します。

    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 の名前空間が構成されます。

  3. test-restricted コマンドを使用して、kubectl apply 名前空間へのポッドのデプロイを試みます。 このコマンドは、エラーになります。これは、test-restricted ポリシーを満たしていないポッドをブロックするように、restricted 名前空間が構成されているためです。

    kubectl apply --namespace test-restricted -f https://raw.githubusercontent.com/Azure-Samples/azure-voting-app-redis/master/azure-vote-all-in-one-redis.yaml
    

    次の出力例は、構成されたポリシーにポッドが違反していることを述べている警告を示しています。

    ...
    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. test-restricted コマンドを使用して、kubectl get pods 名前空間で実行されているポッドがないことを確認します。

    kubectl get pods --namespace test-restricted
    

    次の出力例では、test-restricted 名前空間で実行されているポッドがないことを示しています。

    No resources found in test-restricted namespace.
    
  5. test-privileged コマンドを使用して、kubectl apply 名前空間へのポッドのデプロイを試みます。 今回は、test-privileged ポリシーに違反するポッドを許可するように privileged 名前空間が構成されているため、ポッドは正常にデプロイされる必要があります。

    kubectl apply --namespace test-privileged -f https://raw.githubusercontent.com/Azure-Samples/azure-voting-app-redis/master/azure-vote-all-in-one-redis.yaml
    

    次の出力例は、ポッドが正常にデプロイされたことを示しています。

    deployment.apps/azure-vote-back created
    service/azure-vote-back created
    deployment.apps/azure-vote-front created
    service/azure-vote-front created
    
  6. test-privileged コマンドを使用して、kubectl get pods 名前空間でポッドが実行されていることを確認します。

    kubectl get pods --namespace test-privileged
    

    次の出力例は、test-privileged 名前空間で 2 つのポッドが実行されていることを示しています。

    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 Security Admission の詳細については、名前空間ラベルを使用した Pod Security Standards の適用に関するページを参照してください。 Pod Security Admission で使用される Pod Security Standards の詳細については、Pod Security Standards に関するページを参照してください。