Skip to content

KCSA: Kubernetes Threat Model

16% of the KCSA exam, 7 competencies. Back to the KCSA overview.

Each competency lists what to know and the official documentation to read, then a demo you can run. Demos run on the demo lab, a throwaway kind cluster on your own machine; set it up once before you start.

Know

  • Boundaries sit between the internet and ingress, workloads and the API server, nodes and the control plane, and containers and their host.

Read

Demo: Follow a request across the boundaries

  1. Watch kubectl talk to the API server.

    Terminal window
    kubectl get pods -n kube-system -v=6 2>&1 | grep -E 'Config loaded|GET https' | head -3
  2. Each component crosses the boundary with its own identity.

    Terminal window
    docker exec kind-worker sh -c 'cat /var/lib/kubelet/pki/kubelet-client-current.pem' | openssl x509 -noout -subject
    docker exec kind-control-plane grep -E -- '--(kubelet-client-certificate|etcd-certfile)' /etc/kubernetes/manifests/kube-apiserver.yaml

What you should see: kubectl sends HTTPS requests to the API server. The kubelet authenticates as system:node:kind-worker in group system:nodes, and the API server uses separate client certificates to reach kubelets and etcd. Every arrow in the data-flow diagram is a TLS connection with its own credential, and each credential is something to protect.

Know

  • Attackers persist with CronJobs, DaemonSets, mutating webhooks, static pods, or stolen long-lived credentials. Audit those object types.

Read

Demo: Hunt for the places an attacker would hide

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace kcsa-persist
    kubectl config set-context --current --namespace=kcsa-persist
  2. Plant three common persistence mechanisms.

    Terminal window
    kubectl create cronjob beacon --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 --schedule='*/5 * * * *' -- sh -c 'echo phoning home'
    kubectl create serviceaccount backdoor
    kubectl create clusterrolebinding backdoor-admin --clusterrole=cluster-admin --serviceaccount=kcsa-persist:backdoor
    docker exec kind-worker sh -c 'ls /etc/kubernetes/manifests/'
  3. Now hunt like a defender.

    Terminal window
    kubectl get cronjobs,daemonsets -A
    kubectl get clusterrolebindings -o json | jq -r '.items[] | select(.roleRef.name=="cluster-admin") | "\(.metadata.name): \(.subjects // [] | map(.kind + ":" + (.namespace // "") + "/" + .name) | join(", "))"'
    kubectl get mutatingwebhookconfigurations,validatingwebhookconfigurations
    for n in kind-control-plane kind-worker kind-worker2; do echo "$n:"; docker exec $n ls /etc/kubernetes/manifests/ 2>/dev/null; done

What you should see: The hunt finds the CronJob and the backdoor-admin binding next to the legitimate cluster-admin bindings. Static pod directories on workers should be empty; a manifest there runs with no API object to delete. Webhooks are the subtle one: a mutating webhook can inject a container into every new pod.

Clean up:

Terminal window
kubectl delete clusterrolebinding backdoor-admin
kubectl config set-context --current --namespace=default
kubectl delete namespace kcsa-persist

Know

  • Unbounded resource use, API request floods, and etcd exhaustion. Quotas, limit ranges, API priority and fairness defend.

Read

Demo: Cap object counts and see API fairness

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace kcsa-dos
    kubectl config set-context --current --namespace=kcsa-dos
  2. A quota on object counts, then try to exceed it.

    Terminal window
    kubectl create quota objects --hard=count/configmaps=5,count/secrets=5,pods=5
    for i in $(seq 1 8); do kubectl create configmap c$i --from-literal=k=v 2>&1 | tail -1; done
  3. API Priority and Fairness keeps one noisy client from starving the rest.

    Terminal window
    kubectl get prioritylevelconfigurations
    kubectl get flowschemas | head -12

What you should see: Creation stops at the quota with exceeded quota: objects (the namespace’s own root CA ConfigMap uses one slot). Without limits, a script creating objects in a loop can fill etcd. API Priority and Fairness queues requests by flow so a flood from one client doesn’t block the controllers.

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace kcsa-dos

Malicious Code Execution and Compromised Applications in Containers

Section titled “Malicious Code Execution and Compromised Applications in Containers”

Know

  • Limit the blast radius: non-root, no privilege escalation, read-only filesystem, seccomp, and no service account token unless needed.

Read

Demo: Make a compromised container less useful

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace kcsa-malicious
    kubectl config set-context --current --namespace=kcsa-malicious
  2. An attacker in a normal container can download and run tools.

    Terminal window
    kubectl run soft --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 -- sleep 3600
    kubectl wait --for=condition=Ready pod/soft --timeout=60s
    kubectl exec soft -- sh -c 'cp /bin/busybox /usr/bin/tool && /usr/bin/tool echo dropped a binary'
  3. Read-only root, non-root user, no capabilities.

    Terminal window
    kubectl run hard --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 --overrides='{"spec":{"securityContext":{"runAsNonRoot":true,"runAsUser":10001},"containers":[{"name":"hard","image":"registry.k8s.io/e2e-test-images/busybox:1.37.0-2","command":["sleep","3600"],"securityContext":{"readOnlyRootFilesystem":true,"allowPrivilegeEscalation":false,"capabilities":{"drop":["ALL"]}}}]}}'
    kubectl wait --for=condition=Ready pod/hard --timeout=60s
    kubectl exec hard -- sh -c 'cp /bin/busybox /usr/bin/tool' || true
  4. And find out who can exec into pods at all.

    Terminal window
    kubectl get clusterroles -o json | jq -r '.items[] | select(any(.rules[]?; (.resources // []) | index("pods/exec"))) | .metadata.name'

