|
| 1 | +--- |
| 2 | +title: Use Pod Security Admission in Azure Kubernetes Service (AKS) |
| 3 | +description: Learn how to enable and use Pod Security Admission with Azure Kubernetes Service (AKS). |
| 4 | +services: container-service |
| 5 | +ms.topic: article |
| 6 | +ms.date: 08/08/2022 |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +# Use Pod Security Admission in Azure Kubernetes Service (AKS) |
| 11 | + |
| 12 | +Pod Security Admission enforces Pod Security Standards policies on pods running in a namespace. Pod Security Admission is enabled by default in AKS and is controlled by adding labels to a namespace. For more information about Pod Security Admission, see [Enforce Pod Security Standards with Namespace Labels][kubernetes-psa]. For more information about the Pod Security Standards used by Pod Security Admission, see [Pod Security Standards][kubernetes-pss]. |
| 13 | + |
| 14 | +## Before you begin |
| 15 | + |
| 16 | +- An Azure subscription. If you don't have an Azure subscription, you can create a [free account](https://azure.microsoft.com/free). |
| 17 | +- [Azure CLI installed](/cli/azure/install-azure-cli). |
| 18 | +- An existing AKS cluster running Kubernetes version 1.23 or higher. |
| 19 | + |
| 20 | +## Enable Pod Security Admission for a namespace in your cluster |
| 21 | + |
| 22 | +To enable PSA for a namespace in your cluster, set the `pod-security.kubernetes.io/enforce` label with the policy value you want to enforce. For example: |
| 23 | + |
| 24 | +```azurecli-interactive |
| 25 | +kubectl label --overwrite ns NAMESPACE pod-security.kubernetes.io/enforce=restricted |
| 26 | +``` |
| 27 | + |
| 28 | +The above command enforces the `restricted` policy for the *NAMESPACE* namespace. |
| 29 | + |
| 30 | +You can also enable Pod Security Admission for all your namespaces. For example: |
| 31 | + |
| 32 | +```azurecli-interactive |
| 33 | +kubectl label --overwrite ns --all pod-security.kubernetes.io/warn=baseline |
| 34 | +``` |
| 35 | + |
| 36 | +The above example will generate a user-facing warning if any pods are deployed to any namespace that does not meet the `baseline` policy. |
| 37 | + |
| 38 | +## Example of enforcing a Pod Security Admission policy with a deployment |
| 39 | + |
| 40 | +Create two namespaces, one with the `restricted` policy and one with the `baseline` policy. |
| 41 | + |
| 42 | +```azurecli-interactive |
| 43 | +kubectl create namespace test-restricted |
| 44 | +kubectl create namespace test-privileged |
| 45 | +kubectl label --overwrite ns test-restricted pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/warn=restricted |
| 46 | +kubectl label --overwrite ns test-privileged pod-security.kubernetes.io/enforce=privileged pod-security.kubernetes.io/warn=privileged |
| 47 | +``` |
| 48 | + |
| 49 | +Both the `test-restricted` and `test-privileged` namespaces will block running pods as well as generate a user-facing warning if any pods attempt to run that do not meet the configured policy. |
| 50 | + |
| 51 | +Attempt to deploy pods to the `test-restricted` namespace. |
| 52 | + |
| 53 | +```azurecli-interactive |
| 54 | +kubectl apply --namespace test-restricted -f https://raw.githubusercontent.com/Azure-Samples/azure-voting-app-redis/master/azure-vote-all-in-one-redis.yaml |
| 55 | +``` |
| 56 | + |
| 57 | +Notice you get a warning that the pods violate the configured policy. |
| 58 | + |
| 59 | +```output |
| 60 | +... |
| 61 | +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") |
| 62 | +deployment.apps/azure-vote-back created |
| 63 | +service/azure-vote-back created |
| 64 | +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") |
| 65 | +deployment.apps/azure-vote-front created |
| 66 | +service/azure-vote-front created |
| 67 | +``` |
| 68 | + |
| 69 | +Confirm there are no pods running in the `test-restricted` namespace. |
| 70 | + |
| 71 | +```azurecli-interactive |
| 72 | +kubectl get pods --namespace test-restricted |
| 73 | +``` |
| 74 | + |
| 75 | +```output |
| 76 | +$ kubectl get pods --namespace test-restricted |
| 77 | +No resources found in test-restricted namespace. |
| 78 | +``` |
| 79 | + |
| 80 | +Attempt to deploy pods to the `test-privileged` namespace. |
| 81 | + |
| 82 | +```azurecli-interactive |
| 83 | +kubectl apply --namespace test-privileged -f https://raw.githubusercontent.com/Azure-Samples/azure-voting-app-redis/master/azure-vote-all-in-one-redis.yaml |
| 84 | +``` |
| 85 | + |
| 86 | +Notice there are no warnings about pods not meeting the configured policy. |
| 87 | + |
| 88 | +Confirm you have pods running in the `test-privileged` namespace. |
| 89 | + |
| 90 | +```azurecli-interactive |
| 91 | +kubectl get pods --namespace test-privileged |
| 92 | +``` |
| 93 | + |
| 94 | +```output |
| 95 | +$ kubectl get pods --namespace test-privileged |
| 96 | +NAME READY STATUS RESTARTS AGE |
| 97 | +azure-vote-back-6fcdc5cbd5-svbdf 1/1 Running 0 2m29s |
| 98 | +azure-vote-front-5f4b8d498-tqzwv 1/1 Running 0 2m28s |
| 99 | +``` |
| 100 | + |
| 101 | +Delete both the `test-restricted` and `test-privileged` namespaces. |
| 102 | + |
| 103 | +```azurecli-interactive |
| 104 | +kubectl delete namespace test-restricted test-privileged |
| 105 | +``` |
| 106 | + |
| 107 | +## Next steps |
| 108 | + |
| 109 | +In this article, you learned how to enable Pod Security Admission an AKS cluster. For more information about Pod Security Admission, see [Enforce Pod Security Standards with Namespace Labels][kubernetes-psa]. For more information about the Pod Security Standards used by Pod Security Admission, see [Pod Security Standards][kubernetes-pss]. |
| 110 | + |
| 111 | +<!-- LINKS - Internal --> |
| 112 | +[kubernetes-psa]: https://kubernetes.io/docs/tasks/configure-pod-container/enforce-standards-namespace-labels/ |
| 113 | +[kubernetes-pss]: https://kubernetes.io/docs/concepts/security/pod-security-standards/ |
0 commit comments