Skip to content

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.

Know

  • Label namespaces for the restricted or baseline profile and fix workloads until they pass.

Read

Demo: Enforce restricted and fix a workload to pass

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

    Terminal window
    kubectl create namespace cks-pss
    kubectl config set-context --current --namespace=cks-pss
  2. 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.37
    kubectl create deployment web --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 -- /agnhost netexec --http-port=8080
    sleep 5; kubectl get deployment web
    kubectl get events --field-selector reason=FailedCreate | tail -2
  3. 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:

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

On this site

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

  1. Create a separate cluster whose API server encrypts Secrets with a key you generate.

    Terminal window
    mkdir -p enc
    cat > enc/encryption.yaml <<EOF
    apiVersion: apiserver.config.k8s.io/v1
    kind: EncryptionConfiguration
    resources:
    - resources: ["secrets"]
    providers:
    - aescbc:
    keys:
    - name: key1
    secret: $(head -c 32 /dev/urandom | base64)
    - identity: {}
    EOF
    cat > enc/kind.yaml <<'EOF'
    kind: Cluster
    apiVersion: kind.x-k8s.io/v1alpha4
    nodes:
    - role: control-plane
    kubeadmConfigPatches:
    - |
    kind: ClusterConfiguration
    apiServer:
    extraArgs:
    encryption-provider-config: /etc/kubernetes/enc/encryption.yaml
    extraVolumes:
    - name: enc
    hostPath: /etc/kubernetes/enc
    mountPath: /etc/kubernetes/enc
    readOnly: true
    pathType: DirectoryOrCreate
    extraMounts:
    - hostPath: ./enc/encryption.yaml
    containerPath: /etc/kubernetes/enc/encryption.yaml
    readOnly: true
    EOF
    kind create cluster --name enc --config enc/kind.yaml --image kindest/node:v1.37.0@sha256:a1ed56cfb0e7b93589bdf97c8cd566405a265939e3620fc4f5de89adff580ae5
  2. Store a Secret and read it straight from etcd.

    Terminal window
    kubectl --context kind-enc create secret generic s1 --from-literal=password=NotInPlainText
    kubectl --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 -4
    kubectl --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:

Terminal window
kind delete cluster --name enc && rm -rf enc
kubectl config use-context kind-kind

Practise

  • Enable aescbc or secretbox encryption, then confirm with etcdctl get that 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

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

    Terminal window
    kubectl create namespace cks-sandbox
    kubectl config set-context --current --namespace=cks-sandbox
  2. Define a RuntimeClass for gVisor and schedule a pod with it.

    Terminal window
    kubectl apply -f - <<'EOF'
    apiVersion: node.k8s.io/v1
    kind: RuntimeClass
    metadata:
    name: gvisor
    handler: runsc
    scheduling:
    nodeSelector:
    sandbox.example/gvisor: "true"
    EOF
    kubectl label node kind-worker2 sandbox.example/gvisor=true
    kubectl 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 wide
    kubectl 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:

Terminal window
kubectl delete runtimeclass gvisor
kubectl label node kind-worker2 sandbox.example/gvisor-
kubectl config set-context --current --namespace=default
kubectl delete namespace cks-sandbox

Practise

  • 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

  1. Create a cluster without the default CNI and install Cilium 1.20.5 with WireGuard encryption.

    Terminal window
    cat > cilium-kind.yaml <<'EOF'
    kind: Cluster
    apiVersion: kind.x-k8s.io/v1alpha4
    nodes:
    - role: control-plane
    - role: worker
    - role: worker
    networking:
    disableDefaultCNI: true
    EOF
    kind create cluster --name cilium --config cilium-kind.yaml --image kindest/node:v1.37.0@sha256:a1ed56cfb0e7b93589bdf97c8cd566405a265939e3620fc4f5de89adff580ae5
    helm 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=wireguard
    kubectl --context kind-cilium -n kube-system rollout status daemonset/cilium --timeout=300s
  2. 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:

Terminal window
kind delete cluster --name cilium && rm -f cilium-kind.yaml
kubectl config use-context kind-kind

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.