Skip to content
Hands-on IT IT Drills

Day 9: Pod Security Standards and SecurityContext

Goal: You enforce Pod Security Standards via namespace labels, read violations and make workloads restricted-ready. · Duration: approx. 60 min · Prerequisite: Day 8

Briefing

  • CKS: Minimize Microservice Vulnerabilities – “Use appropriate pod security standards”.
  • Pod Security Admission (PSA) is built into the API server and active by default. It is controlled per namespace via labels – no webhook, no CRD.
  • Three levels: privileged (everything), baseline (no host access, no privileged), restricted (additionally non-root, drop ALL, seccomp).
  • Three modes: enforce (reject), warn (warning to the client), audit (note in the audit log, Day 15). Every mode has a -version label.
pod-security.kubernetes.io/<mode>=<level>
pod-security.kubernetes.io/<mode>-version=v1.37 | latest
mode: enforce | warn | audit     level: privileged | baseline | restricted

Preparation:

mkdir -p ~/cks/tag09 && cd ~/cks/tag09
k create ns tag09
k config set-context --current --namespace=tag09
curl -s http://172.16.0.126:30500/v2/notes-api/tags/list

Expected: {"name":"notes-api","tags":["1.0"]}

If the image is missing (the registry uses an emptyDir and forgets everything on restart), build it from the companion repo:

cd ~/cks
git clone https://github.com/thomaszachmann/cks-zum-anfassen
cd cks-zum-anfassen/notes-api
cat > Dockerfile <<'EOF'
FROM docker.io/library/golang:1.27-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
ARG VERSION=dev
RUN CGO_ENABLED=0 go build -ldflags="-s -w -X main.version=${VERSION}" \
    -o /out/notes-api .

FROM docker.io/library/alpine:3.23
COPY --from=build /out/notes-api /usr/local/bin/notes-api
USER 65532:65532
EXPOSE 8080
ENTRYPOINT ["notes-api"]
EOF
podman build -t 172.16.0.126:30500/notes-api:1.0 --build-arg VERSION=1.0 .
podman push --tls-verify=false 172.16.0.126:30500/notes-api:1.0
cd ~/cks/tag09

Drill 1: The insecure starting point

A typical legacy setup: the app without any SecurityContext and a “debug Pod” with full access to the node.

k create secret generic notes-key --from-literal=NOTES_API_KEY=lab-key-123
k create deploy notes --image=172.16.0.126:30500/notes-api:1.0
k set env deploy/notes --from=secret/notes-key
cat > toolbox.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: toolbox
spec:
  hostPID: true
  containers:
  - name: c
    image: busybox:1.37
    command: ["sleep", "infinity"]
    securityContext:
      privileged: true
EOF
k apply -f toolbox.yaml
k rollout status deploy/notes
k wait --for=condition=Ready pod/toolbox
k get pods
k exec toolbox -- ps -o pid,comm | head -3

Expected:

NAME                     READY   STATUS    RESTARTS   AGE
notes-7d6b8b9c78-fd9c2   1/1     Running   0          19s
toolbox                  1/1     Running   0          18s
PID   COMMAND
    1 systemd
    2 kthreadd

PID 1 is the node’s systemd: with hostPID the container sees (and with privileged influences) every process on the host, including the kubelet and containerd. Without labels, the namespace allows this – it behaves like privileged.

Save your progress? Sign up for free

Drill 2: Warn first, then enforce

warn and audit block nothing. That way you see what restricted would break before it hurts:

k label ns tag09 \
  pod-security.kubernetes.io/warn=restricted \
  pod-security.kubernetes.io/warn-version=v1.37 \
  pod-security.kubernetes.io/audit=restricted \
  pod-security.kubernetes.io/audit-version=v1.37
k run probe --image=busybox:1.37 -- sleep 1d

Expected:

