Skip to content

Commit 4588187

Browse files
Merge pull request #251176 from schaffererin/use-psa-freshness-pass
Use PSA in AKS freshness pass
2 parents d73a5a9 + 538c251 commit 4588187

File tree

1 file changed

+78
-64
lines changed

1 file changed

+78
-64
lines changed

articles/aks/use-psa.md

Lines changed: 78 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
---
22
title: Use Pod Security Admission in Azure Kubernetes Service (AKS)
33
description: Learn how to enable and use Pod Security Admission with Azure Kubernetes Service (AKS).
4+
ms.custom: azure-kubernetes-service
45
ms.topic: article
5-
ms.date: 08/08/2022
6-
6+
ms.date: 09/12/2023
77
---
88

99
# Use Pod Security Admission in Azure Kubernetes Service (AKS)
1010

11-
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].
11+
Pod Security Admission (PSA) uses labels to enforce Pod Security Standards policies on pods running in a namespace. AKS enables Pod Security Admission is enabled by default. For more information about Pod Security Admission and Pod Security Standards, see [Enforce Pod Security Standards with namespace labels][kubernetes-psa] and [Pod Security Standards][kubernetes-pss].
1212

13-
Pod Security Admission is a built-in policy solution for single cluster implementations. If you are looking for enterprise-grade policy, then [Azure policy](use-azure-policy.md) is a better choice.
13+
Pod Security Admission is a built-in policy solution for single cluster implementations. If you want to use an enterprise-grade policy, we recommend you use [Azure policy](use-azure-policy.md).
1414

1515
## Before you begin
1616

@@ -20,90 +20,104 @@ Pod Security Admission is a built-in policy solution for single cluster implemen
2020

2121
## Enable Pod Security Admission for a namespace in your cluster
2222

