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
-
What runs and what listens.
Terminal window docker exec kind-worker systemctl list-units --type=service --state=running --no-pagerdocker exec kind-worker ss -tlnp -
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
-
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:nodeskubectl auth can-i list secrets -A --as=system:serviceaccount:kube-system:replicaset-controllerkubectl auth can-i create pods -n kube-system --as=system:serviceaccount:kube-system:replicaset-controllerkubectl auth can-i list secrets -A --as=system:serviceaccount:default:default -
Grant a group, not people, and grant per namespace.
Terminal window kubectl create namespace cks-iam-appkubectl create rolebinding app-team-edit -n cks-iam-app --clusterrole=edit --group=app-teamkubectl auth can-i delete deployments -n cks-iam-app --as=alice --as-group=app-teamkubectl 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:
kubectl delete namespace cks-iam-appMinimize external access to the network
Section titled “Minimize external access to the network”Know
- Firewall node ports, keep the API server private where possible, and restrict NodePort and LoadBalancer services.
Read
Demo: Find everything reachable from outside
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace cks-exposurekubectl config set-context --current --namespace=cks-exposure -
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=8080kubectl expose deployment web --name=web-np --type=NodePort --port=80 --target-port=8080kubectl 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)"' -
Restrict a LoadBalancer to known source ranges.
Terminal window kubectl expose deployment web --name=web-lb --type=LoadBalancer --port=80 --target-port=8080kubectl 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:
kubectl config set-context --current --namespace=defaultkubectl delete namespace cks-exposureAppropriately 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. UseseccompProfile: RuntimeDefaultor a Localhost profile.
Read
- Restrict a container’s access to resources with AppArmor
- Restrict a container’s syscalls with seccomp
Demo: A custom seccomp profile, and AppArmor on Linux hosts
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace cks-kernelkubectl config set-context --current --namespace=cks-kernel -
Write a seccomp profile that blocks
mkdirand install it on a worker.Terminal window cat > no-mkdir.json <<'EOF'{"defaultAction": "SCMP_ACT_ALLOW","syscalls": [{"names": ["mkdir", "mkdirat"], "action": "SCMP_ACT_ERRNO"}]}EOFdocker exec kind-worker mkdir -p /var/lib/kubelet/seccomp/profilesdocker cp no-mkdir.json kind-worker:/var/lib/kubelet/seccomp/profiles/no-mkdir.json -
Run one pod with it and one with the runtime default.
Terminal window kubectl apply -f - <<'EOF'apiVersion: v1kind: Podmetadata:name: seccomp-customspec:nodeName: kind-workersecurityContext:seccompProfile:type: LocalhostlocalhostProfile: profiles/no-mkdir.jsoncontainers:- name: appimage: registry.k8s.io/e2e-test-images/busybox:1.37.0-2command: ["sleep", "3600"]---apiVersion: v1kind: Podmetadata:name: seccomp-defaultspec:securityContext:seccompProfile:type: RuntimeDefaultcontainers:- name: appimage: registry.k8s.io/e2e-test-images/busybox:1.37.0-2command: ["sleep", "3600"]EOFkubectl wait --for=condition=Ready pod/seccomp-custom pod/seccomp-default --timeout=60skubectl exec seccomp-custom -- mkdir /tmp/x || truekubectl exec seccomp-default -- sh -c 'mkdir /tmp/x && echo created; grep Seccomp /proc/1/status' -
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,}EOFkubectl apply -f - <<'EOF'apiVersion: v1kind: Podmetadata:name: apparmorspec:containers:- name: appimage: registry.k8s.io/e2e-test-images/busybox:1.37.0-2command: ["sleep", "3600"]securityContext:appArmorProfile:type: LocalhostlocalhostProfile: k8s-deny-writeEOFkubectl wait --for=condition=Ready pod/apparmor --timeout=60skubectl exec apparmor -- cat /proc/1/attr/currentkubectl 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:
rm -f no-mkdir.jsonkubectl config set-context --current --namespace=defaultkubectl delete namespace cks-kernelPractise
- 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.