Skip to content
Hands-on IT IT Drills

Appendix B: Practice Exam

Goal: You solve 17 exam-style tasks in 120 minutes and grade yourself. · Duration: 120 min · Prerequisite: Days 0–17

Rules of Play

  • Time: 120 minutes, set a timer. You pass with 66 out of 100 points.
  • Allowed: the Kubernetes docs (kubernetes.io/docs), helm --help, kubectl explain. Not allowed: this book during the exam time.
  • Result files go into ~/pb/.
  • The solutions are at the end. Only read them after the time is up.
  • Cluster: the HA cluster from Day 16 with Traefik, MetalLB and the StorageClass nfs.

Preparation

On cp1 (the sabotage for tasks 14 and 15):

mkdir -p ~/pb
k create ns pb-broken
k create deployment api -n pb-broken \
  --image=registry.k8s.io/e2e-test-images/agnhost:2.53 \
  -- /agnhost netexec --http-port=8080
k set resources deployment api -n pb-broken --requests=memory=64Gi

On worker2:

sudo systemctl stop kubelet

Start the clock.

Exercise 1: Deployment and Service (4 points)

Create the namespace pb-app. In it, a Deployment web with nginx:1.29 and three replicas, plus a ClusterIP Service web on port 80.

Save your progress? Sign up for free

Exercise 2: Update and Rollback (5 points)

Update web to nginx:1.29-alpine with the change cause alpine. Then roll back to the previous version.

Save your progress? Sign up for free

Exercise 3: Configuration (4 points)

ConfigMap app-cfg with COLOR=blue, Secret app-sec with TOKEN=lab-token. A Pod cfg (busybox:1.37, sleep 3600) gets both as environment variables COLOR and TOKEN.

Save your progress? Sign up for free

Exercise 4: RBAC for CI (6 points)

ServiceAccount ci in pb-app. There, it may read Deployments (get, list, watch) and create and delete Pods – nothing else.

Save your progress? Sign up for free

Exercise 5: A New User (6 points)

For the user anna (group ops), generate a key and CSR, have Kubernetes sign it and save the certificate as ~/pb/anna.crt. anna may read everything cluster-wide.

Save your progress? Sign up for free

Exercise 6: NetworkPolicy (5 points)

Only Pods with the label role=client from pb-app may access the Pods of web, and only on port 80. Test with one Pod with and one without the label.

Save your progress? Sign up for free

Exercise 7: Ingress (5 points)

An Ingress web (class traefik) routes web.lab to the Service web. It must work despite Exercise 6: curl -H 'Host: web.lab' 172.16.0.242/ returns 200.

Save your progress? Sign up for free

Exercise 8: Gateway API (6 points)

Gateway pb-gw (class traefik, HTTP listener port 8000) and an HTTPRoute api that routes api.lab to the Service web.

Save your progress? Sign up for free

Exercise 9: Shared Storage (5 points)

PVC shared (500Mi, ReadWriteMany, default class) and a Pod writer that mounts it at /data and writes ok to /data/probe.txt.

Save your progress? Sign up for free

Exercise 10: StorageClass (4 points)

StorageClass nfs-retain: the same provisioner and the same parameters as nfs, but reclaim policy Retain and binding only with the first Pod.

Save your progress? Sign up for free

Exercise 11: Scheduling (5 points)

Label worker1 with tier=fast. A Pod fast (nginx:1.29, request 100m CPU and 64Mi, memory limit 128Mi) may only run on nodes with tier=fast – via node affinity.

Save your progress? Sign up for free

Exercise 12: Autoscaling (4 points)

web should scale between 2 and 6 replicas, target 60 % CPU.

Save your progress? Sign up for free

Exercise 13: etcd Backup (9 points)

Back up etcd to ~/pb/etcd-snap.db and verify the snapshot.

Save your progress? Sign up for free

Exercise 14: A Deployment Won’t Come Up (8 points)

The Deployment api in pb-broken is not fully available. Find the cause and fix it without deleting the Deployment.

Save your progress? Sign up for free

Exercise 15: A Node Doesn’t Report In (10 points)

A node is NotReady. Bring it back.

Save your progress? Sign up for free

Exercise 16: Helm (5 points)

Install the chart podinfo/podinfo in version 6.17.0 as release pi into the new namespace pb-helm, with two replicas.

Save your progress? Sign up for free

Exercise 17: Kustomize (9 points)

Under ~/pb/kust/base there is a Deployment api (nginx:1.28); generate it with --dry-run. An overlay ~/pb/kust/prod sets namespace pb-prod, prefix prod-, image nginx:1.29 and two replicas. Apply the overlay.

