CKS: Minimize Microservice Vulnerabilities
20% 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 appropriate pod security standards
Section titled “Use appropriate pod security standards”Know
- Label namespaces for the
restrictedorbaselineprofile and fix workloads until they pass.
Read
Demo: Enforce restricted and fix a workload to pass
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace cks-psskubectl config set-context --current --namespace=cks-pss -
Enforce
restricted(pinned to v1.37) and try a typical Deployment.Terminal window kubectl label namespace cks-pss pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=v1.37kubectl create deployment web --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 -- /agnhost netexec --http-port=8080sleep 5; kubectl get deployment webkubectl get events --field-selector reason=FailedCreate | tail -2 -
Patch it to comply.
Terminal window kubectl patch deployment web --type=strategic -p '{"spec":{"template":{"spec":{"securityContext":{"runAsNonRoot":true,"runAsUser":65534,"seccompProfile":{"type":"RuntimeDefault"}},"containers":[{"name":"agnhost","securityContext":{"allowPrivilegeEscalation":false,"capabilities":{"drop":["ALL"]}}}]}}}}'kubectl rollout status deployment/web --timeout=60s
What you should see: The Deployment is created but has 0 ready pods: the ReplicaSet’s FailedCreate event says the pod violates PodSecurity "restricted:v1.37". That indirection is easy to miss in an exam. After the patch the rollout completes.
Clean up:
kubectl config set-context --current --namespace=defaultkubectl delete namespace cks-pssOn this site
Manage kubernetes secrets
Section titled “Manage kubernetes secrets”Know
- Enable encryption at rest with an EncryptionConfiguration and rotate the key; restrict RBAC on Secrets.
Read
Demo: Turn on encryption at rest and prove it
-
Create a separate cluster whose API server encrypts Secrets with a key you generate.
Terminal window mkdir -p enccat > enc/encryption.yaml <<EOFapiVersion: apiserver.config.k8s.io/v1kind: EncryptionConfigurationresources:- resources: ["secrets"]providers:- aescbc:keys:- name: key1secret: $(head -c 32 /dev/urandom | base64)- identity: {}EOFcat > enc/kind.yaml <<'EOF'kind: ClusterapiVersion: kind.x-k8s.io/v1alpha4nodes:- role: control-planekubeadmConfigPatches:- |kind: ClusterConfigurationapiServer:extraArgs:encryption-provider-config: /etc/kubernetes/enc/encryption.yamlextraVolumes:- name: enchostPath: /etc/kubernetes/encmountPath: /etc/kubernetes/encreadOnly: truepathType: DirectoryOrCreateextraMounts:- hostPath: ./enc/encryption.yamlcontainerPath: /etc/kubernetes/enc/encryption.yamlreadOnly: trueEOFkind create cluster --name enc --config enc/kind.yaml --image kindest/node:v1.37.0@sha256:a1ed56cfb0e7b93589bdf97c8cd566405a265939e3620fc4f5de89adff580ae5 -
Store a Secret and read it straight from etcd.
Terminal window kubectl --context kind-enc create secret generic s1 --from-literal=password=NotInPlainTextkubectl --context kind-enc -n kube-system exec etcd-enc-control-plane -- etcdctl --endpoints=https://127.0.0.1:2379 \--cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key \get /registry/secrets/default/s1 | hexdump -C | head -4kubectl --context kind-enc get secret s1 -o jsonpath='{.data.password}' | base64 -d; echo
What you should see: The etcd value starts with k8s:enc:aescbc:v1:key1: followed by ciphertext, while the API still returns the plain value to authorized clients. Encryption at rest protects etcd disks and backups, not API access. Secrets created before you turn it on stay plain until rewritten (kubectl get secrets -A -o json | kubectl replace -f -). A KMS provider keeps the key outside the node; a local aescbc key sits next to the data it protects.
Clean up:
kind delete cluster --name enc && rm -rf enckubectl config use-context kind-kindPractise
- Enable aescbc or secretbox encryption, then confirm with
etcdctl getthat a Secret is no longer plaintext.
Understand and implement isolation techniques (multi-tenancy, sandboxed containers, etc.)
Section titled “Understand and implement isolation techniques (multi-tenancy, sandboxed containers, etc.)”Know
- RuntimeClass selects a sandboxed runtime such as gVisor or Kata Containers for untrusted workloads.
Read
Demo: Sandboxed runtimes with RuntimeClass
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace cks-sandboxkubectl config set-context --current --namespace=cks-sandbox -
Define a RuntimeClass for gVisor and schedule a pod with it.
Terminal window kubectl apply -f - <<'EOF'apiVersion: node.k8s.io/v1kind: RuntimeClassmetadata:name: gvisorhandler: runscscheduling:nodeSelector:sandbox.example/gvisor: "true"EOFkubectl label node kind-worker2 sandbox.example/gvisor=truekubectl run sandboxed --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 --overrides='{"spec":{"runtimeClassName":"gvisor"}}' -- sh -c 'dmesg | head -3; sleep 3600'sleep 20; kubectl get pod sandboxed -o widekubectl describe pod sandboxed | sed -n '/Events:/,$p' | tail -3
What you should see: The RuntimeClass’s scheduling.nodeSelector is merged into the pod, so it lands on kind-worker2. It then fails to start because kind nodes don’t have gVisor’s runsc installed. On a node that has it (install runsc and register it as a containerd runtime handler named runsc), the pod runs and dmesg prints gVisor’s own kernel messages instead of the host’s.
Clean up:
kubectl delete runtimeclass gvisorkubectl label node kind-worker2 sandbox.example/gvisor-kubectl config set-context --current --namespace=defaultkubectl delete namespace cks-sandboxPractise
- Create a RuntimeClass and run a pod with it.
Implement Pod-to-Pod encryption (Cilium, Istio)
Section titled “Implement Pod-to-Pod encryption (Cilium, Istio)”Know
- Encrypt traffic between pods with a CNI that supports transparent encryption or with a service mesh in mTLS mode.
Read
Demo: WireGuard between nodes with Cilium
-
Create a cluster without the default CNI and install Cilium 1.20.5 with WireGuard encryption.
Terminal window cat > cilium-kind.yaml <<'EOF'kind: ClusterapiVersion: kind.x-k8s.io/v1alpha4nodes:- role: control-plane- role: worker- role: workernetworking:disableDefaultCNI: trueEOFkind create cluster --name cilium --config cilium-kind.yaml --image kindest/node:v1.37.0@sha256:a1ed56cfb0e7b93589bdf97c8cd566405a265939e3620fc4f5de89adff580ae5helm repo add cilium https://helm.cilium.io/helm install cilium cilium/cilium --version 1.20.5 --kube-context kind-cilium --namespace kube-system \--set image.pullPolicy=IfNotPresent --set ipam.mode=kubernetes \--set encryption.enabled=true --set encryption.type=wireguardkubectl --context kind-cilium -n kube-system rollout status daemonset/cilium --timeout=300s -
Check that encryption is on and every peer is known.
Terminal window kubectl --context kind-cilium -n kube-system exec ds/cilium -c cilium-agent -- cilium-dbg status | grep Encryption
What you should see: The status line reads Encryption: Wireguard [cilium_wg0 (Pubkey: ..., Port: 51871, Peers: 2)]: one peer per other node. Pod traffic that crosses nodes now travels inside WireGuard; traffic between pods on the same node isn’t encrypted because it never leaves the host. Istio’s ambient or sidecar mTLS does the same job with per-workload identities.
Note: WireGuard needs Linux kernel 5.6 or newer on the host (kind nodes share it).
Clean up:
kind delete cluster --name cilium && rm -f cilium-kind.yamlkubectl config use-context kind-kindDomain 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.