23-
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+
### Enable PSA for a single namespace
24+
25+
- Enable PSA for a single namespace in your cluster using the `kubectl label` command and set the `pod-security.kubernetes.io/enforce` label with the policy value you want to enforce. The following example enables the `restricted` policy for the *NAMESPACE* namespace.
26+
27+
```azurecli-interactive
28+
kubectl label --overwrite ns NAMESPACE pod-security.kubernetes.io/enforce=restricted
29+
```
30+
31+
### Enable PSA for all namespaces
32+
33+
- Enable PSA for all namespaces in your cluster using the `kubectl label` command and set the `pod-security.kubernetes.io/warn` label with the policy value you want to enforce. The following example enables the `baseline` policy for all namespaces in your cluster. This policy generates a user-facing warning if any pods are deployed to a namespace that doesn't meet the *baseline* policy.
34+
35+
```azurecli-interactive
36+
kubectl label --overwrite ns --all pod-security.kubernetes.io/warn=baseline
37+
```
2438
25-
```azurecli-interactive
26-
kubectl label --overwrite ns NAMESPACE pod-security.kubernetes.io/enforce=restricted
27-
```
39+
## Enforce a Pod Security Admission policy with a deployment
2840
29-
The above command enforces the `restricted` policy for the *NAMESPACE* namespace.
41+
1. Create two namespaces using the `kubectl create namespace` command.
3042
31-
You can also enable Pod Security Admission for all your namespaces. For example:
43+
```azurecli-interactive
44+
kubectl create namespace test-restricted
45+
kubectl create namespace test-privileged
46+
```
3247
33-
```azurecli-interactive
34-
kubectl label --overwrite ns --all pod-security.kubernetes.io/warn=baseline
35-
```
48+
1. Enable a PSA policy for each namespace, one with the `restricted` policy and one with the `baseline` policy, using the `kubectl label` command.
3649
37-
The above example will generate a user-facing warning if any pods are deployed to any namespace that does not meet the `baseline` policy.
50+
```azurecli-interactive
51+
kubectl label --overwrite ns test-restricted pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/warn=restricted
52+
kubectl label --overwrite ns test-privileged pod-security.kubernetes.io/enforce=privileged pod-security.kubernetes.io/warn=privileged
53+
```
3854
39-
## Example of enforcing a Pod Security Admission policy with a deployment
55+
This configures the `test-restricted` and `test-privileged` namespaces to block running pods and generate a user-facing warning if any pods that don't meet the configured policy attempt to run.
4056
41-
Create two namespaces, one with the `restricted` policy and one with the `baseline` policy.
57+
1. Attempt to deploy pods to the `test-restricted` namespace using the `kubectl apply` command. This command results in an error because the `test-restricted` namespace is configured to block pods that don't meet the `restricted` policy.
4258
43-
```azurecli-interactive
44-
kubectl create namespace test-restricted
45-
kubectl create namespace test-privileged
46-
kubectl label --overwrite ns test-restricted pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/warn=restricted
47-
kubectl label --overwrite ns test-privileged pod-security.kubernetes.io/enforce=privileged pod-security.kubernetes.io/warn=privileged
48-
```
59+
```azurecli-interactive
60+
kubectl apply --namespace test-restricted -f https://raw.githubusercontent.com/Azure-Samples/azure-voting-app-redis/master/azure-vote-all-in-one-redis.yaml
61+
```
4962
50-
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.
63+
The following example output shows a warning stating the pods violate the configured policy:
5164
52-
Attempt to deploy pods to the `test-restricted` namespace.
65+
```output
66+
...
67+
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")
68+
deployment.apps/azure-vote-back created
69+
service/azure-vote-back created
70+
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")
71+
deployment.apps/azure-vote-front created
72+
service/azure-vote-front created
73+
```
5374
54-
```azurecli-interactive
55-
kubectl apply --namespace test-restricted -f https://raw.githubusercontent.com/Azure-Samples/azure-voting-app-redis/master/azure-vote-all-in-one-redis.yaml
56-
```
75+
1. Confirm there are no pods running in the `test-restricted` namespace using the `kubectl get pods` command.
5776
58-
Notice you get a warning that the pods violate the configured policy.
77+
```azurecli-interactive
78+
kubectl get pods --namespace test-restricted
79+
```
5980
60-
```output
61-
...
62-
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")
63-
deployment.apps/azure-vote-back created
64-
service/azure-vote-back created
65-
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")
66-
deployment.apps/azure-vote-front created
67-
service/azure-vote-front created
68-
```
81+
The following example output shows no pods running in the `test-restricted` namespace:
6982
70-
Confirm there are no pods running in the `test-restricted` namespace.
83+
```output
84+
No resources found in test-restricted namespace.
85+
```
7186
72-
```azurecli-interactive
73-
kubectl get pods --namespace test-restricted
74-
```
87+
1. Attempt to deploy pods to the `test-privileged` namespace using the `kubectl apply` command. This time, the pods should deploy successfully because the `test-privileged` namespace is configured to allow pods that violate the `privileged` policy.
7588
76-
```output
77-
$ kubectl get pods --namespace test-restricted
78-
No resources found in test-restricted namespace.
79-
```
89+
```azurecli-interactive
90+
kubectl apply --namespace test-privileged -f https://raw.githubusercontent.com/Azure-Samples/azure-voting-app-redis/master/azure-vote-all-in-one-redis.yaml
91+
```
8092
81-
Attempt to deploy pods to the `test-privileged` namespace.
93+
The following example output shows the pods deployed successfully:
8294
83-
```azurecli-interactive
84-
kubectl apply --namespace test-privileged -f https://raw.githubusercontent.com/Azure-Samples/azure-voting-app-redis/master/azure-vote-all-in-one-redis.yaml
85-
```
95+
```output
96+
deployment.apps/azure-vote-back created
97+
service/azure-vote-back created
98+
deployment.apps/azure-vote-front created
99+
service/azure-vote-front created
100+
```
86101
87-
Notice there are no warnings about pods not meeting the configured policy.
102+
1. Confirm you have pods running in the `test-privileged` namespace using the `kubectl get pods` command.
88103
89-
Confirm you have pods running in the `test-privileged` namespace.
104+
```azurecli-interactive
105+
kubectl get pods --namespace test-privileged
106+
```
90107
91-
```azurecli-interactive
92-
kubectl get pods --namespace test-privileged
93-
```
108+
The following example output shows two pods running in the `test-privileged` namespace:
94109
95-
```output
96-
$ kubectl get pods --namespace test-privileged
97-
NAME READY STATUS RESTARTS AGE
98-
azure-vote-back-6fcdc5cbd5-svbdf 1/1 Running 0 2m29s
99-
azure-vote-front-5f4b8d498-tqzwv 1/1 Running 0 2m28s
100-
```
110+
```output
111+
NAME READY STATUS RESTARTS AGE
112+
azure-vote-back-6fcdc5cbd5-svbdf 1/1 Running 0 2m29s
113+
azure-vote-front-5f4b8d498-tqzwv 1/1 Running 0 2m28s
114+
```
101115
102-
Delete both the `test-restricted` and `test-privileged` namespaces.
116+
1. Remove the `test-restricted` and `test-privileged` namespaces using the `kubectl delete` command.
103117
104-
```azurecli-interactive
105-
kubectl delete namespace test-restricted test-privileged
106-
```
118+
```azurecli-interactive
119+
kubectl delete namespace test-restricted test-privileged
120+
```
107121
108122
## Next steps
109123

0 commit comments

Comments
 (0)