What you should see: The soft pod lets you drop and run a binary. The hard one fails with Read-only file system. Hardening doesn’t stop the initial exploit; it limits what the attacker can do next. Every role that grants pods/exec is a way in for anyone holding it.

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace kcsa-malicious

Know

  • Without NetworkPolicies or mTLS, a compromised pod can reach every other pod and often the cloud metadata service.

Read

Demo: Lateral movement on a flat network

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace kcsa-network
    kubectl config set-context --current --namespace=kcsa-network
  2. A “compromised” pod scans for services in other namespaces through DNS and connects.

    Terminal window
    kubectl run attacker --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 -- sleep 3600
    kubectl wait --for=condition=Ready pod/attacker --timeout=60s
    kubectl exec attacker -- nslookup kube-dns.kube-system.svc.cluster.local
    kubectl exec attacker -- nslookup metrics-server.kube-system.svc.cluster.local
    kubectl exec attacker -- sh -c 'nc -zv -w 2 metrics-server.kube-system.svc.cluster.local 443'
  3. Traffic between pods is plain unless you add encryption.

    Terminal window
    kubectl create deployment web --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 -- /agnhost netexec --http-port=8080
    kubectl expose deployment web --port=8080 && kubectl rollout status deployment/web
    kubectl exec attacker -- wget -qO- -T 3 'http://web:8080/echo?msg=password=hunter2'; echo

What you should see: DNS answers for any Service in any namespace and the connection opens: there is no boundary by default. The echo travels as plain HTTP that anyone on the path can read. NetworkPolicy limits who can connect; mTLS or WireGuard (see the CKS pod-to-pod encryption demo) protects what is sent.

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace kcsa-network

Know

  • Secrets in etcd, environment variables, logs, and backups. Restrict, encrypt, and audit access.

Read

Demo: Steal Secrets with an over-privileged service account

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace kcsa-data
    kubectl config set-context --current --namespace=kcsa-data
  2. Someone gave the namespace’s default service account read access to Secrets.

    Terminal window
    kubectl create secret generic db --from-literal=password=FromInsideThePod
    kubectl create rolebinding default-reads-secrets --clusterrole=view --serviceaccount=kcsa-data:default
    kubectl create role secret-reader --verb=get,list --resource=secrets
    kubectl create rolebinding default-secret-reader --role=secret-reader --serviceaccount=kcsa-data:default
  3. Any pod in the namespace, compromised or not, can now read them.

    Terminal window
    kubectl run thief --rm -i --restart=Never --image=registry.k8s.io/kubectl:v1.37.2 -- get secret db -o go-template='{{.data.password | base64decode}}'; echo
  4. Fix: remove the binding and stop mounting the token.

    Terminal window
    kubectl delete rolebinding default-secret-reader
    kubectl patch serviceaccount default -p '{"automountServiceAccountToken":false}'
    kubectl run thief2 --rm -i --restart=Never --image=registry.k8s.io/kubectl:v1.37.2 -- get secret db

What you should see: The first pod prints the password using nothing but its mounted token. The built-in view role deliberately excludes Secrets, so it was the extra Role that leaked them. After the fix the pod has no token at all, so the in-cluster client has nothing to authenticate with.

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace kcsa-data

Know

  • Privileged pods, hostPath mounts, create pods permissions, and RBAC verbs like escalate, bind, and impersonate lead to cluster takeover.

Read

Demo: Create-pod rights become node root

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace kcsa-escalate
    kubectl config set-context --current --namespace=kcsa-escalate
  2. A pod that mounts the node and enters its namespaces.

    Terminal window
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: Pod
    metadata:
    name: escape
    spec:
    hostPID: true
    containers:
    - name: shell
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sleep", "3600"]
    securityContext:
    privileged: true
    EOF
    kubectl wait --for=condition=Ready pod/escape --timeout=60s
    kubectl exec escape -- nsenter -t 1 -m -u -i -n -p -- sh -c 'hostname; ls /etc/kubernetes'
  3. The fix is admission, not hope.

    Terminal window
    kubectl delete pod escape
    kubectl label namespace kcsa-escalate pod-security.kubernetes.io/enforce=baseline
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: Pod
    metadata:
    name: escape
    spec:
    hostPID: true
    containers:
    - name: shell
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sleep", "3600"]
    securityContext:
    privileged: true
    EOF

What you should see: nsenter into PID 1’s namespaces gives a shell on the node itself: its hostname and its /etc/kubernetes. Anyone allowed to create pods in a namespace without Pod Security enforcement can do this. With baseline enforced the same pod is rejected.

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace kcsa-escalate

On this site


Domain and competency names are from the CNCF KCSA curriculum (current curriculum (unversioned)), cncf/curriculum, licensed CC BY 4.0. Summaries, demos, and exercises are original to this site.