Skip to content
Hands-on IT IT Drills

Appendix B: Practice Exam

Goal: You solve 17 CKS-style tasks in 120 minutes and grade yourself. · Duration: 120 min · Prerequisite: Days 0–19

Rules of Play

  • Time: 120 minutes, set a timer. Each task has a weight in percent, adding up to 100 %, distributed like the CKS curriculum domains (15/15/10/20/20/20 %). Here you pass with 67 % – as things stand, that is also the threshold of the real exam; the authoritative source is https://training.linuxfoundation.org.
  • Allowed: the Kubernetes docs (kubernetes.io/docs, kubernetes.io/blog), k explain and the tools’ -h. In the real exam, the docs of individual tools are added (such as falco.org/docs) – exactly which ones is stated in the current exam instructions of the Linux Foundation. Not allowed: this book during the exam time.
  • Each task has its own namespace pruef-NN. Pass it with -n in every command – as in the exam, where each task requires a different context.
  • Files go into ~/cks/pruef. Tasks 2, 6 and 15 change the control plane: back up first, afterwards run k get --raw /readyz and k get nodes.
  • The solutions are at the end. Only read them after the time is up.

Preparation

All commands on cp1. Namespaces, working directory, backup folder:

mkdir -p ~/cks/pruef/backup ~/cks/pruef/03 && cd ~/cks/pruef
for i in $(seq -w 1 17); do k create ns pruef-$i; done

For Task 1, an API and two clients:

k create deployment api -n pruef-01 --image=172.16.0.126:30500/notes-api:1.0 \
  --port=8080
k set env deploy/api -n pruef-01 NOTES_API_KEY=lab-key-123
k expose deploy api -n pruef-01 --port=8080
k run web -n pruef-01 --image=busybox:1.37 -l app=web -- sleep 3600
k run test -n pruef-01 --image=busybox:1.37 -- sleep 3600

For Task 2, three deviations from the CIS Benchmark:

sudo chmod 644 /etc/kubernetes/manifests/kube-scheduler.yaml
sudo chmod 644 /etc/kubernetes/controller-manager.conf
sudo chown root:root /var/lib/etcd

For Task 3, a shop without TLS and two binaries, one of them modified:

k create deployment shop -n pruef-03 --image=nginx:1.29-alpine-slim --port=80
k expose deploy shop -n pruef-03 --port=80
cd ~/cks/pruef/03
ARCH=$(dpkg --print-architecture)
U=https://dl.k8s.io/release/v1.37.2/bin/linux/${ARCH}
curl -sSLO $U/kubectl
curl -sSLO $U/kubeadm
echo >> kubeadm
chmod +x kubectl kubeadm
cd ~/cks/pruef

For Task 4, a ServiceAccount with full access in the namespace:

k create sa ci -n pruef-04
k create role ci -n pruef-04 --verb='*' --resource='*.*'
k create rolebinding ci -n pruef-04 --role=ci --serviceaccount=pruef-04:ci
k create configmap app-config -n pruef-04 --from-literal=LOG_LEVEL=info
k create configmap other -n pruef-04 --from-literal=x=1
k create secret generic db -n pruef-04 --from-literal=password=Lab-Pruef-04

For Task 5, a frontend with the default ServiceAccount:

k create deployment frontend -n pruef-05 --image=busybox:1.37 -- sleep 3600

For Task 6, a hole in the API server (backup first, then wait 1–2 min until k get --raw /readyz reports ok again):

k create sa viewer -n pruef-06
M=/etc/kubernetes/manifests
sudo cp $M/kube-apiserver.yaml ~/cks/pruef/backup/kube-apiserver.yaml.orig
sudo sed -i 's/--authorization-mode=Node,RBAC$/&,AlwaysAllow/' $M/kube-apiserver.yaml

For Tasks 7 and 8, an archive service and a build Pod:

k create deployment archiv -n pruef-07 --image=busybox:1.37 -- sleep 3600
cat > 08-builder.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: builder
  namespace: pruef-08
spec:
  securityContext:
    seccompProfile:
      type: Localhost
      localhostProfile: no-mkdir.json
  containers:
  - name: builder
    image: busybox:1.37
    command: ["sleep", "3600"]
EOF
k apply -f 08-builder.yaml

For Task 9, an API and a debug Pod with a view of the host processes:

k create deployment api -n pruef-09 --image=172.16.0.126:30500/notes-api:1.0 \
  --port=8080
k set env deploy/api -n pruef-09 NOTES_API_KEY=lab-key-123
k apply -f - <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: debug
  namespace: pruef-09
spec:
  selector:
    matchLabels:
      app: debug
  template:
    metadata:
      labels:
        app: debug
    spec:
      hostPID: true
      containers:
      - name: busybox
        image: busybox:1.37
        command: ["sleep", "3600"]
EOF

For Task 10, a password in a ConfigMap (Lab-Pruef-10 is a pure lab value):

k create configmap db-config -n pruef-10 --from-literal=DB_HOST=db.pruef-10 \
  --from-literal=DB_PASSWORD=Lab-Pruef-10
k create deployment app -n pruef-10 --image=busybox:1.37 -- sleep 3600
k set env deploy/app -n pruef-10 --from=configmap/db-config

For Task 11, a parser for untrusted files:

k apply -f - <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: parser
  namespace: pruef-11
spec:
  selector:
    matchLabels:
      app: parser
  template:
    metadata:
      labels:
        app: parser
    spec:
      runtimeClassName: sandbox
      containers:
      - name: busybox
        image: busybox:1.37
        command: ["sleep", "3600"]
EOF

For Task 12, three Deployments with images of different ages:

k create deployment cache -n pruef-12 --image=redis:7.0.0-alpine
k create deployment web -n pruef-12 --image=httpd:2.4.54-alpine
k create deployment api -n pruef-12 --image=172.16.0.126:30500/notes-api:1.0
k set env deploy/api -n pruef-12 NOTES_API_KEY=lab-key-123

For Task 13, a manifest that has not been applied yet:

cat > 13-api.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  namespace: pruef-13
spec:
  replicas: 1
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
      - name: notes-api
        image: 172.16.0.126:30500/notes-api:1.0
        env:
        - name: NOTES_API_KEY
          value: lab-key-123
        securityContext:
          privileged: true
          capabilities:
            add: ["SYS_ADMIN"]
EOF

For Task 14, a Deployment with latest:

k create deployment web -n pruef-14 --image=nginx:latest

For Task 15, a ConfigMap disappears:

k create configmap feature-flags -n pruef-15 --from-literal=beta=true
k create sa cleanup -n pruef-15
k create role cleanup -n pruef-15 --verb=get,list,delete --resource=configmaps
k create rolebinding cleanup -n pruef-15 --role=cleanup \
  --serviceaccount=pruef-15:cleanup
T=$(k create token cleanup -n pruef-15 --duration=10m)
curl -s --cacert /etc/kubernetes/pki/ca.crt -H "Authorization: Bearer $T" -X DELETE \
  https://172.16.0.126:6443/api/v1/namespaces/pruef-15/configmaps/feature-flags \
  > /dev/null

For Task 16, two Deployments, one of them running a package manager in a loop:

k create deployment updater -n pruef-16 --image=alpine:3.23 -- \
  sh -c 'while true; do apk --version > /dev/null; sleep 15; done'
k create deployment web -n pruef-16 --image=nginx:1.29-alpine-slim

For Task 17, three Deployments, one of them with a stowaway:

k create deployment api -n pruef-17 --image=172.16.0.126:30500/notes-api:1.0
k set env deploy/api -n pruef-17 NOTES_API_KEY=lab-key-123
k create deployment web -n pruef-17 --image=nginx:1.29-alpine-slim
k expose deploy web -n pruef-17 --port=80
k create deployment metrics -n pruef-17 --image=busybox:1.37 -- sh -c \
  'mkdir -p /tmp/.x && printf "#!/bin/sh\nnc -lk -p 4444\n" > /tmp/.x/kworkerd &&
  chmod +x /tmp/.x/kworkerd && /tmp/.x/kworkerd & sleep 3600'

Wait one minute until everything is running. Start the clock.

Task 1: Default deny with an exception (6 %)

Namespace pruef-01. No Pod in the namespace may accept or open connections – default deny applies in both directions. Exceptions: all Pods may reach DNS in kube-system, and Pods with app=web may reach the Pods of api on TCP 8080. Test with web and test.

Task 2: CIS Benchmark (5 %)

Check the control plane on cp1 with kube-bench (benchmark cis-1.12). Fix every FAIL in group 1.1 (control plane files) and prove that these checks pass now. The other groups stay as they are.

Task 3: TLS at the Ingress, verify binaries (4 %)

Namespace pruef-03. Make the Service shop reachable through Traefik at https://shop.pruef.lab:30443 – with a self-signed certificate for shop.pruef.lab in the TLS Secret shop-tls. curl with --cacert returns 200. In ~/cks/pruef/03 there are kubectl and kubeadm v1.37.2: delete the binary that does not match the official release.

Task 4: Minimize RBAC (6 %)

Namespace pruef-04. The ServiceAccount ci may only read Pods (get, list, watch) and fetch the ConfigMap app-config (get) – no other ConfigMap, no Secrets, nothing else. Prove it with k auth can-i.

Task 5: No token in the frontend (4 %)

Namespace pruef-05. The Deployment frontend never talks to the Kubernetes API. It should use its own ServiceAccount frontend and no longer get a token mounted. New Pods with the namespace’s default ServiceAccount don’t get one either.

Task 6: Who allowed that? (5 %)

The ServiceAccount viewer in pruef-06 has not a single role binding, yet it may read Secrets cluster-wide. Find the cause at the API server and fix it without changing other flags.

Task 7: AppArmor (5 %)

Namespace pruef-07. Both workers have an AppArmor profile loaded that forbids any write access. The Deployment archiv should run with it. Prove that the container uses the profile and cannot write.

Task 8: seccomp (5 %)

Namespace pruef-08. The Pod builder does not start. It should run with the seccomp profile no-mkdir.json, which is located on the workers under /var/lib/kubelet/seccomp/profiles. Repair it and prove that mkdir fails.

Task 9: Pod Security Standards (8 %)

Namespace pruef-09. From now on, the namespace enforces the restricted standard and warns about violations of it. Afterwards, the Deployment api keeps running in a compliant way and answers /healthz. Workloads that use host namespaces have no place in the namespace anymore.

Task 10: Get the password out of the ConfigMap (6 %)

Namespace pruef-10. The database password is in the ConfigMap db-config. Move it into the Secret db-pass (key DB_PASSWORD); the Deployment app gets DB_PASSWORD from there and DB_HOST still from the ConfigMap. Prove with etcdctl that the ConfigMap in etcd no longer contains a password and that the Secret is stored encrypted. Write the first 23 bytes of the Secret from etcd to ~/cks/pruef/10-etcd.txt.

Task 11: Sandbox (6 %)

Namespace pruef-11. The Deployment parser processes untrusted files and should run in the existing gVisor sandbox. It does not come up. Repair it and prove that the container runs in gVisor.

Task 12: Critical images (7 %)

Namespace pruef-12. No running image may have vulnerabilities with severity CRITICAL. Switch cache to redis:8.2-alpine (check it first). Scale every other affected Deployment to 0 and write its image to ~/cks/pruef/12-critical.txt.

Task 13: Static analysis (6 %)

The manifest ~/cks/pruef/13-api.yaml has not been applied yet. Rework it until kubesec no longer reports critical findings and gives at least 10 points. Then apply it; api answers /healthz.

Task 14: No images without a tag (7 %)

Create a ValidatingAdmissionPolicy pruef-14-image-tag with a binding of the same name that applies only in pruef-14: Pods whose containers use an image without a tag or with :latest are rejected; images with a digest (@sha256:) are allowed. Afterwards, the Deployment web runs with nginx:1.29-alpine-slim.

