Pod Security Admission (PSA) では、名前空間で実行されているポッドに対して Pod セキュリティの標準のポリシーを適用するラベルが使用されます。 AKS では、Pod Security Admission が既定で有効になっています。 Pod Security Admission や Pod Security Standards の詳細については、「名前空間ラベル付きの Pod Security Standards を適用する」および「Pod Security Standards」を参照してください。
ポッド セキュリティ アドミッションは、単一クラスター実装用の組み込みポリシー ソリューションです。 エンタープライズ レベルのポリシーを使用する場合は、Azure ポリシーを使用することをお勧めします。
開始する前に
- Azure サブスクリプション。 Azure サブスクリプションをお持ちでない場合は、無料のアカウントを作成できます。
- Azure CLI がインストールされていること。
- Kubernetes バージョン 1.23 以降を実行している既存の AKS クラスター。
クラスター内の名前空間に対して 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 ポリシーを適用する
kubectl create namespaceコマンドを使用して 2 つの名前空間を作成します。kubectl create namespace test-restricted kubectl create namespace test-privilegedrestrictedコマンドを使用して、名前空間ごとに 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の名前空間が構成されます。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 createdtest-restrictedコマンドを使用して、kubectl get pods名前空間で実行されているポッドがないことを確認します。kubectl get pods --namespace test-restricted次の出力例では、
test-restricted名前空間で実行されているポッドがないことを示しています。No resources found in test-restricted namespace.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 createdtest-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 2m28stest-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 に関するページを参照してください。