Skip to content

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-admin for daily work.

Read

Demo: Find over-privileged subjects

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

    Terminal window
    kubectl create namespace cks-rbac
    kubectl config set-context --current --namespace=cks-rbac
  2. 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)"'
  3. 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)"'
  4. Replace a broad grant with a narrow one, and prove it.

    Terminal window
    kubectl create serviceaccount deployer
    kubectl create role deployer --verb=get,list,watch,create,update,patch --resource=deployments.apps
    kubectl create rolebinding deployer --role=deployer --serviceaccount=cks-rbac:deployer
    kubectl auth can-i --list --as=system:serviceaccount:cks-rbac:deployer
    kubectl 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:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace cks-rbac

On 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: false on the default ServiceAccount and on pods that never call the API.

Read

Demo: Default service accounts that do nothing

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

    Terminal window
    kubectl create namespace cks-sa
    kubectl config set-context --current --namespace=cks-sa
  2. 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=60s
    kubectl exec app -- ls /var/run/secrets/kubernetes.io/serviceaccount 2>&1 || true
  3. Workloads that need the API get their own account, a short-lived token, and a specific audience.

    Terminal window
    kubectl create serviceaccount reporter
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: Pod
    metadata:
    name: reporter
    spec:
    serviceAccountName: reporter
    automountServiceAccountToken: false
    containers:
    - name: app
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sleep", "3600"]
    volumeMounts:
    - name: token
    mountPath: /var/run/secrets/tokens
    readOnly: true
    volumes:
    - name: token
    projected:
    sources:
    - serviceAccountToken:
    path: api-token
    expirationSeconds: 600
    audience: reporting-service
    EOF
    kubectl wait --for=condition=Ready pod/reporter --timeout=60s
    kubectl exec reporter -- cat /var/run/secrets/tokens/api-token | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null; echo
  4. 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:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace cks-sa

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

  1. Find where the API server listens and what anonymous users can do.

    Terminal window
    kubectl cluster-info
    docker port kind-control-plane
    docker exec kind-control-plane curl -sk https://127.0.0.1:6443/livez; echo
    docker exec kind-control-plane curl -sk -o /dev/null -w '%{http_code}\n' https://127.0.0.1:6443/api
    kubectl get clusterrolebindings -o json | jq -r '.items[] | select(any(.subjects[]?; .name=="system:anonymous" or .name=="system:unauthenticated")) | .metadata.name'
  2. 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:nodes
    kubectl 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

  1. Read every version in the cluster.

    Terminal window
    kubectl version
    kubectl get nodes -o custom-columns=NAME:.metadata.name,KUBELET:.status.nodeInfo.kubeletVersion,RUNTIME:.status.nodeInfo.containerRuntimeVersion,KERNEL:.status.nodeInfo.kernelVersion
  2. 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)"'
  3. 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.