1
1
---
2
- title : Applying Pod Security Standards at the cluster level
2
+ title : Apply Pod Security Standards at the Cluster Level
3
3
content_type : tutorial
4
4
weight : 10
5
5
---
@@ -8,15 +8,16 @@ weight: 10
8
8
This tutorial applies only for new clusters.
9
9
{{% /alert %}}
10
10
11
- Pod Security admission (PSA) is enabled by default in v1.23 and later, as it [ graduated
12
- to beta] ( /blog/2021/12/15/pod-security-admission-beta/ ) . Pod Security Admission
13
- is an admission controller that applies Pod Security Standards when pods are
14
- created. This tutorial shows you how to enforce the ` baseline ` Pod Security
15
- Standard at the cluster level which applies a standard configuration
11
+ Pod Security admission (PSA) is enabled by default in v1.23 and later, as it has
12
+ [ graduated to beta] ( /blog/2021/12/09/pod-security-admission-beta/ ) .
13
+ Pod Security
14
+ is an admission controller that carries out checks against the Kubernetes
15
+ [ Pod Security Standards] ( docs/concepts/security/pod-security-standards/ ) when new pods are
16
+ created. This tutorial shows you how to enforce the ` baseline ` Pod Security
17
+ Standard at the cluster level which applies a standard configuration
16
18
to all namespaces in a cluster.
17
19
18
- For applying pod security standards one namespace at a time, please [ follow this
19
- tutorial] ( /docs/tutorials/security/ns-level-pss ) .
20
+ To apply Pod Security Standards to specific namespaces, refer to [ Apply Pod Security Standards at the namespace level] ( /docs/tutorials/security/ns-level-pss ) .
20
21
21
22
## {{% heading "prerequisites" %}}
22
23
@@ -37,12 +38,12 @@ that are most appropriate for your configuration, do the following:
37
38
1 . Create a cluster with no Pod Security Standards applied:
38
39
39
40
``` shell
40
- kind create cluster --name psa-wo-cluster-pss --image kindest/node:latest
41
+ kind create cluster --name psa-wo-cluster-pss --image kindest/node:v1.23.0
41
42
```
42
- The output is similar to this:
43
+ The output is similar to this:
43
44
```
44
45
Creating cluster " psa-wo-cluster-pss" ...
45
- ✓ Ensuring node image (kindest/node:latest ) 🖼
46
+ ✓ Ensuring node image (kindest/node:v1.23.0 ) 🖼
46
47
✓ Preparing nodes 📦
47
48
✓ Writing configuration 📜
48
49
✓ Starting control-plane 🕹️
@@ -57,12 +58,12 @@ that are most appropriate for your configuration, do the following:
57
58
58
59
```
59
60
60
- 2 . Set the kubectl context to the new cluster:
61
+ 1 . Set the kubectl context to the new cluster:
61
62
62
63
` ` ` shell
63
64
kubectl cluster-info --context kind-psa-wo-cluster-pss
64
65
` ` `
65
- The output is similar to this:
66
+ The output is similar to this:
66
67
67
68
```
68
69
Kubernetes control plane is running at https://127.0.0.1:61350
@@ -72,7 +73,7 @@ that are most appropriate for your configuration, do the following:
72
73
To further debug and diagnose cluster problems, use ' kubectl cluster-info dump' .
73
74
```
74
75
75
- 3. Get a list of namespaces in the cluster:
76
+ 1. Get a list of namespaces in the cluster:
76
77
77
78
` ` ` shell
78
79
kubectl get ns
@@ -87,71 +88,71 @@ that are most appropriate for your configuration, do the following:
87
88
local-path-storage Active 9m26s
88
89
```
89
90
90
- 4 . Use ` --dry-run=server` to understand what happens when different Pod Security Standards
91
- are applied:
91
+ 1 . Use ` --dry-run=server` to understand what happens when different Pod Security Standards
92
+ are applied:
92
93
93
94
1. Privileged
94
- ` ` ` shell
95
- kubectl label --dry-run=server --overwrite ns --all \
96
- pod-security.kubernetes.io/enforce=privileged
97
- ` ` `
98
- The output is similar to this:
99
- ```
100
- namespace/default labeled
101
- namespace/kube-node-lease labeled
102
- namespace/kube-public labeled
103
- namespace/kube-system labeled
104
- namespace/local-path-storage labeled
105
- ```
95
+ ` ` ` shell
96
+ kubectl label --dry-run=server --overwrite ns --all \
97
+ pod-security.kubernetes.io/enforce=privileged
98
+ ` ` `
99
+ The output is similar to this:
100
+ ```
101
+ namespace/default labeled
102
+ namespace/kube-node-lease labeled
103
+ namespace/kube-public labeled
104
+ namespace/kube-system labeled
105
+ namespace/local-path-storage labeled
106
+ ```
106
107
2. Baseline
107
- ` ` ` shell
108
- kubectl label --dry-run=server --overwrite ns --all \
109
- pod-security.kubernetes.io/enforce=baseline
110
- ` ` `
111
- The output is similar to this:
112
- ```
113
- namespace/default labeled
114
- namespace/kube-node-lease labeled
115
- namespace/kube-public labeled
116
- Warning: existing pods in namespace " kube-system" violate the new PodSecurity enforce level " baseline:latest"
117
- Warning: etcd-psa-wo-cluster-pss-control-plane (and 3 other pods): host namespaces, hostPath volumes
118
- Warning: kindnet-vzj42: non-default capabilities, host namespaces, hostPath volumes
119
- Warning: kube-proxy-m6hwf: host namespaces, hostPath volumes, privileged
120
- namespace/kube-system labeled
121
- namespace/local-path-storage labeled
122
- ```
108
+ ` ` ` shell
109
+ kubectl label --dry-run=server --overwrite ns --all \
110
+ pod-security.kubernetes.io/enforce=baseline
111
+ ` ` `
112
+ The output is similar to this:
113
+ ```
114
+ namespace/default labeled
115
+ namespace/kube-node-lease labeled
116
+ namespace/kube-public labeled
117
+ Warning: existing pods in namespace " kube-system" violate the new PodSecurity enforce level " baseline:latest"
118
+ Warning: etcd-psa-wo-cluster-pss-control-plane (and 3 other pods): host namespaces, hostPath volumes
119
+ Warning: kindnet-vzj42: non-default capabilities, host namespaces, hostPath volumes
120
+ Warning: kube-proxy-m6hwf: host namespaces, hostPath volumes, privileged
121
+ namespace/kube-system labeled
122
+ namespace/local-path-storage labeled
123
+ ```
123
124
124
125
3. Restricted
125
- ` ` ` shell
126
- kubectl label --dry-run=server --overwrite ns --all \
127
- pod-security.kubernetes.io/enforce=restricted
128
- ` ` `
129
- The output is similar to this:
130
- ```
131
- namespace/default labeled
132
- namespace/kube-node-lease labeled
133
- namespace/kube-public labeled
134
- Warning: existing pods in namespace " kube-system" violate the new PodSecurity enforce level " restricted:latest"
135
- Warning: coredns-7bb9c7b568-hsptc (and 1 other pod): unrestricted capabilities, runAsNonRoot ! = true, seccompProfile
136
- Warning: etcd-psa-wo-cluster-pss-control-plane (and 3 other pods): host namespaces, hostPath volumes, allowPrivilegeEscalation ! = false, unrestricted capabilities, restricted volume types, runAsNonRoot ! = true
137
- Warning: kindnet-vzj42: non-default capabilities, host namespaces, hostPath volumes, allowPrivilegeEscalation ! = false, unrestricted capabilities, restricted volume types, runAsNonRoot ! = true, seccompProfile
138
- Warning: kube-proxy-m6hwf: host namespaces, hostPath volumes, privileged, allowPrivilegeEscalation ! = false, unrestricted capabilities, restricted volume types, runAsNonRoot ! = true, seccompProfile
139
- namespace/kube-system labeled
140
- Warning: existing pods in namespace " local-path-storage" violate the new PodSecurity enforce level " restricted:latest"
141
- Warning: local-path-provisioner-d6d9f7ffc-lw9lh: allowPrivilegeEscalation ! = false, unrestricted capabilities, runAsNonRoot ! = true, seccompProfile
142
- namespace/local-path-storage labeled
143
- ```
126
+ ` ` ` shell
127
+ kubectl label --dry-run=server --overwrite ns --all \
128
+ pod-security.kubernetes.io/enforce=restricted
129
+ ` ` `
130
+ The output is similar to this:
131
+ ```
132
+ namespace/default labeled
133
+ namespace/kube-node-lease labeled
134
+ namespace/kube-public labeled
135
+ Warning: existing pods in namespace " kube-system" violate the new PodSecurity enforce level " restricted:latest"
136
+ Warning: coredns-7bb9c7b568-hsptc (and 1 other pod): unrestricted capabilities, runAsNonRoot ! = true, seccompProfile
137
+ Warning: etcd-psa-wo-cluster-pss-control-plane (and 3 other pods): host namespaces, hostPath volumes, allowPrivilegeEscalation ! = false, unrestricted capabilities, restricted volume types, runAsNonRoot ! = true
138
+ Warning: kindnet-vzj42: non-default capabilities, host namespaces, hostPath volumes, allowPrivilegeEscalation ! = false, unrestricted capabilities, restricted volume types, runAsNonRoot ! = true, seccompProfile
139
+ Warning: kube-proxy-m6hwf: host namespaces, hostPath volumes, privileged, allowPrivilegeEscalation ! = false, unrestricted capabilities, restricted volume types, runAsNonRoot ! = true, seccompProfile
140
+ namespace/kube-system labeled
141
+ Warning: existing pods in namespace " local-path-storage" violate the new PodSecurity enforce level " restricted:latest"
142
+ Warning: local-path-provisioner-d6d9f7ffc-lw9lh: allowPrivilegeEscalation ! = false, unrestricted capabilities, runAsNonRoot ! = true, seccompProfile
143
+ namespace/local-path-storage labeled
144
+ ```
144
145
145
146
From the previous output, you' ll notice that applying the `privileged` Pod Security Standard shows no warnings
146
147
for any namespaces. However, `baseline` and `restricted` standards both have
147
148
warnings, specifically in the `kube-system` namespace.
148
149
149
150
## Set modes, versions and standards
150
151
151
- In this tutorial , you apply the following Pod Security Standards to the `latest` version:
152
+ In this section , you apply the following Pod Security Standards to the `latest` version:
152
153
153
- * `baseline` standard in `enforce` mode.
154
- * `restricted` standard in `warn` and `audit` mode.
154
+ * `baseline` standard in `enforce` mode.
155
+ * `restricted` standard in `warn` and `audit` mode.
155
156
156
157
The `baseline` Pod Security Standard provides a convenient
157
158
middle ground that allows keeping the exemption list short and prevents known
@@ -166,37 +167,37 @@ following:
166
167
1. Based on the risk posture applied to a cluster, a stricter Pod Security
167
168
Standard like ` restricted` might be a better choice.
168
169
1. Exempting the ` kube-system` namespace allows pods to run as
169
- ` privileged` in this namespace. We recommend that you apply strict RBAC
170
+ ` privileged` in this namespace. For real world use, the Kubernetes project
171
+ strongly recommends that you apply strict RBAC
170
172
policies that limit access to ` kube-system` , following the principle of least
171
173
privilege.
172
-
174
+ To implement the preceding standards, do the following:
173
175
1. Create a configuration file that can be consumed by the Pod Security
174
- Admission Controller to implement these Pod Security Standards:
175
-
176
- ```
177
- mkdir -p /tmp/pss
178
- cat <<EOF > /tmp/pss/cluster-level-pss.yaml
179
- apiVersion: apiserver.config.k8s.io/v1
180
- kind: AdmissionConfiguration
181
- plugins:
182
- - name: PodSecurity
183
- configuration:
184
- apiVersion: pod-security.admission.config.k8s.io/v1beta1
185
- kind: PodSecurityConfiguration
186
- defaults:
187
- enforce: "baseline"
188
- enforce-version: "latest"
189
- audit: "restricted"
190
- audit-version: "latest"
191
- warn: "restricted"
192
- warn-version: "latest"
193
- exemptions:
194
- usernames: [ ]
195
- runtimeClasses: [ ]
196
- namespaces: [ kube-system]
197
- EOF
198
- ```
176
+ Admission Controller to implement these Pod Security Standards:
199
177
178
+ ```
179
+ mkdir -p /tmp/pss
180
+ cat << EOF > /tmp/pss/cluster-level-pss.yaml
181
+ apiVersion: apiserver.config.k8s.io/v1
182
+ kind: AdmissionConfiguration
183
+ plugins:
184
+ - name: PodSecurity
185
+ configuration:
186
+ apiVersion: pod-security.admission.config.k8s.io/v1beta1
187
+ kind: PodSecurityConfiguration
188
+ defaults:
189
+ enforce: "baseline"
190
+ enforce-version: "latest"
191
+ audit: "restricted"
192
+ audit-version: "latest"
193
+ warn: "restricted"
194
+ warn-version: "latest"
195
+ exemptions:
196
+ usernames: []
197
+ runtimeClasses: []
198
+ namespaces: [kube-system]
199
+ EOF
200
+ ` ` `
200
201
201
202
1. Configure the API server to consume this file during cluster creation:
202
203
@@ -234,22 +235,22 @@ EOF
234
235
EOF
235
236
` ` `
236
237
237
- {{<note>}}
238
- If you use Docker Desktop with KinD, the `/tmp`
239
- directory is added as a Shared Directory under
240
- **Preferences > Resources > File Sharing** on Mac OS .
241
- {{</note>}}
238
+ {{< note> }}
239
+ If you use Docker Desktop with KinD on macOS, you can
240
+ add ` /tmp ` as a Shared Directory under the menu item
241
+ ** Preferences > Resources > File Sharing** .
242
+ {{< /note> }}
242
243
243
- 2 . Create a cluster that uses Pod Security Admission to apply
244
+ 1 . Create a cluster that uses Pod Security Admission to apply
244
245
these Pod Security Standards:
245
246
246
247
` ` ` shell
247
- kind create cluster --name psa-with-cluster-pss --image kindest/node:latest --config /tmp/pss/cluster-config.yaml
248
+ kind create cluster --name psa-with-cluster-pss --image kindest/node:v1.23.0 --config /tmp/pss/cluster-config.yaml
248
249
` ` `
249
250
The output is similar to this:
250
251
` ` `
251
252
Creating cluster "psa-with-cluster-pss" ...
252
- ✓ Ensuring node image (kindest/node:latest ) 🖼
253
+ ✓ Ensuring node image (kindest/node:v1.23.0 ) 🖼
253
254
✓ Preparing nodes 📦
254
255
✓ Writing configuration 📜
255
256
✓ Starting control-plane 🕹️
@@ -261,17 +262,20 @@ EOF
261
262
kubectl cluster-info --context kind-psa-with-cluster-pss
262
263
263
264
Have a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/#community 🙂
264
-
265
- 3. Point kubectl to the cluster
265
+ ` ` `
266
+
267
+ 1. Point kubectl to the cluster
266
268
` ` ` shell
267
269
kubectl cluster-info --context kind-psa-with-cluster-pss
268
- Kubernetes control plane is running at https://127.0.0.1:63855
269
- CoreDNS is running at https://127.0.0.1:63855/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
270
-
271
- To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
272
270
` ` `
273
-
274
- 4. Create a Pod with minimal configuration in the default namespace:
271
+ The output is similar to this:
272
+ ` ` `
273
+ Kubernetes control plane is running at https://127.0.0.1:63855
274
+ CoreDNS is running at https://127.0.0.1:63855/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
275
+
276
+ To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
277
+ ` ` `
278
+ 1. Create the following Pod specification for a minimal configuration in the default namespace:
275
279
276
280
` ` `
277
281
cat << EOF > /tmp/pss/nginx-pod.yaml
@@ -287,33 +291,34 @@ EOF
287
291
- containerPort: 80
288
292
EOF
289
293
` ` `
290
- 5 . Create Pod after pod security is enabled at cluster level :
291
-
294
+ 1 . Create the Pod in the cluster:
295
+
292
296
` ` ` shell
293
297
kubectl apply -f /tmp/pss/nginx-pod.yaml
294
298
` ` `
295
299
The output is similar to this:
296
300
` ` `
297
301
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")
298
302
pod/nginx created
299
- ```
303
+ ` ` `
304
+
300
305
# # Clean up
301
306
302
- Run `kind delete cluster -name psa-with-cluster-pss` and
303
- `kind delete cluster -name psa-wo-cluster-pss` to delete the clusters you
307
+ Run ` kind delete cluster -name psa-with-cluster-pss` and
308
+ ` kind delete cluster -name psa-wo-cluster-pss` to delete the clusters you
304
309
created.
305
310
306
311
# # {{% heading "whatsnext" %}}
307
312
308
- - Run a
309
- [gist](https://gist.github.com/PushkarJ/9f7a0045f4bec31097bdd1e9db0f2f6e)
313
+ - Run a
314
+ [shell script](/examples/security/kind-with-cluster-level-baseline-pod-security.sh)
310
315
to perform all the preceding steps at once:
311
- 1. Create a Pod Security Standards based cluster level Configuration
312
- 2. Create a file to let API server consumes this configuration
313
- 3. Create a cluster that creates an API server with this configuration
314
- 4. Set kubectl context to this new cluster
315
- 5. Create a minimal pod yaml file
316
- 6. Apply this file to create a Pod in the new cluster
316
+ 1. Create a Pod Security Standards based cluster level Configuration
317
+ 2. Create a file to let API server consumes this configuration
318
+ 3. Create a cluster that creates an API server with this configuration
319
+ 4. Set kubectl context to this new cluster
320
+ 5. Create a minimal pod yaml file
321
+ 6. Apply this file to create a Pod in the new cluster
317
322
- [Pod Security Admission](/docs/concepts/security/pod-security-admission/)
318
323
- [Pod Security Standards](/docs/concepts/security/pod-security-standards/)
319
- - [Applying Pod Security Standards at the namespace level](/docs/tutorials/security/ns-level-pss/)
324
+ - [Apply Pod Security Standards at the namespace level](/docs/tutorials/security/ns-level-pss/)
0 commit comments