Stop the clock.

Save your progress? Sign up for free

Solutions

Solution 1

k create ns pb-app
k create deployment web -n pb-app --image=nginx:1.29 --replicas=3
k expose deployment web -n pb-app --port=80

Solution 2

k set image deployment/web -n pb-app nginx=nginx:1.29-alpine
k annotate deployment web -n pb-app kubernetes.io/change-cause="alpine"
k rollout status deployment web -n pb-app
k rollout undo deployment web -n pb-app
k get deploy web -n pb-app -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'

Expected (last line): nginx:1.29

Solution 3

k create configmap app-cfg -n pb-app --from-literal=COLOR=blue
k create secret generic app-sec -n pb-app --from-literal=TOKEN=lab-token
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: cfg
  namespace: pb-app
spec:
  containers:
  - name: cfg
    image: busybox:1.37
    command: ["sleep", "3600"]
    env:
    - name: COLOR
      valueFrom:
        configMapKeyRef:
          name: app-cfg
          key: COLOR
    - name: TOKEN
      valueFrom:
        secretKeyRef:
          name: app-sec
          key: TOKEN
EOF
k exec -n pb-app cfg -- env | grep -E 'COLOR|TOKEN' | sort

Expected:

COLOR=blue
TOKEN=lab-token

Solution 4

Warning, trap: k create role ci-role --verb=get,list,watch --resource=deployments.apps --verb=create,delete --resource=pods grants all verbs on all resources – Deployments would then be deletable. Two rules require YAML:

k create sa ci -n pb-app
cat <<'EOF' | k apply -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: ci-role
  namespace: pb-app
rules:
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["create", "delete"]
EOF
k create rolebinding ci-rb -n pb-app --role=ci-role --serviceaccount=pb-app:ci
k auth can-i list deployments -n pb-app --as=system:serviceaccount:pb-app:ci
k auth can-i delete deployments -n pb-app --as=system:serviceaccount:pb-app:ci

Expected: yes, then no.

Solution 5

cd ~/pb
openssl genrsa -out anna.key 2048
openssl req -new -key anna.key -subj "/CN=anna/O=ops" -out anna.csr
cat <<EOF | k apply -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
  name: anna
spec:
  request: $(base64 -w0 anna.csr)
  signerName: kubernetes.io/kube-apiserver-client
  usages: ["client auth"]
EOF
k certificate approve anna
k get csr anna -o jsonpath='{.status.certificate}' | base64 -d > anna.crt
openssl x509 -in anna.crt -noout -subject
k create clusterrolebinding anna-view --clusterrole=view --user=anna
k auth can-i list pods -A --as=anna

Expected:

subject=O = ops, CN = anna
yes

Solution 6

cat <<'EOF' | k apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: web-from-client
  namespace: pb-app
spec:
  podSelector:
    matchLabels:
      app: web
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: client
    ports:
    - port: 80
EOF
k run c1 -n pb-app --image=busybox:1.37 -l role=client -- sleep 3600
k run c2 -n pb-app --image=busybox:1.37 -- sleep 3600
k exec -n pb-app c1 -- wget -qO- -T3 web | grep -o '<title>.*</title>'
k exec -n pb-app c2 -- wget -qO- -T3 web

Expected:

<title>Welcome to nginx!</title>
wget: download timed out

Solution 7

k create ingress web -n pb-app --class=traefik --rule="web.lab/*=web:80"
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: web.lab' 172.16.0.242/

Expected: 504 – Traefik is not a role=client Pod, so the policy from Exercise 6 locks it out. Additionally allow the controller’s namespace:

cat <<'EOF' | k apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: web-from-traefik
  namespace: pb-app
spec:
  podSelector:
    matchLabels:
      app: web
  policyTypes:
  - Ingress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: traefik
    ports:
    - port: 80
EOF
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: web.lab' 172.16.0.242/

Expected: 200

Solution 8

cat <<'EOF' | k apply -f -
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: pb-gw
  namespace: pb-app
spec:
  gatewayClassName: traefik
  listeners:
  - name: http
    protocol: HTTP
    port: 8000
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: api
  namespace: pb-app
spec:
  parentRefs:
  - name: pb-gw
  hostnames: ["api.lab"]
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /
    backendRefs:
    - name: web
      port: 80
EOF
k get gateway pb-gw -n pb-app
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: api.lab' 172.16.0.242/

Expected: PROGRAMMED True and 200 (the policy from Solution 7 applies here too).

Solution 9

cat <<'EOF' | k apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: shared
  namespace: pb-app
