Day 2: Exam Craft
Goal: You work with kubectl so fast that the clock is not your opponent in the exam. · Duration: approx. 45 min · Prerequisite: Day 1
Briefing
The exam has about 17 tasks in 120 minutes – roughly seven minutes per task. If you type YAML by hand, you lose. Today you train the moves you need on every following day:
- Alias and autocompletion
- Generate YAML instead of writing it
(
--dry-run=client -o yaml) - Switch contexts and namespaces
kubectl explaininstead of searching the docs- Filter output with jsonpath, custom-columns and
--sort-by
Drill 1: Alias and autocompletion
On cp1, into ~/.bashrc:
cat <<'EOF' >> ~/.bashrc
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k
export do="--dry-run=client -o yaml"
EOF
source ~/.bashrc
type kExpected:
k is aliased to `kubectl'
Now try k get po -n kube-<TAB> – the namespace is
completed.
Tip: In the exam environment,
kand completion are usually already set up. Check it in the first minute withtype k.
Save your progress? Sign up for free
Drill 2: Generate YAML instead of writing it
k create ns tag02
k create deployment api --image=nginx:1.29 --replicas=3 $doExpected (shortened):
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: api
name: api
spec:
replicas: 3
selector:
matchLabels:
app: api
...
containers:
- image: nginx:1.29
name: nginx
A pod with its own command – everything after -- becomes
the container command:
k run tmp --image=busybox:1.37 --restart=Never --command $do -- sleep 3600Expected (excerpt):
containers:
- command:
- sleep
- "3600"
image: busybox:1.37
name: tmp
Remember: k create knows deployment, service, configmap,
secret, job, cronjob, role, rolebinding, serviceaccount, ingress, quota,
priorityclass – almost everything that comes up in the exam.
Save your progress? Sign up for free
Drill 3: Contexts and namespaces
Every exam task starts with “Set the context”. If you forget it, you solve the task in the wrong cluster – zero points.
k config get-contexts
k config set-context dev --cluster=kubernetes --user=kubernetes-admin --namespace=tag02
k config use-context dev
k config get-contextsExpected:
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* dev kubernetes kubernetes-admin tag02
kubernetes-admin@kubernetes kubernetes kubernetes-admin default
Change only the namespace of the current context and check it:
k config set-context --current --namespace=default
k config view --minify -o jsonpath='{..namespace}{"\n"}'
k config use-context kubernetes-admin@kubernetesExpected:
Context "dev" modified.
default
Switched to context "kubernetes-admin@kubernetes".
Save your progress? Sign up for free
Drill 4: kubectl explain
Which fields does an update strategy have?
k explain deployment.spec.strategy --recursive | sed -n '/FIELDS/,$p'Expected:
FIELDS:
rollingUpdate <RollingUpdateDeployment>
maxSurge <IntOrString>
maxUnavailable <IntOrString>
type <string>
enum: Recreate, RollingUpdate
A single field with its description:
k explain pod.spec.containers.livenessProbe.httpGet | head -9Expected:
KIND: Pod
VERSION: v1
FIELD: httpGet <HTTPGetAction>
DESCRIPTION:
HTTPGet specifies an HTTP GET request to perform.
HTTPGetAction describes an action based on HTTP Get requests.
Save your progress? Sign up for free
Drill 5: Filter output
Name and IP of all nodes, one line per node:
J='{range .items[*]}{.metadata.name}{" "}{.status.addresses[0].address}{"\n"}{end}'
k get nodes -o jsonpath="$J"Expected:
cp1 172.16.0.126
worker1 172.16.0.228
worker2 172.16.0.83
Custom columns, sorted by node:
k get pods -A -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName \
--sort-by=.spec.nodeName | head -4Expected (shortened):
NAME NODE
etcd-cp1 cp1
kube-apiserver-cp1 cp1
tigera-operator-85dfd45867-dwbmx cp1
The kubelet versions of all nodes in one line:
k get nodes -o jsonpath='{.items[*].status.nodeInfo.kubeletVersion}{"\n"}'Expected: v1.36.6 v1.36.6 v1.36.6
Tip: Exam tasks often ask you to “write the result to the file /opt/…”. Then append
> /opt/…and verify withcat.
Save your progress? Sign up for free
Drill 6: Immutable fields and replace –force
Many pod fields can no longer be changed after startup. Create a pod and try to give it a memory limit afterwards:
mkdir -p ~/cka/tag02 && cd ~/cka/tag02
k run web --image=nginx:1.29 -n tag02 $do > web.yaml
k apply -f web.yaml
k set resources pod web -n tag02 --limits=memory=64Mi $do > web2.yaml
k apply -f web2.yamlExpected (shortened):
The Pod "web" is invalid: spec: Forbidden: pod updates may not change fields other than
`spec.containers[*].image`,`spec.initContainers[*].image`,`spec.activeDeadlineSeconds`,
`spec.tolerations` ...
The exam approach: delete the pod and recreate it in one step.
k replace --force -f web2.yaml
k get pod web -n tag02 -o jsonpath='{.spec.containers[0].resources}{"\n"}'Expected:
pod "web" deleted from tag02 namespace
pod/web replaced
{"limits":{"memory":"64Mi"},"requests":{"memory":"64Mi"}}
Why are requests there too? If a request is missing,
Kubernetes copies the limit.
Save your progress? Sign up for free
Drill 7: Delete fast, read events
A normal delete waits for the grace period (default 30
s). In the exam, every second counts:
k delete pod web -n tag02 --force --grace-period=0Expected:
Warning: Immediate deletion does not wait for confirmation that the running resource
has been terminated. ...
pod "web" force deleted from tag02 namespace
Events in chronological order:
k get events -n tag02 --sort-by=.lastTimestamp | tail -3Expected (shortened):
5s Normal Started pod/web Container started
3s Normal Killing pod/web Stopping container web
Warning: Use
--force --grace-period=0only for practice pods. In production the container may keep running on the node.
Save your progress? Sign up for free
Exercise: Speed Drills
- Generate the YAML for a Job
oncewithbusybox:1.37that runsdate, and save it tojob.yaml. Target time: 30 s - List all resource types that are not bound to a namespace. Target time: 20 s
- Print the names of all pods in
kube-system, one line per pod, without a header. Target time: 45 s - Set the current context to the namespace
tag02and back todefault. Target time: 30 s
Solution
k create job once --image=busybox:1.37 $do -- date > job.yaml
k api-resources --namespaced=false
k get pods -n kube-system -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
k config set-context --current --namespace=tag02
k config set-context --current --namespace=defaultAlternative for 3:
k get pods -n kube-system --no-headers -o custom-columns=:.metadata.name.
Save your progress? Sign up for free
Checkpoint
type kExpected: k is aliased tokubectl’`
k config current-contextExpected:
kubernetes-admin@kubernetes
echo $doExpected: --dry-run=client -o yaml
Challenge
For all pods in the cluster, print namespace, name and image of the
first container, sorted by namespace, and write the result to
~/cka/tag02/images.txt.
Hint: custom-columns can address any field, including inside lists.
Solution
C=NS:.metadata.namespace,NAME:.metadata.name,IMAGE:.spec.containers[0].image
cd ~/cka/tag02
k get pods -A --sort-by=.metadata.namespace -o custom-columns=$C > images.txt
head -3 images.txtExpected (shortened):
NS NAME IMAGE
calico-system calico-apiserver-79f749f5bc-c8w8d quay.io/calico/calico:v3.33.1
calico-system calico-apiserver-79f749f5bc-nltj4 quay.io/calico/calico:v3.33.1
Clean up:
k delete ns tag02
k config delete-context devSave your progress? Sign up for free
Exam Tip
- First copy and run the task’s context command. Always.
- First
$do > datei.yaml, then edit it selectively, thenk apply -f. No YAML from scratch. - In vim:
:set pastebefore pasting, otherwise the indentation gets mangled.
Enterprise Note
- In daily work, tools such as k9s, kubectx/kubens or Lens replace many of these moves.
- Several clusters in one kubeconfig: show the context in the prompt (e.g. kube-ps1) so that nobody accidentally works in production.
--force --grace-period=0is usually forbidden in production runbooks.