Skip to content

CKS: System Hardening

10% 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.

Minimize host OS footprint (reduce attack surface)

Section titled “Minimize host OS footprint (reduce attack surface)”

Know

  • Remove unused packages and services from nodes, close unused ports, and prefer minimal, immutable node images.

Read

Demo: Inventory a node

  1. What runs and what listens.

    Terminal window
    docker exec kind-worker systemctl list-units --type=service --state=running --no-pager
    docker exec kind-worker ss -tlnp
  2. What is installed, and which binaries carry the setuid bit.

    Terminal window
    docker exec kind-worker sh -c 'dpkg -l | wc -l'
    docker exec kind-worker sh -c 'find / -xdev -perm -4000 -type f 2>/dev/null'

What you should see: A node should run the kubelet and container runtime and little else. The kind node image is Debian with a few hundred packages because it is built for convenience; a hardened node OS (or a minimal one built for Kubernetes) has far fewer, and every setuid binary or listening service you remove is one less path to root.

Using least-privilege identity and access management

Section titled “Using least-privilege identity and access management”

Know

  • Nodes and workloads should hold only the cloud permissions they need; avoid node-level credentials shared with every pod.

Read

Demo: Each identity gets exactly its job

  1. Compare what a kubelet, a controller, and a normal workload may do.

    Terminal window
    kubectl auth can-i list secrets -A --as=system:node:kind-worker --as-group=system:nodes
    kubectl auth can-i list secrets -A --as=system:serviceaccount:kube-system:replicaset-controller
    kubectl auth can-i create pods -n kube-system --as=system:serviceaccount:kube-system:replicaset-controller
    kubectl auth can-i list secrets -A --as=system:serviceaccount:default:default
  2. Grant a group, not people, and grant per namespace.

    Terminal window
    kubectl create namespace cks-iam-app
    kubectl create rolebinding app-team-edit -n cks-iam-app --clusterrole=edit --group=app-team
    kubectl auth can-i delete deployments -n cks-iam-app --as=alice --as-group=app-team
    kubectl auth can-i delete deployments -n kube-system --as=alice --as-group=app-team

What you should see: The node can’t list Secrets cluster-wide, and the ReplicaSet controller can create pods but not read Secrets. Binding to a group means people join and leave through your identity provider instead of through RBAC edits. The same principle applies to the cloud IAM roles your nodes and workloads use.

Clean up:

Terminal window
kubectl delete namespace cks-iam-app

Know

  • Firewall node ports, keep the API server private where possible, and restrict NodePort and LoadBalancer services.

Read

Demo: Find everything reachable from outside

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

    Terminal window
    kubectl create namespace cks-exposure
    kubectl config set-context --current --namespace=cks-exposure
  2. Create a few exposures, then find them all.

    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 --name=web-np --type=NodePort --port=80 --target-port=8080
    kubectl run hostport --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 --overrides='{"spec":{"containers":[{"name":"hostport","image":"registry.k8s.io/e2e-test-images/agnhost:2.66.1","args":["netexec","--http-port=8080"],"ports":[{"containerPort":8080,"hostPort":18080}]}]}}'
    kubectl get services -A -o json | jq -r '.items[] | select(.spec.type=="NodePort" or .spec.type=="LoadBalancer") | "\(.metadata.namespace)/\(.metadata.name)\t\(.spec.type)\t\([.spec.ports[].nodePort] | join(","))"'
    kubectl get pods -A -o json | jq -r '.items[] | select(.spec.hostNetwork==true or any(.spec.containers[].ports[]?; .hostPort)) | "\(.metadata.namespace)/\(.metadata.name)\thostNetwork=\(.spec.hostNetwork // false)"'
  3. Restrict a LoadBalancer to known source ranges.

    Terminal window
    kubectl expose deployment web --name=web-lb --type=LoadBalancer --port=80 --target-port=8080
    kubectl patch service web-lb -p '{"spec":{"loadBalancerSourceRanges":["203.0.113.0/24"]}}'
    kubectl get service web-lb -o jsonpath='{.spec.loadBalancerSourceRanges}{"\n"}'