spec:
  accessModes: ["ReadWriteMany"]
  resources:
    requests:
      storage: 500Mi
---
apiVersion: v1
kind: Pod
metadata:
  name: writer
  namespace: pb-app
spec:
  containers:
  - name: w
    image: busybox:1.37
    command: ["sh", "-c", "echo ok > /data/probe.txt; sleep 3600"]
    volumeMounts:
    - name: d
      mountPath: /data
  volumes:
  - name: d
    persistentVolumeClaim:
      claimName: shared
EOF
k exec -n pb-app writer -- cat /data/probe.txt

Expected: ok

Solution 10

cat <<'EOF' | k apply -f -
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-retain
provisioner: nfs.csi.k8s.io
parameters:
  server: 172.16.0.126
  share: /srv/nfs/k8s
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
EOF
k get sc nfs-retain

Expected (abridged):

NAME         PROVISIONER      RECLAIMPOLICY   VOLUMEBINDINGMODE
nfs-retain   nfs.csi.k8s.io   Retain          WaitForFirstConsumer

Solution 11

k label node worker1 tier=fast
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: fast
  namespace: pb-app
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: tier
            operator: In
            values: ["fast"]
  containers:
  - name: fast
    image: nginx:1.29
    resources:
      requests:
        cpu: 100m
        memory: 64Mi
      limits:
        memory: 128Mi
EOF
k get pod fast -n pb-app -o jsonpath='{.spec.nodeName}{"\n"}'

Expected: worker1

Solution 12

k set resources deployment web -n pb-app --requests=cpu=100m
k autoscale deployment web -n pb-app --cpu=60% --min=2 --max=6

Without a CPU request, the HPA would stay at <unknown>.

Solution 13

sudo 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 \
  snapshot save /home/ubuntu/pb/etcd-snap.db
sudo etcdutl snapshot status /home/ubuntu/pb/etcd-snap.db -w json

Expected (abridged):

Snapshot saved at /home/ubuntu/pb/etcd-snap.db
{"hash":3699137888,"revision":6933,"totalKey":1386,"totalSize":26976256,"version":"3.7.0
"}

~ does not work reliably with sudo – use absolute paths.

Solution 14

k get pods -n pb-broken
k describe pod -n pb-broken $(k get pods -n pb-broken \
  --field-selector=status.phase=Pending -o name | head -1) | grep FailedScheduling
k set resources deployment api -n pb-broken --requests=memory=64Mi
k rollout status deployment api -n pb-broken

Expected (abridged):

api-898b99767-pzxjd   0/1   Pending   0   2m32s
api-c4994b678-gfxrm   1/1   Running   0   2m32s
  Warning  FailedScheduling  ...  0/3 nodes are available: 3 Insufficient memory. ...
deployment "api" successfully rolled out

A 64Gi memory request – no node has that much. The old Pod kept running because the rollout never finished.

Solution 15

k get nodes

On the affected node (worker2):

systemctl is-active kubelet
systemctl is-enabled kubelet
sudo systemctl start kubelet

Expected: inactive, enabled – so only start is needed. On cp1: k wait --for=condition=Ready node/worker2 --timeout=120s.

Solution 16

helm install pi podinfo/podinfo -n pb-helm --create-namespace --version 6.17.0 \
  --set replicaCount=2
k get deploy -n pb-helm

Expected: pi-podinfo with 2/2 after a short while.

Solution 17

mkdir -p ~/pb/kust/base ~/pb/kust/prod
cd ~/pb/kust
k create deployment api --image=nginx:1.28 $do > base/deploy.yaml
printf 'resources:\n- deploy.yaml\n' > base/kustomization.yaml
cat > prod/kustomization.yaml <<'EOF'
resources:
- ../base
namespace: pb-prod
namePrefix: prod-
images:
- name: nginx
  newTag: "1.29"
replicas:
- name: api
  count: 2
EOF
k create ns pb-prod
k apply -k prod
k get deploy -n pb-prod -o custom-columns=NAME:.metadata.name,REPLICAS:.spec.replicas

Expected:

deployment.apps/prod-api created
NAME       REPLICAS
prod-api   2

Scoring

Count the points of the tasks that are completely and verifiably solved – there are no half points.

Points Assessment
85–100 exam-ready
66–84 passed, but barely: review your weak spots
under 66 review the days covering the missed tasks and start over

Clean up:

k delete ns pb-app pb-broken pb-helm pb-prod
k delete clusterrolebinding anna-view
k delete csr anna
k delete sc nfs-retain
k label node worker1 tier-

Checkpoint

k get ns | grep -c '^pb-'

Expected: 0

Good luck in the exam.