Goal
Turn on Pod Security Admission for a namespace in three steps (warn, audit, enforce), watch a typical "works on my laptop" pod get rejected, and rewrite it until it passes the restricted level: non-root, no privilege escalation, all capabilities dropped, a seccomp profile and a read-only root file system.
Before you start
kubectl versionshows a server of 1.25 or newer (Pod Security Admission is built in and on).- Save this pod as
bad-pod.yaml:bad-pod.yamlapiVersion: v1 kind: Pod metadata: name: web spec: containers: - name: web image: nginx:1.27 ports: [{containerPort: 80}] securityContext: privileged: true
Tasks
Tick a task when it is done. Only your browser remembers it.
- Make the namespace
secure-appswith the labelswarn=restrictedandaudit=restrictedonly. Apply the bad pod and read the warnings. - Delete the pod and set
enforce=restrictedwith a pinned version (enforce-version=v1.30or your version). - Apply the bad pod again and read why it is rejected.
- Rewrite it as
good-pod.yamlso it passes restricted. nginx must still serve pages (it needs a non-root image or port above 1024 and writable temp folders). - Check with a server-side dry run that existing pods in the
defaultnamespace would passbaseline.
Check your work
Hints
Show the hints
- Labels:
pod-security.kubernetes.io/<mode>=<level>andpod-security.kubernetes.io/<mode>-version=<v>. - Restricted needs:
runAsNonRoot: true,allowPrivilegeEscalation: false,capabilities: drop: [ALL],seccompProfile: type: RuntimeDefault, and no privileged, hostPath, hostNetwork or hostPort. nginxinc/nginx-unprivilegedlistens on 8080 and runs as user 101.- With
readOnlyRootFilesystem: true, give nginxemptyDirvolumes for/tmp(and its cache folders). kubectl label --dry-run=server --overwrite ns default pod-security.kubernetes.io/enforce=baselineshows warnings for pods that would fail.
Solution
Show the solution
- Warn and audit first.
kubectl create ns secure-apps kubectl label ns secure-apps pod-security.kubernetes.io/warn=restricted pod-security.kubernetes.io/audit=restricted kubectl -n secure-apps apply -f bad-pod.yaml Warning: would violate PodSecurity "restricted:latest": privileged (container "web" must not set securityContext.privileged=true), allowPrivilegeEscalation != false, unrestricted capabilities, runAsNonRoot != true, seccompProfile pod/web created - Enforce.
kubectl -n secure-apps delete pod web kubectl label ns secure-apps pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=v1.30 kubectl -n secure-apps apply -f bad-pod.yaml Error from server (Forbidden): error when creating "bad-pod.yaml": pods "web" is forbidden: violates PodSecurity "restricted:v1.30": privileged ... - The fixed pod.good-pod.yaml
apiVersion: v1 kind: Pod metadata: name: web spec: securityContext: runAsNonRoot: true runAsUser: 101 seccompProfile: type: RuntimeDefault containers: - name: web image: nginxinc/nginx-unprivileged:1.27-alpine ports: [{containerPort: 8080}] securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true capabilities: drop: [ALL] volumeMounts: - {name: tmp, mountPath: /tmp} volumes: - {name: tmp, emptyDir: {}} - Test another namespace without changing it.
kubectl label --dry-run=server --overwrite ns default pod-security.kubernetes.io/enforce=baseline
My notes
No notes yet.