Free to read · CKA · Cluster architecture and kubectl fluency

kubectl fluency for the exam

How the control plane and the nodes of a kubeadm cluster work together, how the API is organised, and the kubectl and exam habits that make every later module faster.

Lesson · 14 min. This module is free on this site. The rest of the course, the quizzes and the labs open with a free account.

Why this matters

The CKA is a speed test as much as a knowledge test. Fifteen to twenty tasks in two hours leaves six to eight minutes each, including reading and checking. Candidates who type whole manifests from scratch run out of time. Candidates who generate a skeleton, edit three lines and apply it do not.

Generate, don't type

Most objects have an imperative generator. Use it to create the object directly, or with --dry-run=client -o yaml to produce a file you can edit.

export do="--dry-run=client -o yaml"     # a common shortcut; type it once per shell

kubectl run tmp --image=busybox:1.36 --restart=Never --rm -it -- sh
kubectl run web --image=nginx:1.27 --labels=app=web --port=80 $do > web.yaml
kubectl create deployment api --image=nginx:1.27 --replicas=3 $do > api.yaml
kubectl expose deployment api --port=80 --target-port=8080 --name=api-svc
kubectl create service nodeport web --tcp=80:80 --node-port=30080
kubectl create configmap app-cfg --from-literal=MODE=prod --from-file=app.conf
kubectl create secret generic db --from-literal=password='S3cret!'
kubectl create job once --image=busybox:1.36 -- echo hello
kubectl create cronjob tick --image=busybox:1.36 --schedule="*/5 * * * *" -- date
kubectl create job tick-now --from=cronjob/tick
kubectl create serviceaccount builder -n ci
kubectl create role pod-reader --verb=get,list,watch --resource=pods -n dev
kubectl create rolebinding pod-reader-b --role=pod-reader --serviceaccount=ci:builder -n dev
kubectl create ingress web --rule="shop.internal/=web:80" --class=nginx
kubectl create priorityclass high --value=1000 --description="critical apps"
kubectl create poddisruptionbudget web-pdb --selector=app=web --min-available=2

Then change what the generator cannot set (probes, volumes, affinity) in the file:

vim api.yaml
kubectl apply -f api.yaml

Change things quickly

kubectl scale deployment api --replicas=5
kubectl set image deployment/api nginx=nginx:1.28
kubectl set resources deployment api --requests=cpu=100m,memory=64Mi --limits=memory=128Mi
kubectl set serviceaccount deployment api builder
kubectl label node node01 disk=ssd
kubectl taint node node01 dedicated=gpu:NoSchedule
kubectl taint node node01 dedicated=gpu:NoSchedule-        # trailing minus removes it
kubectl annotate deployment api owner=team-a
kubectl rollout status deployment api
kubectl rollout undo deployment api
kubectl patch deployment api -p '{"spec":{"revisionHistoryLimit":5}}'

Some fields of a running pod cannot change. kubectl edit then saves your version to a file in /tmp and refuses. Replace the pod instead:

kubectl replace --force -f /tmp/kubectl-edit-1234.yaml

--force deletes and recreates the object. That is fine for a pod in a lab; think before doing it to anything with state.

Read output the way the grader does

Many tasks ask you to write a value to a file. Use output options to get exactly that value, nothing more.

kubectl get pods -o wide                               # node and pod IP
kubectl get pods -A --sort-by=.metadata.creationTimestamp
kubectl get pods --field-selector=status.phase=Pending -A
kubectl get pod web -o jsonpath='{.status.podIP}{"\n"}'
kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.nodeInfo.kubeletVersion}{"\n"}{end}'
kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName,IMAGE:.spec.containers[*].image
kubectl get deploy api -o yaml | grep -A3 strategy

jsonpath tips:

  • Wrap the expression in single quotes so the shell leaves it alone.
  • {range .items[*]}...{end} loops over a list; add {"\n"} for line breaks.
  • Filters look like {.items[?(@.metadata.name=="web")].status.podIP}.
  • When the answer must be one name per line, -o name gives pod/web; strip the prefix with cut -d/ -f2, or use -o custom-columns=:metadata.name --no-headers.

Logs and events

The curriculum calls these "container output streams". Containers write to stdout and stderr; the kubelet stores those streams and kubectl logs reads them.

kubectl logs web                       # single-container pod
kubectl logs web -c sidecar            # pick the container
kubectl logs web --previous            # the crashed instance before the restart
kubectl logs deploy/api --tail=20      # one pod of the Deployment
kubectl logs -l app=api --all-containers --prefix
kubectl logs web --since=10m | grep -i error > /opt/errors.txt

Events explain what the cluster did to your objects: scheduling, pulls, probe failures, kills.

kubectl get events -n shop --sort-by=.metadata.creationTimestamp
kubectl events -n shop --types=Warning
kubectl describe pod web | sed -n '/Events:/,$p'

Events expire (one hour by default), so read them early when you investigate.

Resource usage

kubectl top needs the metrics-server add-on. It shows current CPU and memory use, not requests or limits.

kubectl top nodes
kubectl top pods -n shop --sort-by=memory
kubectl top pods -A --sort-by=cpu --containers | head

If top says the metrics API is not available, check that the metrics-server Deployment in kube-system is running and that its APIService is Available:

kubectl get apiservice v1beta1.metrics.k8s.io

Shell habits that pay off

  • Use the k alias and bash completion; on the exam both are set up for you on each host.
  • Use -n <ns> on every command rather than changing the default namespace. Fewer surprises when you move between tasks.
  • kubectl explain for fields, kubectl api-resources for names and versions.
  • kubectl get <kind> <name> -o yaml > before.yaml before risky edits gives you an undo.
  • watch -n2 kubectl get pods -n shop shows progress without retyping.

Common mistakes

  • Writing --dry-run alone. The flag needs a value: --dry-run=client (or server).
  • Putting kubectl flags after -- in kubectl run. Everything after -- goes to the container as its command.
  • Using kubectl logs on a restarted pod and missing the crash because it shows the new container. Add --previous.
  • Writing extra headers or blank lines into an answer file when only a value was asked for.
  • Forgetting --restart=Never and getting a pod whose restart policy is Always.

Practise it

These labs reward fast, precise kubectl output:

Practise this on a real cluster in your account: T11, T26, T27, T28.

Official documentation