CKS: Cluster Hardening
15% of the CKS exam, 4 competencies. Back to the CKS 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.
Use Role Based Access Controls to minimize exposure
Section titled “Use Role Based Access Controls to minimize exposure”Know
- Namespace-scoped Roles, no wildcards, and no
cluster-adminfor daily work.
Read
Demo: Find over-privileged subjects
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace cks-rbackubectl config set-context --current --namespace=cks-rbac -
Who has cluster-admin, and through which binding?
Terminal window kubectl get clusterrolebindings -o json | jq -r '.items[] | select(.roleRef.name=="cluster-admin") | .metadata.name as $b | (.subjects // [])[] | "\($b)\t\(.kind)\t\(.namespace // "-")\t\(.name)"' -
Find roles with wildcards or the escalation verbs.
Terminal window kubectl get clusterroles,roles -A -o json | jq -r '.items[] | select(.metadata.name | startswith("system:") | not) | select(any(.rules[]?; ((.verbs // []) | index("*") or index("escalate") or index("bind") or index("impersonate")))) | "\(.kind)/\(.metadata.name)"' -
Replace a broad grant with a narrow one, and prove it.
Terminal window kubectl create serviceaccount deployerkubectl create role deployer --verb=get,list,watch,create,update,patch --resource=deployments.appskubectl create rolebinding deployer --role=deployer --serviceaccount=cks-rbac:deployerkubectl auth can-i --list --as=system:serviceaccount:cks-rbac:deployerkubectl auth can-i create rolebindings --as=system:serviceaccount:cks-rbac:deployer
What you should see: On kind, cluster-admin is bound to the system:masters and kubeadm:cluster-admins groups. The second query lists non-system roles that grant *, escalate, bind, or impersonate: each lets its holder gain permissions it doesn’t have. The deployer can manage Deployments and nothing that changes access.
Clean up:
kubectl config set-context --current --namespace=defaultkubectl delete namespace cks-rbacOn this site
Exercise caution in using service accounts e.g. disable defaults, minimize permissions on newly created ones
Section titled “Exercise caution in using service accounts e.g. disable defaults, minimize permissions on newly created ones”Know
- Set
automountServiceAccountToken: falseon the default ServiceAccount and on pods that never call the API.
Read
Demo: Default service accounts that do nothing
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace cks-sakubectl config set-context --current --namespace=cks-sa -
Turn off token mounting on the default account.
Terminal window kubectl patch serviceaccount default -p '{"automountServiceAccountToken":false}'kubectl run app --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 -- sleep 3600 && kubectl wait --for=condition=Ready pod/app --timeout=60skubectl exec app -- ls /var/run/secrets/kubernetes.io/serviceaccount 2>&1 || true -
Workloads that need the API get their own account, a short-lived token, and a specific audience.
Terminal window kubectl create serviceaccount reporterkubectl apply -f - <<'EOF'apiVersion: v1kind: Podmetadata:name: reporterspec:serviceAccountName: reporterautomountServiceAccountToken: falsecontainers:- name: appimage: registry.k8s.io/e2e-test-images/busybox:1.37.0-2command: ["sleep", "3600"]volumeMounts:- name: tokenmountPath: /var/run/secrets/tokensreadOnly: truevolumes:- name: tokenprojected:sources:- serviceAccountToken:path: api-tokenexpirationSeconds: 600audience: reporting-serviceEOFkubectl wait --for=condition=Ready pod/reporter --timeout=60skubectl exec reporter -- cat /var/run/secrets/tokens/api-token | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null; echo -
Look for long-lived token Secrets that shouldn’t exist anymore.
Terminal window kubectl get secrets -A --field-selector type=kubernetes.io/service-account-token
What you should see: With automount off, the pod has no token. The projected token’s payload shows aud: ["reporting-service"] and an expiry 10 minutes out; the kubelet refreshes it before it expires. There should be no service-account-token Secrets unless someone created one by hand, and each one is a token that never expires.
Clean up:
kubectl config set-context --current --namespace=defaultkubectl delete namespace cks-saRestrict access to Kubernetes API
Section titled “Restrict access to Kubernetes API”Know
- Disable anonymous auth where possible, restrict network access to the API server, and audit who can impersonate.
Read
Demo: What reaches the API, and as whom
-
Find where the API server listens and what anonymous users can do.
Terminal window kubectl cluster-infodocker port kind-control-planedocker exec kind-control-plane curl -sk https://127.0.0.1:6443/livez; echodocker exec kind-control-plane curl -sk -o /dev/null -w '%{http_code}\n' https://127.0.0.1:6443/apikubectl get clusterrolebindings -o json | jq -r '.items[] | select(any(.subjects[]?; .name=="system:anonymous" or .name=="system:unauthenticated")) | .metadata.name' -
Show what NodeRestriction stops a node identity from doing.
Terminal window kubectl auth can-i get secrets -n kube-system --as=system:node:kind-worker --as-group=system:nodeskubectl label node kind-worker2 test=1 --as=system:node:kind-worker --as-group=system:nodes
What you should see: kind publishes the API server only on 127.0.0.1 of your machine. Anonymous requests reach /livez and get 403 on /api; the only anonymous binding is system:public-info-viewer. A node identity can’t read arbitrary Secrets and can’t modify another node: the Node authorizer and NodeRestriction admission plugin confine a stolen kubelet credential to its own node.
Upgrade Kubernetes to avoid vulnerabilities
Section titled “Upgrade Kubernetes to avoid vulnerabilities”Know
- Stay on a supported minor version and apply patch releases; upgrade with kubeadm one minor version at a time.
Read
Demo: Know your versions and the CVEs that apply
-
Read every version in the cluster.
Terminal window kubectl versionkubectl get nodes -o custom-columns=NAME:.metadata.name,KUBELET:.status.nodeInfo.kubeletVersion,RUNTIME:.status.nodeInfo.containerRuntimeVersion,KERNEL:.status.nodeInfo.kernelVersion -
Check the official Kubernetes CVE feed.
Terminal window curl -sL https://k8s.io/docs/reference/issues-security/official-cve-feed/index.json \| jq -r '.items[:10][] | "\(.date_published[:10]) \(.id) \(.summary)"' -
See the upgrade path kubeadm proposes.
Terminal window docker exec kind-control-plane kubeadm upgrade plan 2>&1 | tail -15
What you should see: Control plane, kubelet, and runtime versions side by side; the kubelet may lag the API server by up to three minor versions but never lead it. The CVE feed lists recent Kubernetes vulnerabilities with dates; check each one against your versions. Patch releases ship security fixes, so falling behind on patches is the most common avoidable exposure.
On this site
Domain and competency names are from the CNCF CKS curriculum (v1.37), cncf/curriculum, licensed CC BY 4.0. Summaries, demos, and exercises are original to this site.