What you should see: The queries list your NodePort, the hostPort pod, and the system pods that use host networking (kube-proxy, kindnet, and the control plane). Every entry is a port open on node addresses. loadBalancerSourceRanges asks the cloud load balancer to filter by source; whether it is enforced depends on the provider.

Clean up:

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

Appropriately use kernel hardening tools such as AppArmor, seccomp

Section titled “Appropriately use kernel hardening tools such as AppArmor, seccomp”

Know

  • Load an AppArmor profile on the node, then reference it in the pod securityContext.appArmorProfile. Use seccompProfile: RuntimeDefault or a Localhost profile.

Read

Demo: A custom seccomp profile, and AppArmor on Linux hosts

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

    Terminal window
    kubectl create namespace cks-kernel
    kubectl config set-context --current --namespace=cks-kernel
  2. Write a seccomp profile that blocks mkdir and install it on a worker.

    Terminal window
    cat > no-mkdir.json <<'EOF'
    {
    "defaultAction": "SCMP_ACT_ALLOW",
    "syscalls": [
    {"names": ["mkdir", "mkdirat"], "action": "SCMP_ACT_ERRNO"}
    ]
    }
    EOF
    docker exec kind-worker mkdir -p /var/lib/kubelet/seccomp/profiles
    docker cp no-mkdir.json kind-worker:/var/lib/kubelet/seccomp/profiles/no-mkdir.json
  3. Run one pod with it and one with the runtime default.

    Terminal window
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: Pod
    metadata:
    name: seccomp-custom
    spec:
    nodeName: kind-worker
    securityContext:
    seccompProfile:
    type: Localhost
    localhostProfile: profiles/no-mkdir.json
    containers:
    - name: app
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sleep", "3600"]
    ---
    apiVersion: v1
    kind: Pod
    metadata:
    name: seccomp-default
    spec:
    securityContext:
    seccompProfile:
    type: RuntimeDefault
    containers:
    - name: app
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sleep", "3600"]
    EOF
    kubectl wait --for=condition=Ready pod/seccomp-custom pod/seccomp-default --timeout=60s
    kubectl exec seccomp-custom -- mkdir /tmp/x || true
    kubectl exec seccomp-default -- sh -c 'mkdir /tmp/x && echo created; grep Seccomp /proc/1/status'
  4. AppArmor (Linux hosts with AppArmor enabled, such as Ubuntu): load a profile on the host, which kind nodes share, and use it.

    Terminal window
    sudo apparmor_parser -r <<'EOF'
    #include <tunables/global>
    profile k8s-deny-write flags=(attach_disconnected) {
    #include <abstractions/base>
    file,
    deny /** w,
    }
    EOF
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: Pod
    metadata:
    name: apparmor
    spec:
    containers:
    - name: app
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sleep", "3600"]
    securityContext:
    appArmorProfile:
    type: Localhost
    localhostProfile: k8s-deny-write
    EOF
    kubectl wait --for=condition=Ready pod/apparmor --timeout=60s
    kubectl exec apparmor -- cat /proc/1/attr/current
    kubectl exec apparmor -- touch /tmp/x || true

What you should see: mkdir fails with Operation not permitted under the custom profile and works under RuntimeDefault; Seccomp: 2 means a filter is active. With AppArmor, /proc/1/attr/current shows k8s-deny-write (enforce) and the write is denied. If the AppArmor pod is rejected or stuck, your host doesn’t have AppArmor; practise that part on an Ubuntu VM.

Clean up:

Terminal window
rm -f no-mkdir.json
kubectl config set-context --current --namespace=default
kubectl delete namespace cks-kernel

Practise

  • Load an AppArmor profile that denies file writes and prove a pod using it cannot write.

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.