Task 15: Audit log (7 %)

Someone deleted the ConfigMap feature-flags in pruef-15. Write the username to ~/cks/pruef/15-wer.txt. Then extend the audit policy: changes (create, update, patch, delete) to ConfigMaps in pruef-15 end up in the log with level RequestResponse, all other ConfigMaps stay at Metadata. Recreate feature-flags with beta=false and prove the content in the audit log.

Task 16: Falco rule (7 %)

Create a Falco rule Paketmanager im Container: priority WARNING, it fires when apk, apt, apt-get, dpkg, yum or dnf starts in a container. Output: %evt.time,%k8s.ns.name,%k8s.pod.name,%proc.name. Use it to find the Pod in pruef-16, write namespace/pod to ~/cks/pruef/16-befund.txt and scale its owner to 0.

Task 17: Stowaway, immutable containers (6 %)

Namespace pruef-17. One of the three Pods starts a program it created itself at runtime and listens on a port nobody asked for. Save its YAML and the program to ~/cks/pruef/17-evidence/ and scale the owner to 0. Afterwards, api and web run with a read-only root filesystem and stay reachable.

Stop the clock.

Solutions

All solutions on cp1 in the folder ~/cks/pruef, except where ssh is shown.

Solution 1

Four policies: deny for both directions, DNS for everyone, and the exception twice – as egress for web and as ingress for api.

cat > 01-netpol.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny
  namespace: pruef-01
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  - Egress
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns
  namespace: pruef-01
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: kube-system
      podSelector:
        matchLabels:
          k8s-app: kube-dns
    ports:
    - port: 53
      protocol: UDP
    - port: 53
      protocol: TCP
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: web-to-api
  namespace: pruef-01
spec:
  podSelector:
    matchLabels:
      app: web
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: api
    ports:
    - port: 8080
      protocol: TCP
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-from-web
  namespace: pruef-01
spec:
  podSelector:
    matchLabels:
      app: api
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: web
    ports:
    - port: 8080
      protocol: TCP
EOF
k apply -f 01-netpol.yaml
k exec -n pruef-01 web -- wget -qO- -T3 api:8080/healthz; echo
k exec -n pruef-01 test -- wget -qO- -T3 api:8080/healthz
k exec -n pruef-01 web -- wget -qO- -T3 http://172.16.0.126:30080/
k exec -n pruef-01 test -- nslookup api | grep -A1 Name

Expected:

networkpolicy.networking.k8s.io/default-deny created
networkpolicy.networking.k8s.io/allow-dns created
networkpolicy.networking.k8s.io/web-to-api created
networkpolicy.networking.k8s.io/api-from-web created
ok
wget: download timed out
command terminated with exit code 1
wget: download timed out
command terminated with exit code 1
Name:   api.pruef-01.svc.cluster.local
Address: 10.101.170.45

Before, both clients reached api, and web reached Traefik. The last line shows: DNS works despite default deny – without allow-dns, even name resolution fails.

Solution 2

sudo kube-bench run --targets=master --benchmark=cis-1.12 --group=1.1 \
  > 02-vorher.txt 2>&1
grep '^\[FAIL\]' 02-vorher.txt | cut -c1-70
grep -A3 -E '^1\.1\.(5|12|17) ' 02-vorher.txt

Expected (abridged):

[FAIL] 1.1.5 Ensure that the scheduler pod specification file permissi
[FAIL] 1.1.12 Ensure that the etcd data directory ownership is set to
[FAIL] 1.1.17 Ensure that the controller-manager.conf file permissions
1.1.5 Run the below command (based on the file location on your system) on the …
For example, chmod 600 /etc/kubernetes/manifests/kube-scheduler.yaml
…
1.1.12 On the etcd server node, get the etcd data directory, passed as an argument …
…
For example, chown etcd:etcd /var/lib/etcd
--
1.1.17 Run the below command (based on the file location on your system) on the …
For example,
chmod 600 /etc/kubernetes/controller-manager.conf
sudo chmod 600 /etc/kubernetes/manifests/kube-scheduler.yaml
sudo chmod 600 /etc/kubernetes/controller-manager.conf
sudo chown etcd:etcd /var/lib/etcd
sudo kube-bench run --targets=master --benchmark=cis-1.12 --check=1.1.5,1.1.12,1.1.17 \
  2>/dev/null | grep -E '^\[(PASS|FAIL)' | cut -c1-70

Expected:

[PASS] 1.1.5 Ensure that the scheduler pod specification file permissi
[PASS] 1.1.12 Ensure that the etcd data directory ownership is set to
[PASS] 1.1.17 Ensure that the controller-manager.conf file permissions

Changing permissions and ownership does not restart any component – only changed content in the manifest folder does that.

Solution 3

openssl req -x509 -newkey rsa:2048 -nodes -days 365 \
  -keyout 03-shop.key -out 03-shop.crt -subj "/CN=shop.pruef.lab" \
  -addext "subjectAltName=DNS:shop.pruef.lab"
k create secret tls shop-tls -n pruef-03 --cert=03-shop.crt --key=03-shop.key
k create ingress shop -n pruef-03 --class=traefik \
  --rule="shop.pruef.lab/*=shop:80,tls=shop-tls"
curl -s -o /dev/null -w "%{http_code}\n" --cacert 03-shop.crt \
  --resolve shop.pruef.lab:30443:172.16.0.126 https://shop.pruef.lab:30443/
curl -sv --cacert 03-shop.crt --resolve shop.pruef.lab:30443:172.16.0.126 \
  https://shop.pruef.lab:30443/ -o /dev/null 2>&1 | grep -E "subject:|verify ok"

Expected (abridged):

secret/shop-tls created
ingress.networking.k8s.io/shop created
200
*  subject: CN=shop.pruef.lab
*  SSL certificate verify ok.

verify ok means: Traefik really serves the certificate from shop-tls, not its own default certificate. Then check the binaries against the official checksums:

cd ~/cks/pruef/03
./kubectl version --client
ARCH=$(dpkg --print-architecture)
U=https://dl.k8s.io/release/v1.37.2/bin/linux/${ARCH}
echo "$(curl -sSL $U/kubectl.sha512)  kubectl" | sha512sum -c
echo "$(curl -sSL $U/kubeadm.sha512)  kubeadm" | sha512sum -c
rm kubeadm
cd ~/cks/pruef

Expected:

Client Version: v1.37.2
kubectl: OK
kubeadm: FAILED
sha512sum: WARNING: 1 computed checksum did NOT match

The version string alone proves nothing – a tampered binary reports the same version. Only the checksum counts.

Solution 4

With k create role, --resource-name applies to all rules. Two rules with different names are only possible via YAML:

cat > 04-role.yaml <<'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: ci
  namespace: pruef-04
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources: ["configmaps"]
  resourceNames: ["app-config"]
  verbs: ["get"]
EOF
k replace -f 04-role.yaml
SA=system:serviceaccount:pruef-04:ci
k auth can-i list pods -n pruef-04 --as=$SA
k auth can-i get configmap/app-config -n pruef-04 --as=$SA
k auth can-i get configmap/other -n pruef-04 --as=$SA
k auth can-i list configmaps -n pruef-04 --as=$SA
k auth can-i get secrets -n pruef-04 --as=$SA
k auth can-i delete pods -n pruef-04 --as=$SA

Expected:

role.rbac.authorization.k8s.io/ci replaced
yes
yes
no
no
no
no

list configmaps is no, even though app-config is allowed: resourceNames has no effect on list and watch. That is exactly what the task asks for.

Solution 5

k create sa frontend -n pruef-05
k patch sa frontend -n pruef-05 -p '{"automountServiceAccountToken":false}'
k patch sa default -n pruef-05 -p '{"automountServiceAccountToken":false}'
k set serviceaccount deploy frontend frontend -n pruef-05
k rollout status -n pruef-05 deploy/frontend
k exec -n pruef-05 deploy/frontend -- ls /var/run/secrets/kubernetes.io/serviceaccount
k get sa -n pruef-05 \
  -o custom-columns=NAME:.metadata.name,AUTOMOUNT:.automountServiceAccountToken

Expected (abridged):

serviceaccount/frontend created
serviceaccount/frontend patched
serviceaccount/default patched
deployment.apps/frontend serviceaccount updated
deployment "frontend" successfully rolled out
ls: /var/run/secrets/kubernetes.io/serviceaccount: No such file or directory
command terminated with exit code 1
NAME       AUTOMOUNT
default    false
frontend   false

Before, the output was ca.crt namespace token. The old Pod still needs 30 s to terminate (sleep does not react to SIGTERM) – until then, k get pods shows it as Terminating.

Solution 6

First prove that no binding is to blame, then look at the API server:

k auth can-i get secrets -A --as=system:serviceaccount:pruef-06:viewer
k get rolebindings,clusterrolebindings -A -o wide | grep pruef-06:viewer
sudo kube-bench run --targets=master --benchmark=cis-1.12 --group=1.2 2>/dev/null \
  | grep '^\[FAIL\]' | cut -c1-80
ps -o args= -C kube-apiserver | tr ' ' '\n' | grep authorization-mode

Expected (abridged):

yes
[FAIL] 1.2.5 Ensure that the --kubelet-certificate-authority argument is set as
[FAIL] 1.2.6 Ensure that the --authorization-mode argument is not set to AlwaysA
[FAIL] 1.2.17 Ensure that the --audit-log-maxage argument is set to 30 or as app
…
--authorization-mode=Node,RBAC,AlwaysAllow

The authorizers are asked in order. RBAC never explicitly denies, it only allows – if it finds no rule, the API server asks the next one, and AlwaysAllow says yes. Back up, fix, compare against the original:

M=/etc/kubernetes/manifests
sudo cp $M/kube-apiserver.yaml ~/cks/pruef/backup/kube-apiserver.yaml.06
sudo sed -i 's/,AlwaysAllow$//' $M/kube-apiserver.yaml
sudo cmp $M/kube-apiserver.yaml ~/cks/pruef/backup/kube-apiserver.yaml.orig \
  && echo gleich

Expected: gleich

After 1–2 minutes:

k get --raw /readyz; echo
ps -o args= -C kube-apiserver | tr ' ' '\n' | grep authorization-mode
k auth can-i get secrets -A --as=system:serviceaccount:pruef-06:viewer
sudo kube-bench run --targets=master --benchmark=cis-1.12 --check=1.2.6 2>/dev/null \
  | grep -E '^\[(PASS|FAIL)' | cut -c1-80
k get nodes

Expected (abridged):

ok
--authorization-mode=Node,RBAC
no
[PASS] 1.2.6 Ensure that the --authorization-mode argument is not set to AlwaysA
NAME      STATUS   ROLES           AGE    VERSION
cp1       Ready    control-plane   165m   …
worker1   Ready    <none>          163m   …
worker2   Ready    <none>          163m   …

The remaining FAILs in 1.2 (kubelet CA, audit sizes, token lifetime) belong to the lab state and are not part of the task. The controller manager and the scheduler often go into CrashLoopBackOff briefly after the restart (leader election) – just wait.

Solution 7

The node shows which profiles are loaded and what the writing one is called (from your own machine):

ssh [email protected] 'sudo aa-status | grep cks'

Expected:

   cks-deny-etc
   cks-deny-write

The same on worker2 (172.16.0.83). Back on cp1:

cat > 07-patch.yaml <<'EOF'
spec:
  template:
    spec:
      containers:
      - name: busybox
        securityContext:
          appArmorProfile:
            type: Localhost
            localhostProfile: cks-deny-write
EOF
k patch deploy archiv -n pruef-07 --patch-file 07-patch.yaml
k rollout status -n pruef-07 deploy/archiv
k exec -n pruef-07 deploy/archiv -- cat /proc/1/attr/current; echo
k exec -n pruef-07 deploy/archiv -- touch /tmp/x

Expected:

deployment.apps/archiv patched
deployment "archiv" successfully rolled out
cks-deny-write (enforce)
touch: /tmp/x: Permission denied
command terminated with exit code 1

Before, /proc/1/attr/current showed the default profile cri-containerd.apparmor.d (enforce). The container is called busybox because k create deployment names it after the image.

Solution 8

k get pod builder -n pruef-08
k describe pod builder -n pruef-08 | grep 'seccomp profile'

Expected (abridged):

NAME      READY   STATUS                 RESTARTS   AGE
builder   0/1     CreateContainerError   0          9s
  Warning  Failed  …  failed to create containerd container: cannot load seccomp
profile "/var/lib/kubelet/seccomp/no-mkdir.json": open
/var/lib/kubelet/seccomp/no-mkdir.json: no such file or directory

localhostProfile is relative to /var/lib/kubelet/seccomp – profiles/ is missing. The field is immutable in a running Pod, so replace it:

sed -i 's#localhostProfile: no-mkdir.json#localhostProfile: profiles/no-mkdir.json#' \
  08-builder.yaml
k replace --force -f 08-builder.yaml
k wait -n pruef-08 --for=condition=Ready pod/builder --timeout=60s
k exec -n pruef-08 builder -- grep Seccomp: /proc/1/status
k exec -n pruef-08 builder -- mkdir /tmp/neu
k exec -n pruef-08 builder -- touch /tmp/datei && echo touch-ok

Expected:

pod "builder" deleted from pruef-08 namespace
pod/builder replaced
pod/builder condition met
Seccomp:    2
mkdir: can't create directory '/tmp/neu': Operation not permitted
command terminated with exit code 1
touch-ok

touch still works: the profile blocks only mkdir/mkdirat and allows everything else.

Solution 9

First use a server-side dry run to see what violates restricted:

k label --dry-run=server --overwrite ns pruef-09 \
  pod-security.kubernetes.io/enforce=restricted

Expected (abridged):

Warning: existing pods in namespace "pruef-09" violate the new PodSecurity enforce
level "restricted:latest"
Warning: api-7cf6764965-j9gd8: allowPrivilegeEscalation != false, unrestricted
capabilities, runAsNonRoot != true, seccompProfile
Warning: debug-67794d55cb-xlxqf: host namespaces, allowPrivilegeEscalation != false,
unrestricted capabilities, runAsNonRoot != true, seccompProfile
namespace/pruef-09 labeled (server dry run)

api can be repaired (the image already runs as 65532), debug with hostPID cannot – it has to go:

k label --overwrite ns pruef-09 pod-security.kubernetes.io/enforce=restricted \
  pod-security.kubernetes.io/warn=restricted
cat > 09-patch.yaml <<'EOF'
spec:
  template:
    spec:
      securityContext:
        runAsNonRoot: true
        seccompProfile:
          type: RuntimeDefault
      containers:
      - name: notes-api
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop: ["ALL"]
EOF
k patch deploy api -n pruef-09 --patch-file 09-patch.yaml
k delete deploy debug -n pruef-09
k rollout status -n pruef-09 deploy/api
k label --dry-run=server --overwrite ns pruef-09 \
  pod-security.kubernetes.io/enforce=restricted
k exec -n pruef-09 deploy/api -- wget -qO- localhost:8080/healthz; echo
k run probe -n pruef-09 --image=busybox:1.37 --privileged -- sleep 1

Expected (abridged):

