Advanced
Searching inLabs
How to search
  • Search a task or a tool (lvm, selinux, @podman); filter by level and by book or course.
  • With an empty search box, a filter alone lists the labs — for example all hard labs of one course.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Lab 4.1CKS, Chapter 4

Enforce the restricted Pod Security Standard and fix a rejected pod

Level
Medium
35 min
Machines
a Kubernetes cluster 1.25 or newer (kind, minikube or kubeadm)
Status
Not started

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 version shows a server of 1.25 or newer (Pod Security Admission is built in and on).
  • Save this pod as bad-pod.yaml:
    bad-pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: web
    spec:
      containers:
        - name: web
          image: nginx:1.27
          ports: [{containerPort: 80}]
          securityContext:
            privileged: true

Tasks

  1. Make the namespace secure-apps with the labels warn=restricted and audit=restricted only. Apply the bad pod and read the warnings.
  2. Delete the pod and set enforce=restricted with a pinned version (enforce-version=v1.30 or your version).
  3. Apply the bad pod again and read why it is rejected.
  4. Rewrite it as good-pod.yaml so it passes restricted. nginx must still serve pages (it needs a non-root image or port above 1024 and writable temp folders).
  5. Check with a server-side dry run that existing pods in the default namespace would pass baseline.

Check your work

kubectl get ns secure-apps --show-labels
kubectl -n secure-apps apply -f good-pod.yaml
kubectl -n secure-apps get pod web
kubectl -n secure-apps exec web -- id
kubectl -n secure-apps port-forward pod/web 8080:8080 & sleep 2; curl -s localhost:8080 | head -4; kill %1
  • The namespace has pod-security.kubernetes.io/enforce=restricted.
  • bad-pod.yaml is rejected with violates PodSecurity "restricted:...".
  • good-pod.yaml is Running; id shows a user that is not 0; curl shows the nginx welcome page.

Hints

Show the hints
  • Labels: pod-security.kubernetes.io/<mode>=<level> and pod-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-unprivileged listens on 8080 and runs as user 101.
  • With readOnlyRootFilesystem: true, give nginx emptyDir volumes for /tmp (and its cache folders).
  • kubectl label --dry-run=server --overwrite ns default pod-security.kubernetes.io/enforce=baseline shows warnings for pods that would fail.

Solution

Show the solution
  1. 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
  2. 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 ...
  3. 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: {}}
    kubectl -n secure-apps apply -f good-pod.yaml
    kubectl -n secure-apps exec web -- id
    uid=101(nginx) gid=101(nginx) groups=101(nginx)
  4. 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.

Lessons for this lab

Commands in this lab