Skip to content

Commit b4f737e

Browse files
authored
Merge pull request #37587 from sftim/20221029_revise_psa_tutorials
Revise tutorials for Pod Security Admission
2 parents a8454fd + 04cfb74 commit b4f737e

File tree

5 files changed

+107
-92
lines changed

5 files changed

+107
-92
lines changed

content/en/docs/tutorials/security/cluster-level-pss.md

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ Install the following on your workstation:
3030
- [KinD](https://kind.sigs.k8s.io/docs/user/quick-start/#installation)
3131
- [kubectl](/docs/tasks/tools/)
3232

33+
This tutorial demonstrates what you can configure for a Kubernetes cluster that you fully
34+
control. If you are learning how to configure Pod Security Admission for a managed cluster
35+
where you are not able to configure the control plane, read
36+
[Apply Pod Security Standards at the namespace level](/docs/tutorials/security/ns-level-pss).
37+
3338
## Choose the right Pod Security Standard to apply
3439

3540
[Pod Security Admission](/docs/concepts/security/pod-security-admission/)
@@ -42,22 +47,22 @@ that are most appropriate for your configuration, do the following:
4247
1. Create a cluster with no Pod Security Standards applied:
4348

4449
```shell
45-
kind create cluster --name psa-wo-cluster-pss --image kindest/node:v1.24.0
50+
kind create cluster --name psa-wo-cluster-pss
4651
```
47-
The output is similar to this:
52+
The output is similar to:
4853
```
4954
Creating cluster "psa-wo-cluster-pss" ...
50-
✓ Ensuring node image (kindest/node:v1.24.0) 🖼
51-
✓ Preparing nodes 📦
55+
✓ Ensuring node image (kindest/node:v{{< skew currentVersion >}}.0) 🖼
56+
✓ Preparing nodes 📦
5257
✓ Writing configuration 📜
5358
✓ Starting control-plane 🕹️
5459
✓ Installing CNI 🔌
5560
✓ Installing StorageClass 💾
5661
Set kubectl context to "kind-psa-wo-cluster-pss"
5762
You can now use your cluster with:
58-
63+
5964
kubectl cluster-info --context kind-psa-wo-cluster-pss
60-
65+
6166
Thanks for using kind! 😊
6267
```
6368

@@ -72,7 +77,7 @@ that are most appropriate for your configuration, do the following:
7277
Kubernetes control plane is running at https://127.0.0.1:61350
7378
7479
CoreDNS is running at https://127.0.0.1:61350/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
75-
80+
7681
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
7782
```
7883

@@ -82,7 +87,7 @@ that are most appropriate for your configuration, do the following:
8287
kubectl get ns
8388
```
8489
The output is similar to this:
85-
```
90+
```
8691
NAME STATUS AGE
8792
default Active 9m30s
8893
kube-node-lease Active 9m32s
@@ -99,21 +104,23 @@ that are most appropriate for your configuration, do the following:
99104
kubectl label --dry-run=server --overwrite ns --all \
100105
pod-security.kubernetes.io/enforce=privileged
101106
```
102-
The output is similar to this:
103-
```
107+
108+
The output is similar to:
109+
```
104110
namespace/default labeled
105111
namespace/kube-node-lease labeled
106112
namespace/kube-public labeled
107113
namespace/kube-system labeled
108114
namespace/local-path-storage labeled
109115
```
110116
2. Baseline
111-
```shell
117+
```shell
112118
kubectl label --dry-run=server --overwrite ns --all \
113119
pod-security.kubernetes.io/enforce=baseline
114120
```
115-
The output is similar to this:
116-
```
121+
122+
The output is similar to:
123+
```
117124
namespace/default labeled
118125
namespace/kube-node-lease labeled
119126
namespace/kube-public labeled
@@ -123,15 +130,16 @@ that are most appropriate for your configuration, do the following:
123130
Warning: kube-proxy-m6hwf: host namespaces, hostPath volumes, privileged
124131
namespace/kube-system labeled
125132
namespace/local-path-storage labeled
126-
```
133+
```
127134

128135
3. Restricted
129136
```shell
130137
kubectl label --dry-run=server --overwrite ns --all \
131138
pod-security.kubernetes.io/enforce=restricted
132139
```
133-
The output is similar to this:
134-
```
140+
141+
The output is similar to:
142+
```
135143
namespace/default labeled
136144
namespace/kube-node-lease labeled
137145
namespace/kube-public labeled
@@ -180,7 +188,7 @@ following:
180188

181189
```
182190
mkdir -p /tmp/pss
183-
cat <<EOF > /tmp/pss/cluster-level-pss.yaml
191+
cat <<EOF > /tmp/pss/cluster-level-pss.yaml
184192
apiVersion: apiserver.config.k8s.io/v1
185193
kind: AdmissionConfiguration
186194
plugins:
@@ -212,7 +220,7 @@ following:
212220
1. Configure the API server to consume this file during cluster creation:
213221
214222
```
215-
cat <<EOF > /tmp/pss/cluster-config.yaml
223+
cat <<EOF > /tmp/pss/cluster-config.yaml
216224
kind: Cluster
217225
apiVersion: kind.x-k8s.io/v1alpha4
218226
nodes:
@@ -255,22 +263,22 @@ following:
255263
these Pod Security Standards:
256264
257265
```shell
258-
kind create cluster --name psa-with-cluster-pss --image kindest/node:v1.24.0 --config /tmp/pss/cluster-config.yaml
266+
kind create cluster --name psa-with-cluster-pss --config /tmp/pss/cluster-config.yaml
259267
```
260268
The output is similar to this:
261269
```
262270
Creating cluster "psa-with-cluster-pss" ...
263-
✓ Ensuring node image (kindest/node:v1.24.0) 🖼
264-
✓ Preparing nodes 📦
265-
✓ Writing configuration 📜
266-
✓ Starting control-plane 🕹️
267-
✓ Installing CNI 🔌
268-
✓ Installing StorageClass 💾
271+
✓ Ensuring node image (kindest/node:v{{< skew currentVersion >}}.0) 🖼
272+
✓ Preparing nodes 📦
273+
✓ Writing configuration 📜
274+
✓ Starting control-plane 🕹️
275+
✓ Installing CNI 🔌
276+
✓ Installing StorageClass 💾
269277
Set kubectl context to "kind-psa-with-cluster-pss"
270278
You can now use your cluster with:
271-
279+
272280
kubectl cluster-info --context kind-psa-with-cluster-pss
273-
281+
274282
Have a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/#community 🙂
275283
```
276284

@@ -281,36 +289,21 @@ following:
281289
The output is similar to this:
282290
```
283291
Kubernetes control plane is running at https://127.0.0.1:63855
284-
285292
CoreDNS is running at https://127.0.0.1:63855/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
286-
293+
287294
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
288295
```
289-
1. Create the following Pod specification for a minimal configuration in the default namespace:
290296

291-
```
292-
cat <<EOF > /tmp/pss/nginx-pod.yaml
293-
apiVersion: v1
294-
kind: Pod
295-
metadata:
296-
name: nginx
297-
spec:
298-
containers:
299-
- image: nginx
300-
name: nginx
301-
ports:
302-
- containerPort: 80
303-
EOF
304-
```
305-
1. Create the Pod in the cluster:
297+
1. Create a Pod in the default namespace:
306298

307299
```shell
308-
kubectl apply -f /tmp/pss/nginx-pod.yaml
300+
kubectl apply -f https://k8s.io/examples/security/example-baseline-pod.yaml
309301
```
310-
The output is similar to this:
302+
303+
The pod is started normally, but the output includes a warning:
311304
```
312-
Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "nginx" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "nginx" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "nginx" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "nginx" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")
313-
pod/nginx created
305+
Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "nginx" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "nginx" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "nginx" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "nginx" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")
306+
pod/nginx created
314307
```
315308

316309
## Clean up

content/en/docs/tutorials/security/ns-level-pss.md

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ Install the following on your workstation:
3131
1. Create a `KinD` cluster as follows:
3232

3333
```shell
34-
kind create cluster --name psa-ns-level --image kindest/node:v1.23.0
34+
kind create cluster --name psa-ns-level
3535
```
3636

3737
The output is similar to this:
3838

3939
```
4040
Creating cluster "psa-ns-level" ...
41-
✓ Ensuring node image (kindest/node:v1.23.0) 🖼
41+
✓ Ensuring node image (kindest/node:v{{< skew currentVersion >}}.0) 🖼
4242
✓ Preparing nodes 📦
4343
✓ Writing configuration 📜
4444
✓ Starting control-plane 🕹️
@@ -80,20 +80,21 @@ The output is similar to this:
8080
namespace/example created
8181
```
8282

83-
## Apply Pod Security Standards
83+
## Enable Pod Security Standards checking for that namespace
8484

8585
1. Enable Pod Security Standards on this namespace using labels supported by
86-
built-in Pod Security Admission. In this step we will warn on baseline pod
87-
security standard as per the latest version (default value)
86+
built-in Pod Security Admission. In this step you will configure a check to
87+
warn on Pods that don't meet the latest version of the _baseline_ pod
88+
security standard.
8889

8990
```shell
9091
kubectl label --overwrite ns example \
9192
pod-security.kubernetes.io/warn=baseline \
9293
pod-security.kubernetes.io/warn-version=latest
9394
```
9495

95-
2. Multiple pod security standards can be enabled on any namespace, using labels.
96-
Following command will `enforce` the `baseline` Pod Security Standard, but
96+
2. You can configure multiple pod security standard checks on any namespace, using labels.
97+
The following command will `enforce` the `baseline` Pod Security Standard, but
9798
`warn` and `audit` for `restricted` Pod Security Standards as per the latest
9899
version (default value)
99100

@@ -107,51 +108,34 @@ namespace/example created
107108
pod-security.kubernetes.io/audit-version=latest
108109
```
109110

110-
## Verify the Pod Security Standards
111+
## Verify the Pod Security Standard enforcement
111112

112-
1. Create a minimal pod in `example` namespace:
113+
1. Create a baseline Pod in the `example` namespace:
113114

114115
```shell
115-
cat <<EOF > /tmp/pss/nginx-pod.yaml
116-
apiVersion: v1
117-
kind: Pod
118-
metadata:
119-
name: nginx
120-
spec:
121-
containers:
122-
- image: nginx
123-
name: nginx
124-
ports:
125-
- containerPort: 80
126-
EOF
116+
kubectl apply -n example -f https://k8s.io/examples/security/example-baseline-pod.yaml
127117
```
128-
129-
1. Apply the pod spec to the cluster in `example` namespace:
130-
131-
```shell
132-
kubectl apply -n example -f /tmp/pss/nginx-pod.yaml
133-
```
134-
The output is similar to this:
118+
The Pod does start OK; the output includes a warning. For example:
135119

136120
```
137121
Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "nginx" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "nginx" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "nginx" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "nginx" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")
138122
pod/nginx created
139123
```
140124

141-
1. Apply the pod spec to the cluster in `default` namespace:
125+
1. Create a baseline Pod in the `default` namespace:
142126

143127
```shell
144-
kubectl apply -n default -f /tmp/pss/nginx-pod.yaml
128+
kubectl apply -n default -f https://k8s.io/examples/security/example-baseline-pod.yaml
145129
```
146130
Output is similar to this:
147131

148132
```
149133
pod/nginx created
150134
```
151135

152-
The Pod Security Standards were applied only to the `example`
153-
namespace. You could create the same Pod in the `default` namespace
154-
with no warnings.
136+
The Pod Security Standards enforcement and warning settings were applied only
137+
to the `example` namespace. You could create the same Pod in the `default`
138+
namespace with no warnings.
155139

156140
## Clean up
157141

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: nginx
5+
spec:
6+
containers:
7+
- image: nginx
8+
name: nginx
9+
ports:
10+
- containerPort: 80

content/en/examples/security/kind-with-cluster-level-baseline-pod-security.sh

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ nodes:
5151
# default None
5252
propagation: None
5353
EOF
54-
kind create cluster --name psa-with-cluster-pss --image kindest/node:v1.23.0 --config /tmp/pss/cluster-config.yaml
54+
kind create cluster --name psa-with-cluster-pss --config /tmp/pss/cluster-config.yaml
5555
kubectl cluster-info --context kind-psa-with-cluster-pss
56+
5657
# Wait for 15 seconds (arbitrary) ServiceAccount Admission Controller to be available
5758
sleep 15
58-
cat <<EOF > /tmp/pss/nginx-pod.yaml
59+
cat <<EOF |
5960
apiVersion: v1
6061
kind: Pod
6162
metadata:
@@ -67,4 +68,17 @@ spec:
6768
ports:
6869
- containerPort: 80
6970
EOF
70-
kubectl apply -f /tmp/pss/nginx-pod.yaml
71+
kubectl apply -f -
72+
73+
# Await input
74+
sleep 1
75+
( bash -c 'true' 2>/dev/null && bash -c 'read -p "Press any key to continue... " -n1 -s' ) || \
76+
( printf "Press Enter to continue... " && read ) 1>&2
77+
78+
# Clean up
79+
printf "\n\nCleaning up:\n" 1>&2
80+
set -e
81+
kubectl delete pod --all -n example --now
82+
kubectl delete ns example
83+
kind delete cluster --name psa-with-cluster-pss
84+
rm -f /tmp/pss/cluster-config.yaml
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
#!/bin/sh
2-
# Until v1.23 is released, kind node image needs to be built from k/k master branch
3-
# Ref: https://kind.sigs.k8s.io/docs/user/quick-start/#building-images
4-
kind create cluster --name psa-ns-level --image kindest/node:v1.23.0
2+
kind create cluster --name psa-ns-level
53
kubectl cluster-info --context kind-psa-ns-level
6-
# Wait for 15 seconds (arbitrary) ServiceAccount Admission Controller to be available
4+
# Wait for 15 seconds (arbitrary) for ServiceAccount Admission Controller to be available
75
sleep 15
8-
kubectl create ns example
6+
7+
# Create and label the namespace
8+
kubectl create ns example || exit 1 # if namespace exists, don't do the next steps
99
kubectl label --overwrite ns example \
1010
pod-security.kubernetes.io/enforce=baseline \
1111
pod-security.kubernetes.io/enforce-version=latest \
1212
pod-security.kubernetes.io/warn=restricted \
1313
pod-security.kubernetes.io/warn-version=latest \
1414
pod-security.kubernetes.io/audit=restricted \
1515
pod-security.kubernetes.io/audit-version=latest
16-
cat <<EOF > /tmp/pss/nginx-pod.yaml
16+
17+
# Try running a Pod
18+
cat <<EOF |
1719
apiVersion: v1
1820
kind: Pod
1921
metadata:
@@ -25,4 +27,16 @@ spec:
2527
ports:
2628
- containerPort: 80
2729
EOF
28-
kubectl apply -n example -f /tmp/pss/nginx-pod.yaml
30+
kubectl apply -n example -f -
31+
32+
# Await input
33+
sleep 1
34+
( bash -c 'true' 2>/dev/null && bash -c 'read -p "Press any key to continue... " -n1 -s' ) || \
35+
( printf "Press Enter to continue... " && read ) 1>&2
36+
37+
# Clean up
38+
printf "\n\nCleaning up:\n" 1>&2
39+
set -e
40+
kubectl delete pod --all -n example --now
41+
kubectl delete ns example
42+
kind delete cluster --name psa-ns-level

0 commit comments

Comments
 (0)