deployment.apps/api patched
deployment.apps "debug" deleted from pruef-09 namespace
deployment "api" successfully rolled out
namespace/pruef-09 not labeled (server dry run)
ok
Error from server (Forbidden): pods "probe" is forbidden: violates PodSecurity
"restricted:latest": privileged (container "probe" must not set …

The second dry run without a warning is the proof: no running Pod violates restricted anymore. The label alone would not have terminated debug – PSA only checks new Pods.

Solution 10

The situation in etcd (the value is plain text, grep -a counts it):

P=/etc/kubernetes/pki
E="--cacert=$P/etcd/ca.crt --cert=$P/apiserver-etcd-client.crt"
E="$E --key=$P/apiserver-etcd-client.key"
sudo etcdctl $E get /registry/configmaps/pruef-10/db-config --print-value-only \
  | grep -ac Lab-Pruef-10

Expected: 1

Create the Secret, delete the key from the ConfigMap, rewire the variable:

k create secret generic db-pass -n pruef-10 --from-literal=DB_PASSWORD=Lab-Pruef-10
k patch cm db-config -n pruef-10 --type=json \
  -p '[{"op":"remove","path":"/data/DB_PASSWORD"}]'
k set env deploy/app -n pruef-10 DB_PASSWORD-
k set env deploy/app -n pruef-10 --from=secret/db-pass
k rollout status -n pruef-10 deploy/app
k set env deploy/app -n pruef-10 --list
k exec -n pruef-10 deploy/app -- env | grep DB_

Expected (abridged):

secret/db-pass created
configmap/db-config patched
deployment.apps/app env updated
deployment.apps/app env updated
deployment "app" successfully rolled out
# Deployment app, container busybox
# DB_HOST from configmap db-config, key DB_HOST
# DB_PASSWORD from secret db-pass, key DB_PASSWORD
DB_HOST=db.pruef-10
DB_PASSWORD=Lab-Pruef-10

Proof in etcd:

sudo etcdctl $E get /registry/configmaps/pruef-10/db-config --print-value-only \
  | grep -ac Lab-Pruef-10
sudo etcdctl $E get /registry/secrets/pruef-10/db-pass --print-value-only \
  | head -c 23 > 10-etcd.txt
cat 10-etcd.txt; echo

Expected:

0
k8s:enc:aescbc:v1:key2:

The EncryptionConfiguration from Day 10 does not encrypt ConfigMaps – only resources: [secrets]. That is why a password never belongs in a ConfigMap.

Solution 11

k get deploy,pods -n pruef-11
k get events -n pruef-11 --field-selector reason=FailedCreate \
  -o jsonpath='{.items[0].message}'; echo
k get runtimeclass

Expected:

NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/parser   0/1     0            0           5s
Error creating: pods "parser-5bf6896dc7-" is forbidden: pod rejected: RuntimeClass
"sandbox" not found
NAME     HANDLER   AGE
gvisor   runsc     76m
k patch deploy parser -n pruef-11 \
  -p '{"spec":{"template":{"spec":{"runtimeClassName":"gvisor"}}}}'
k rollout status -n pruef-11 deploy/parser
k get pod -n pruef-11 -o wide | awk '{print $1, $3, $7}'
k exec -n pruef-11 deploy/parser -- dmesg | head -1
k exec -n pruef-11 deploy/parser -- uname -r

Expected:

deployment.apps/parser patched
deployment "parser" successfully rolled out
NAME STATUS NODE
parser-6466d7fddb-wsv8v Running worker2
[   0.000000] Starting gVisor...
4.19.0-gvisor

No Pods, no Pod events: you find the reason in the ReplicaSet’s event (FailedCreate), not in k describe pod.

Solution 12

k get pods -n pruef-12 -o jsonpath='{.items[*].spec.containers[*].image}' \
  | tr ' ' '\n' | sort -u
trivy image -q --scanners vuln --severity CRITICAL redis:7.0.0-alpine | grep ^Total
trivy image -q --scanners vuln --severity CRITICAL httpd:2.4.54-alpine | grep ^Total
trivy image -q --scanners vuln --severity CRITICAL --insecure --exit-code 1 \
  172.16.0.126:30500/notes-api:1.0 > /dev/null; echo $?
trivy image -q --scanners vuln --severity CRITICAL --exit-code 1 \
  redis:8.2-alpine > /dev/null; echo $?

Expected:

172.16.0.126:30500/notes-api:1.0
httpd:2.4.54-alpine
redis:7.0.0-alpine
Total: 1 (CRITICAL: 1)
Total: 6 (CRITICAL: 6)
0
0

cache and web are affected; notes-api:1.0 and the replacement image are free of CRITICAL (as of the Trivy DB from 2026-11-18):

k set image deploy/cache -n pruef-12 redis=redis:8.2-alpine
k scale deploy web -n pruef-12 --replicas=0
echo httpd:2.4.54-alpine > 12-critical.txt
k rollout status -n pruef-12 deploy/cache
k get deploy -n pruef-12 -o custom-columns=NAME:.metadata.name,\
READY:.status.readyReplicas,IMAGE:.spec.template.spec.containers[0].image

Expected:

deployment.apps/cache image updated
deployment.apps/web scaled
deployment "cache" successfully rolled out
NAME    READY    IMAGE
api     1        172.16.0.126:30500/notes-api:1.0
cache   1        redis:8.2-alpine
web     <none>   httpd:2.4.54-alpine

Without findings, Trivy prints no Total line – so to confirm “clean”, check the exit code instead of grepping for Total.

Solution 13

kubesec scan 13-api.yaml | jq -r '.[0] | .score, .message'
kubesec scan 13-api.yaml | jq -r '.[0].scoring.critical[] | .id + ": " + .selector'

Expected:

-60
Failed with a score of -60 points
CapSysAdmin: containers[] .securityContext .capabilities .add == SYS_ADMIN
Privileged: containers[] .securityContext .privileged == true

Remove the two critical items, then work through the advice from .scoring.advise:

cat > 13-api.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  namespace: pruef-13
spec:
  replicas: 1
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      automountServiceAccountToken: false
      securityContext:
        runAsNonRoot: true
        runAsUser: 65532
        runAsGroup: 65532
        seccompProfile:
          type: RuntimeDefault
      containers:
      - name: notes-api
        image: 172.16.0.126:30500/notes-api:1.0
        env:
        - name: NOTES_API_KEY
          value: lab-key-123
        securityContext:
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: true
          capabilities:
            drop: ["ALL"]
        resources:
          requests:
            cpu: 50m
            memory: 32Mi
          limits:
            cpu: 200m
            memory: 64Mi
EOF
kubesec scan 13-api.yaml | jq -r '.[0] | .score, .message'
kubesec scan 13-api.yaml | jq -r '.[0].scoring.critical | length'
k apply -f 13-api.yaml
k rollout status -n pruef-13 deploy/api
k exec -n pruef-13 deploy/api -- wget -qO- localhost:8080/healthz; echo

Expected:

12
Passed with a score of 12 points
0
deployment.apps/api created
deployment "api" successfully rolled out
ok

A high score is useless if the application does not start afterwards – so always finish with /healthz.

Solution 14

The image name comes after the last /; only there does a : indicate a tag (before it, there may be the registry’s port):

cat > 14-vap.yaml <<'EOF'
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: pruef-14-image-tag
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups: [""]
      apiVersions: ["v1"]
      operations: ["CREATE", "UPDATE"]
      resources: ["pods"]
  validations:
  - expression: >-
      object.spec.containers.all(c, c.image.contains('@sha256:') ||
      (c.image.substring(c.image.lastIndexOf('/') + 1).contains(':') &&
      !c.image.endsWith(':latest')))
    messageExpression: >-
      "Image ohne festes Tag: " + object.spec.containers.map(c, c.image).join(", ")
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicyBinding
metadata:
  name: pruef-14-image-tag
spec:
  policyName: pruef-14-image-tag
  validationActions: ["Deny"]
  matchResources:
    namespaceSelector:
      matchLabels:
        kubernetes.io/metadata.name: pruef-14
EOF
k apply -f 14-vap.yaml
k run probe -n pruef-14 --image=nginx
k run probe -n pruef-14 --image=nginx:latest
k run probe -n pruef-14 --image=nginx:1.29-alpine-slim --dry-run=server
k get pods -n pruef-14

Expected (abridged):

validatingadmissionpolicy.admissionregistration.k8s.io/pruef-14-image-tag created
validatingadmissionpolicybinding.admissionregistration.k8s.io/pruef-14-image-tag
created
The pods "probe" is invalid: : ValidatingAdmissionPolicy 'pruef-14-image-tag' with
binding 'pruef-14-image-tag' denied request: Image ohne festes Tag: nginx
The pods "probe" is invalid: : ValidatingAdmissionPolicy 'pruef-14-image-tag' with
binding 'pruef-14-image-tag' denied request: Image ohne festes Tag: nginx:latest
pod/probe created (server dry run)
NAME                  READY   STATUS    RESTARTS   AGE
web-d8f7cc9c9-wtnpb   1/1     Running   0          8s

The old web Pod keeps running – admission only checks new requests. New Pods with latest would be rejected, so pin the image:

k set image deploy/web -n pruef-14 nginx=nginx:1.29-alpine-slim
k rollout status deploy/web -n pruef-14
k get deploy web -n pruef-14 -o wide | awk '{print $1, $2, $7}'

Expected:

deployment.apps/web image updated
deployment "web" successfully rolled out
NAME READY IMAGES
web 1/1 nginx:1.29-alpine-slim

Without a binding, the policy has no effect. Errors in the CEL expression are shown by k get validatingadmissionpolicy pruef-14-image-tag -o yaml under status.typeChecking.

Solution 15

L=/var/log/kubernetes/audit/audit.log
sudo jq -c 'select(.objectRef.resource=="configmaps"
  and .objectRef.name=="feature-flags" and .verb=="delete")
  | [.user.username, .responseStatus.code, .sourceIPs[0], .stageTimestamp]' $L