namespace/tag09 labeled
Warning: would violate PodSecurity "restricted:v1.37": allowPrivilegeEscalation != false
(container "probe" must set securityContext.allowPrivilegeEscalation=false),
unrestricted capabilities (container "probe" must set
securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container
"probe" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container
"probe" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")
pod/probe created

The warning is the checklist: every parenthesis names the field and the required value. audit writes the same information as the annotation pod-security.kubernetes.io/audit-violations into the audit log – which is only switched on from Day 15.

Save your progress? Sign up for free

Drill 3: Check existing Pods with –dry-run=server

Before you set enforce, ask the API server which running Pods would violate the new level:

k label --dry-run=server --overwrite ns tag09 \
  pod-security.kubernetes.io/enforce=restricted \
  pod-security.kubernetes.io/enforce-version=v1.37
k label --dry-run=server --overwrite ns tag09 \
  pod-security.kubernetes.io/enforce=baseline \
  pod-security.kubernetes.io/enforce-version=v1.37

Expected:

Warning: existing pods in namespace "tag09" violate the new PodSecurity enforce level
"restricted:v1.37"
Warning: notes-7d6b8b9c78-fd9c2 (and 1 other pod): allowPrivilegeEscalation != false,
unrestricted capabilities, runAsNonRoot != true, seccompProfile
Warning: toolbox: host namespaces, privileged, allowPrivilegeEscalation != false,
unrestricted capabilities, runAsNonRoot != true, seccompProfile
namespace/tag09 labeled (server dry run)
Warning: existing pods in namespace "tag09" violate the new PodSecurity enforce level
"baseline:v1.37"
Warning: toolbox: host namespaces, privileged
namespace/tag09 labeled (server dry run)

baseline only bothers toolbox, restricted all of them. The same for the whole cluster (changes nothing, only a probe):

k label --dry-run=server --overwrite ns --all \
  pod-security.kubernetes.io/enforce=baseline

Expected (shortened):

namespace/default labeled (server dry run)
Warning: existing pods in namespace "kube-system" violate the new PodSecurity enforce
level "baseline:latest"
Warning: cilium-kf854 (and 2 other pods): forbidden AppArmor profile, non-default
capabilities, host namespaces, hostPath volumes, hostPort, probe or lifecycle host,
privileged, seLinuxOptions, seccompProfile
Warning: etcd-cp1 (and 3 other pods): host namespaces, hostPath volumes, hostPort, probe
or lifecycle host
Warning: kube-proxy-7b5pz (and 2 other pods): host namespaces, hostPath volumes,
privileged
namespace/kube-system labeled (server dry run)
...

kube-system needs host access (CNI, kube-proxy, static Pods) – there it stays privileged.

Save your progress? Sign up for free

Drill 4: Enforce baseline

k label --overwrite ns tag09 \
  pod-security.kubernetes.io/enforce=baseline \
  pod-security.kubernetes.io/enforce-version=v1.37
k get pod toolbox
sed 's/name: toolbox/name: toolbox2/' toolbox.yaml | k apply -f -
k run hn --image=busybox:1.37 --overrides='{"spec":{"hostNetwork":true}}' -- sleep 1d

Expected:

Warning: existing pods in namespace "tag09" violate the new PodSecurity enforce level
"baseline:v1.37"
Warning: toolbox: host namespaces, privileged
namespace/tag09 labeled
NAME      READY   STATUS    RESTARTS   AGE
toolbox   1/1     Running   0          39s
Error from server (Forbidden): error when creating "STDIN": pods "toolbox2" is
forbidden: violates PodSecurity "baseline:v1.37": host namespaces (hostPID=true),
privileged (container "c" must not set securityContext.privileged=true)
Error from server (Forbidden): pods "hn" is forbidden: violates PodSecurity
"baseline:v1.37": host namespaces (hostNetwork=true)

Trap: PSA only checks at creation time. toolbox keeps running – you have to remove it yourself. A hostPath volume fails the same way with hostPath volumes (volume "…").

k delete pod toolbox probe --now

Save your progress? Sign up for free

Drill 5: restricted – Deployment there, Pods not

k create ns tag09-strict
k label ns tag09-strict \
  pod-security.kubernetes.io/enforce=restricted \
  pod-security.kubernetes.io/enforce-version=v1.37
k -n tag09-strict create secret generic notes-key \
  --from-literal=NOTES_API_KEY=lab-key-123
k -n tag09-strict create deploy notes --image=172.16.0.126:30500/notes-api:1.0
k -n tag09-strict set env deploy/notes --from=secret/notes-key
k -n tag09-strict get deploy notes
k -n tag09-strict get events --field-selector reason=FailedCreate \
  -o jsonpath='{.items[0].message}{"\n"}'

Expected (shortened):

Warning: would violate PodSecurity "restricted:v1.37": allowPrivilegeEscalation != false
...
deployment.apps/notes created
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
notes   0/1     0            0           3s
Error creating: pods "notes-7d6b8b9c78-wfvf8" is forbidden: violates PodSecurity
"restricted:v1.37": allowPrivilegeEscalation != false (container "notes-api" must set
securityContext.allowPrivilegeEscalation=false), ...

The Deployment itself is created (only a warning); only the ReplicaSet controller fails at the Pod. In the exam, the symptom is “0/1 and no Pod” – the cause is in the events or in k describe rs.

Save your progress? Sign up for free

Drill 6: Fix the violations

The four points from the warning, as a patch on the Pod template:

cat > fix.yaml <<'EOF'
spec:
  template:
    spec:
      securityContext:
        runAsNonRoot: true
        seccompProfile:
          type: RuntimeDefault
      containers:
      - name: notes-api
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop: ["ALL"]
EOF
k -n tag09-strict patch deploy notes --patch-file fix.yaml
k -n tag09-strict rollout status deploy/notes
k -n tag09-strict exec deploy/notes -- \
  grep -E '^Uid|CapEff|NoNewPrivs|Seccomp:' /proc/1/status
k -n tag09-strict exec deploy/notes -- wget -qO- localhost:8080/healthz

Expected:

deployment.apps/notes patched
deployment "notes" successfully rolled out
Uid:    65532   65532   65532   65532
CapEff: 0000000000000000
NoNewPrivs: 1
Seccomp:    2
ok

Every value is proven: non-root (65532), no capabilities, no_new_privs, seccomp filter active. The strategic merge patch merges the fields via the container name; by the way, restricted does not require readOnlyRootFilesystem (Day 18).

Save your progress? Sign up for free

Drill 7: runAsNonRoot – admission says yes, the kubelet says no

cat > shell.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: shell
spec:
  securityContext:
    runAsNonRoot: true
    seccompProfile:
      type: RuntimeDefault
  containers:
  - name: c
    image: busybox:1.37
    command: ["sleep", "1d"]
    securityContext:
      allowPrivilegeEscalation: false
      capabilities:
        drop: ["ALL"]
EOF
k -n tag09-strict apply -f shell.yaml
k -n tag09-strict get pod shell -w   # Ctrl+C once the error status shows
k -n tag09-strict describe pod shell | grep -m1 'Error:'

Expected (shortened):

pod/shell created
NAME    READY   STATUS                       RESTARTS   AGE
shell   0/1     CreateContainerConfigError   0          7s
  ... Error: container has runAsNonRoot and image will run as root (pod:
  "shell_tag09-strict(b26d…)", container: c)

PSA only checks whether runAsNonRoot: true is set. Whether the image really starts as non-root is noticed only by the kubelet. busybox has no USER – so specify a numeric user:

k -n tag09-strict delete pod shell --now
sed -i 's/^    runAsNonRoot: true/&\n    runAsUser: 1000/' shell.yaml
k -n tag09-strict apply -f shell.yaml
k -n tag09-strict wait --for=condition=Ready pod/shell
k -n tag09-strict exec shell -- id

Expected:

pod/shell condition met
uid=1000 gid=0(root) groups=0(root)

Save your progress? Sign up for free

Drill 8: Find dangerous Pods in the cluster

Without PSA labels you have to search yourself. jq is usually available in the exam; otherwise -o custom-columns helps (Speed Drill 3):

k get pods -A -o json | jq -r '.items[]
  | select(.spec.hostNetwork or .spec.hostPID or .spec.hostIPC)
  | .metadata.namespace + "/" + .metadata.name'
k get pods -A -o json | jq -r '.items[]
  | select(any(.spec.containers[], .spec.initContainers[]?;
      .securityContext.privileged))
  | .metadata.namespace + "/" + .metadata.name'
k get pods -A -o json | jq -r '.items[]
  | select(any(.spec.volumes[]?; .hostPath))
  | .metadata.namespace + "/" + .metadata.name'

Expected (shortened, system Pods only – your tag09 Pods are clean):

kube-system/cilium-envoy-7kl7t
kube-system/cilium-kf854
kube-system/etcd-cp1
kube-system/kube-proxy-7b5pz
...
kube-system/cilium-kf854
kube-system/kube-proxy-7b5pz
...

initContainers count too: with Cilium, privileged is set only there. And where does PSA come from in the first place?

sudo grep admission /etc/kubernetes/manifests/kube-apiserver.yaml

Expected: - --enable-admission-plugins=NodeRestriction

PodSecurity is not listed there because it is one of the default plugins (k -n kube-system exec kube-apiserver-cp1 -- kube-apiserver -h | grep enable-admission-plugins lists them). Cluster-wide defaults and exemptions (namespaces, users, RuntimeClasses) are set by an AdmissionConfiguration with kind: PodSecurityConfiguration, included via --admission-control-config-file. That is an API server intervention like on Day 6 – here for your knowledge only, do not run it.

Save your progress? Sign up for free

Exercise: Speed Drills

  1. Namespace tag09-team: enforce baseline, warn and audit restricted, all versions pinned to v1.37. Target time: 1 min
  2. Check which Pods in tag09 violate restricted, without changing anything. Target time: 30 s
  3. List all Pods in the cluster with hostNetwork: true – without jq. Target time: 1 min
  4. Pod sleeper (busybox:1.37, sleep 1d) runs in tag09-strict. Target time: 2 min

Solution

k create ns tag09-team
k label ns tag09-team \
  pod-security.kubernetes.io/enforce=baseline \
  pod-security.kubernetes.io/enforce-version=v1.37 \
  pod-security.kubernetes.io/warn=restricted \
  pod-security.kubernetes.io/warn-version=v1.37 \
  pod-security.kubernetes.io/audit=restricted \
  pod-security.kubernetes.io/audit-version=v1.37
k label --dry-run=server --overwrite ns tag09 \
  pod-security.kubernetes.io/enforce=restricted
k get pods -A -o custom-columns=\
NS:.metadata.namespace,NAME:.metadata.name,HOSTNET:.spec.hostNetwork | grep -E 'NS|true'
k run sleeper --image=busybox:1.37 $do -- sleep 1d > sleeper.yaml

Extend sleeper.yaml (Pod and container level):

spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1000
    seccompProfile:
      type: RuntimeDefault
  containers:
  - name: sleeper
    image: busybox:1.37
    args: ["sleep", "1d"]
    securityContext:
      allowPrivilegeEscalation: false
      capabilities:
        drop: ["ALL"]
k -n tag09-strict apply -f sleeper.yaml
k -n tag09-strict wait --for=condition=Ready pod/sleeper

Expected (shortened):

namespace/tag09-team labeled
Warning: existing pods in namespace "tag09" violate the new PodSecurity enforce level
"restricted:v1.37"
Warning: notes-7d6b8b9c78-fd9c2: allowPrivilegeEscalation != false, unrestricted
capabilities, runAsNonRoot != true, seccompProfile
namespace/tag09 labeled (server dry run)
NS                   NAME                                      HOSTNET
kube-system          cilium-envoy-7kl7t                        true
...
pod/sleeper condition met

In task 2, restricted:v1.37 applies even though you do not specify a version: the existing label enforce-version=v1.37 stays in place.

Save your progress? Sign up for free

Checkpoint

k get ns -l pod-security.kubernetes.io/enforce=restricted

Expected: tag09-strict is included.

k get ns tag09-strict \
  -o jsonpath='{.metadata.labels.pod-security\.kubernetes\.io/enforce-version}{"\n"}'

Expected: v1.37

k -n tag09-strict get deploy notes

Expected: notes 1/1 1 1 …

k get pod toolbox

Expected: Error from server (NotFound): pods "toolbox" not found

Challenge

Install the legacy setup (below). Afterwards, namespace tag09-ch should enforce restricted in version v1.37 and the Deployment legacy should still run – without the host network, without a host path, without privileged.

k create ns tag09-ch
k -n tag09-ch create secret generic notes-key \
  --from-literal=NOTES_API_KEY=lab-key-123
cat > legacy.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: legacy
spec:
  selector:
    matchLabels:
      app: legacy
  template:
    metadata:
      labels:
        app: legacy
    spec:
      hostNetwork: true
      containers:
      - name: notes-api
        image: 172.16.0.126:30500/notes-api:1.0
        env:
        - name: PORT
          value: "18080"
        envFrom:
        - secretRef:
            name: notes-key
        securityContext:
          privileged: true
        volumeMounts:
        - name: logs
          mountPath: /host-logs
      volumes:
      - name: logs
        hostPath:
          path: /var/log
EOF
k -n tag09-ch apply -f legacy.yaml

Hint: First get the list of violations with --dry-run=server, then set the label – that does not remove the running Pod; only the rollout counts.

Solution

k label --dry-run=server --overwrite ns tag09-ch \
  pod-security.kubernetes.io/enforce=restricted \
  pod-security.kubernetes.io/enforce-version=v1.37

Expected:

Warning: existing pods in namespace "tag09-ch" violate the new PodSecurity enforce level
"restricted:v1.37"
Warning: legacy-6b55476555-2kbmg: host namespaces, privileged, allowPrivilegeEscalation
!= false, unrestricted capabilities, restricted volume types, runAsNonRoot != true,
seccompProfile
namespace/tag09-ch labeled (server dry run)

restricted volume types: only configMap, csi, downwardAPI, emptyDir, ephemeral, persistentVolumeClaim, projected and secret are allowed. Set the label, then in legacy.yaml delete hostNetwork, privileged, volumeMounts and volumes, and add the familiar fields:

k label --overwrite ns tag09-ch \
  pod-security.kubernetes.io/enforce=restricted \
  pod-security.kubernetes.io/enforce-version=v1.37
    spec:
      securityContext:
        runAsNonRoot: true
        seccompProfile:
          type: RuntimeDefault
      containers:
      - name: notes-api
        image: 172.16.0.126:30500/notes-api:1.0
        env:
        - name: PORT
          value: "18080"
        envFrom:
        - secretRef:
            name: notes-key
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop: ["ALL"]
k -n tag09-ch apply -f legacy.yaml
k -n tag09-ch rollout status deploy/legacy
k label --dry-run=server --overwrite ns tag09-ch \
  pod-security.kubernetes.io/enforce-version=latest

Expected:

deployment.apps/legacy configured
deployment "legacy" successfully rolled out
namespace/tag09-ch labeled (server dry run)

No more warnings. Why enforce-version=latest as the probe? If you set the same label with the same value, kubectl reports not labeled and PSA checks nothing at all – the probe needs a real change.

Clean up:

k config set-context --current --namespace=default
k delete ns tag09 tag09-strict tag09-team tag09-ch

Save your progress? Sign up for free

Exam Tip

  • The API server rejects wrong values (must be one of privileged, baseline, restricted, version must be "latest" or "v1.x"). A typo in the key (…/enforcee), on the other hand, is accepted silently – check with --show-labels afterwards.
  • After enforce, old Pods keep running. If the task demands “no violating Pods”, recreate the Pods (k rollout restart) or delete them.
  • Docs: kubernetes.io → Concepts → Security → “Pod Security Standards” (field list per level) and Tasks → “Enforce Pod Security Standards with Namespace Labels”.

Enterprise Note

  • Pin the version (v1.37) so that a cluster upgrade does not block workloads overnight; before raising it, test with --dry-run=server and warn on the new version.
  • Cluster default baseline enforce plus restricted warn/audit via AdmissionConfiguration, exemptions only for system namespaces.
  • Whoever may create or label namespaces can switch off PSA – keep namespaces patch and update tight via RBAC (Day 5).
  • For finer-grained rules (allowed registries, mandatory labels), add Kyverno, Gatekeeper or ValidatingAdmissionPolicy.