echo system:serviceaccount:pruef-15:cleanup > 15-wer.txt

Expected (abridged):

["system:serviceaccount:pruef-15:cleanup",200,"172.16.0.126","2026-11-18T20:01:21…"]

In the policy, the first matching rule wins. So the new rule must go before the existing Metadata rule for Secrets and ConfigMaps. Back up, then insert it with sudo vim /etc/kubernetes/audit/policy.yaml directly after the rule for watch:

sudo cp /etc/kubernetes/audit/policy.yaml ~/cks/pruef/backup/policy.yaml.orig
- level: RequestResponse
  namespaces: ["pruef-15"]
  verbs: ["create", "update", "patch", "delete"]
  resources:
  - group: ""
    resources: ["configmaps"]
sudo sed -n '/verbs: \["watch"\]/,/serviceaccounts/p' /etc/kubernetes/audit/policy.yaml

Expected:

  verbs: ["watch"]
- level: RequestResponse
  namespaces: ["pruef-15"]
  verbs: ["create", "update", "patch", "delete"]
  resources:
  - group: ""
    resources: ["configmaps"]
- level: Metadata
  resources:
  - group: ""
    resources: ["secrets", "configmaps", "serviceaccounts/token"]

The API server reads the policy only at startup. The manifest does not change, so stop the container directly; the kubelet restarts it:

sudo crictl stop $(sudo crictl ps --name kube-apiserver -q)

After 1–2 minutes (k get --raw /readyz reports ok):

k create configmap feature-flags -n pruef-15 --from-literal=beta=false
k create configmap other -n pruef-16 --from-literal=x=1
sudo jq -c 'select(.objectRef.resource=="configmaps" and .verb=="create"
  and (.objectRef.namespace=="pruef-15" or .objectRef.namespace=="pruef-16"))
  | [.level, .objectRef.namespace, .requestObject.data]' $L | tail -2
k delete cm other -n pruef-16

Expected:

configmap/feature-flags created
configmap/other created
["RequestResponse","pruef-15",{"beta":"false"}]
["Metadata","pruef-16",null]
configmap "other" deleted from pruef-16 namespace

Lab duty after the exam (the audit policy from Day 15 remains the standard):

sudo cp ~/cks/pruef/backup/policy.yaml.orig /etc/kubernetes/audit/policy.yaml
sudo crictl stop $(sudo crictl ps --name kube-apiserver -q)

Then wait for readyz again.

Solution 16

Back up the values, add the rule via the Helm value customRules:

helm get values falco -n falco -o yaml > backup/falco-values.yaml
cat > 16-rules.yaml <<'EOF'
customRules:
  pruef-16.yaml: |-
    - list: pkg_tools
      items: [apk, apt, apt-get, dpkg, yum, dnf]
    - rule: Paketmanager im Container
      desc: Ein Paketmanager startet in einem laufenden Container
      condition: spawned_process and container and proc.name in (pkg_tools)
      output: "%evt.time,%k8s.ns.name,%k8s.pod.name,%proc.name"
      priority: WARNING
EOF
helm upgrade falco falcosecurity/falco --version 9.2.2 -n falco --reuse-values \
  -f 16-rules.yaml
k -n falco rollout status ds/falco

Expected (abridged):

Release "falco" has been upgraded. Happy Helming!
daemon set "falco" successfully rolled out

After one minute, evaluate the alerts of all Falco Pods:

k -n falco logs -l app.kubernetes.io/name=falco -c falco --since=5m \
  | grep '^[0-9:.]*: Warning [0-9]' | cut -d' ' -f3 | cut -d, -f2-4 | sort | uniq -c

Expected:

      3 pruef-16,updater-57b5dd6d7f-m8lrt,apk

A complete alert looks like this (abridged) – Falco appends the container fields itself:

20:04:35.715182780: Warning 20:04:35.715182780,pruef-16,updater-57b5dd6d7f-m8lrt,apk
container_id=ebcd6f4014f2 container_name=alpine … k8s_ns_name=pruef-16
k get pod -n pruef-16 updater-57b5dd6d7f-m8lrt \
  -o jsonpath='{.metadata.ownerReferences[0].name}'; echo
echo pruef-16/updater-57b5dd6d7f-m8lrt > 16-befund.txt
k scale deploy updater -n pruef-16 --replicas=0

Expected:

updater-57b5dd6d7f
deployment.apps/updater scaled

The Pod’s owner is the ReplicaSet; its name without the hash is the Deployment. Lab duty after the exam – reset Falco to the default rules from Day 16:

helm upgrade falco falcosecurity/falco --version 9.2.2 -n falco --reset-values \
  --set driver.kind=modern_ebpf --set tty=true \
  --set falcoctl.artifact.follow.enabled=false \
  --set resources.requests.cpu=50m --set resources.requests.memory=128Mi
k -n falco rollout status ds/falco
helm get values falco -n falco -o yaml | diff - backup/falco-values.yaml && echo gleich

Expected (abridged):

Release "falco" has been upgraded. Happy Helming!
daemon set "falco" successfully rolled out
gleich

Solution 17

Look at the processes of all three Pods:

for d in api web metrics; do echo "== $d"
  k exec -n pruef-17 deploy/$d -- ps -o pid,args; done

Expected:

== api
PID   COMMAND
    1 notes-api
   13 ps -o pid,args
== web
PID   COMMAND
    1 nginx: master process nginx -g daemon off;
   31 nginx: worker process
   32 nginx: worker process
   33 ps -o pid,args
== metrics
PID   COMMAND
    1 sleep 3600
    7 {kworkerd} /bin/sh /tmp/.x/kworkerd
   10 nc -lk -p 4444
   11 ps -o pid,args

A “kernel thread” that is a shell script from a hidden folder under /tmp – that is the stowaway:

k exec -n pruef-17 deploy/metrics -- netstat -tln
k exec -n pruef-17 deploy/metrics -- ls -la /tmp/.x
P=$(k get pod -n pruef-17 -l app=metrics -o name)
mkdir -p 17-evidence
k get -n pruef-17 $P -o yaml > 17-evidence/pod.yaml
k exec -n pruef-17 $P -- cat /tmp/.x/kworkerd > 17-evidence/kworkerd
k scale deploy metrics -n pruef-17 --replicas=0
ls 17-evidence

Expected (abridged):

Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 :::4444                 :::*                    LISTEN
-rwxr-xr-x    1 root     root            25 Nov 18 20:06 kworkerd
deployment.apps/metrics scaled
kworkerd
pod.yaml

Save first, then scale – with the Pod, its writable layer disappears and with it the evidence. Now make web and api immutable. nginx needs writable locations for its cache and PID file:

cat > 17-web.yaml <<'EOF'
spec:
  template:
    spec:
      containers:
      - name: nginx
        securityContext:
          readOnlyRootFilesystem: true
        volumeMounts:
        - name: cache
          mountPath: /var/cache/nginx
        - name: run
          mountPath: /run
      volumes:
      - name: cache
        emptyDir: {}
      - name: run
        emptyDir: {}
EOF
k patch deploy web -n pruef-17 --patch-file 17-web.yaml
k patch deploy api -n pruef-17 -p '{"spec":{"template":{"spec":{"containers":[
  {"name":"notes-api","securityContext":{"readOnlyRootFilesystem":true}}]}}}}'
k rollout status -n pruef-17 deploy/web
k rollout status -n pruef-17 deploy/api
k get deploy -n pruef-17
k exec -n pruef-17 deploy/web -- touch /usr/share/nginx/html/x
k exec -n pruef-17 deploy/api -- touch /x
k exec -n pruef-17 deploy/api -- wget -qO- -T3 web | grep -o '<title>.*</title>'

Expected (abridged):

deployment.apps/web patched
deployment.apps/api patched
deployment "web" successfully rolled out
deployment "api" successfully rolled out
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
api       1/1     1            1           22s
metrics   0/0     0            0           21s
web       1/1     1            1           21s
touch: /usr/share/nginx/html/x: Read-only file system
command terminated with exit code 1
touch: /x: Read-only file system
command terminated with exit code 1
<title>Welcome to nginx!</title>

notes-api writes nothing to disk and therefore needs no emptyDir.

Scoring

Add up the weights of the tasks that are completely and verifiably solved – there is no partial credit here. If you switch off a hardening measure to make something run, you get zero for that task.

Result Assessment
85–100 % exam-ready
67–84 % passed, but barely: review your weak spots
under 67 % review the days covering the missed tasks and start over
Tasks Curriculum domain Days
1–3 Cluster Setup (15 %) 2–4
4–6 Cluster Hardening (15 %) 5, 6
7, 8 System Hardening (10 %) 7, 8
9–11 Minimize Microservice Vulnerabilities (20 %) 9–12
12–14 Supply Chain Security (20 %) 13, 14
15–17 Monitoring, Logging and Runtime Security (20 %) 15–18

Clean up (the API server, audit policy and Falco are already back in their initial state after Solutions 6, 15 and 16):

for i in $(seq -w 1 17); do k delete ns pruef-$i --wait=false; done
k delete -f 14-vap.yaml
rm ~/cks/pruef/03/kubectl

Checkpoint

k get ns | grep -c '^pruef-'

Expected (after about a minute): 0

k get validatingadmissionpolicies,validatingadmissionpolicybindings

Expected: No resources found

ps -o args= -C kube-apiserver | tr ' ' '\n' \
  | grep -E 'authorization-mode|authentication-config|encryption-pro|audit-policy'

Expected:

--authorization-mode=Node,RBAC
--authentication-config=/etc/kubernetes/authn/authn-config.yaml
--encryption-provider-config=/etc/kubernetes/enc/enc.yaml
--audit-policy-file=/etc/kubernetes/audit/policy.yaml
sudo diff ~/cks/pruef/backup/policy.yaml.orig /etc/kubernetes/audit/policy.yaml \
  && echo gleich

Expected: gleich

sudo stat -c '%a %U:%G %n' /etc/kubernetes/manifests/kube-scheduler.yaml \
  /etc/kubernetes/controller-manager.conf /var/lib/etcd

Expected:

600 root:root /etc/kubernetes/manifests/kube-scheduler.yaml
600 root:root /etc/kubernetes/controller-manager.conf
700 etcd:etcd /var/lib/etcd
helm get values falco -n falco -o yaml | grep -c customRules

Expected: 0

k get --raw /readyz; echo
k get nodes
k get pods -A --no-headers | grep -vc Running

Expected: ok, three nodes Ready, 0.

Good luck in the exam.