LevelUpWith

Explore / Exam Prep for the Certified Kubernetes Administrator (CKA) / Lesson 2

Lesson 2: Speed Habits: kind Labs, Contexts and Imperative YAML

[Exam craft] Learners build a multi-node kind practice cluster and the habits that save time in every task: switching contexts and namespaces, imperative kubectl commands, generating YAML with --dry-run=client -o yaml, finding fields with kubectl explain, and searching the allowed docs at kubernetes.io/docs, helm.sh/docs and gateway-api.sigs.k8s.io.

Free to read, no account needed. Part of Exam Prep for the Certified Kubernetes Administrator (CKA).

By the end of this level you'll be able to

  • Create a multi-node kind cluster and switch between contexts and namespaces with kubectl config use-context and set-context --current --namespace
  • Generate Pod, Deployment and Service manifests with kubectl create/run --dry-run=client -o yaml, then edit and apply them
  • Use kubectl explain --recursive and the allowed documentation to find a field you don't know in under a minute

Why speed habits come first

The CKA gives you 2 hours for 15 to 20 hands-on tasks. The pass mark is 66%. Your registration includes one retake and two exam simulator sessions, and the certification is valid for two years. Most candidates who run out of time do not lack knowledge. They lose minutes typing YAML by hand, working in the wrong cluster, or scrolling through docs. In this level you build the habits that save those minutes in every later task.

This course is independent of the Linux Foundation and CNCF and is not endorsed by them. It follows CNCF's CKA curriculum (CC BY 4.0). The checks are original practice scenarios, not real exam questions. They do not replace hands-on practice, and no course can promise a pass.

Task 1: Build a multi-node kind cluster

kind (Kubernetes IN Docker) runs each Kubernetes node as a Docker container. kind v0.33.0 uses kindest/node:v1.37.0 by default, which matches the exam's Kubernetes v1.37.

  1. Install Docker, kubectl and kind (go install sigs.k8s.io/kind@v0.33.0, or use your package manager).
  2. Save this file as cka-lab.yaml:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
  1. Create the cluster:
kind create cluster --name cka --config cka-lab.yaml
kubectl get nodes

Expected output (ages will differ):

NAME                STATUS   ROLES           AGE   VERSION
cka-control-plane   Ready    control-plane   60s   v1.37.0
cka-worker          Ready    <none>          40s   v1.37.0
cka-worker2         Ready    <none>          40s   v1.37.0
  1. Create a second cluster so you can practise switching: kind create cluster --name lab2.

kind names each kubectl context kind-<cluster-name>, so you now have kind-cka and kind-lab2.

Task 2: Switch contexts and namespaces

On the exam, an infobox at the start of each task tells you which host to ssh into and which cluster or namespace to use. If you work in the wrong place, you get no marks for that task. Follow these steps:

  1. List the contexts. The * marks the active one:
kubectl config get-contexts
kubectl config current-context
  1. Switch clusters:
kubectl config use-context kind-cka
  1. Set the namespace once instead of typing -n on every command:
kubectl create namespace web
kubectl config set-context --current --namespace=web
kubectl config view --minify | grep namespace:

The second command changes only the current context. When you switch back to kind-lab2, it keeps its own namespace.

  1. Add two shortcuts to ~/.bashrc:
alias k=kubectl
complete -o default -F __start_kubectl k
export do="--dry-run=client -o yaml"

The complete line needs kubectl completion loaded first (source <(kubectl completion bash)).

Task 3: Generate YAML instead of typing it

--dry-run=client builds the object on your machine without sending it to the cluster. Adding -o yaml prints it. You get a valid skeleton in seconds, then edit only what the task asks for.

  1. Generate the manifests:
k run web-pod --image=nginx:1.27 --port=80 $do > pod.yaml
k create deployment web --image=nginx:1.27 --replicas=3 $do > deploy.yaml
k create deployment web --image=nginx:1.27 --replicas=3   # create it for real
k expose deployment web --port=80 --target-port=80 --type=NodePort $do > svc.yaml
  1. Edit. For example, open deploy.yaml in vim and add a resources block under the container.
  2. Apply and verify:
k apply -f deploy.yaml
k get deploy,pods -o wide

Rules of thumb:

  • Use k run for Pods and k create deployment for Deployments.
  • Use k expose (it copies the selector) or k create service for Services.
  • k expose needs the live Deployment so it can read its selector. That is why the steps above create it before exposing it.
  • Do not use kubectl apply to generate a skeleton. Run --dry-run=client -o yaml first.
  • If a resource already exists, k get <kind> <name> -o yaml > file.yaml gives you a starting point.

Task 4: Find a field in under a minute

You will meet fields you do not remember. Try these in order:

  1. kubectl explain. It reads the schema from the API server, so it always matches the cluster's version:
k explain pod.spec.containers.resources
k explain deployment.spec.strategy --recursive
k explain pod.spec --recursive --max-depth=1

--recursive prints the whole tree of nested field names. Use grep to find the one you need, then explain that path to read its description.

  1. The allowed documentation. During the exam you may open:
    • kubernetes.io/docs (and kubernetes.io/blog)
    • helm.sh/docs
    • gateway-api.sigs.k8s.io

You may use the search box on kubernetes.io/docs, but do not open external search results. Practise searching for terms like "PersistentVolumeClaim" or "NetworkPolicy" and copying the example YAML. The kubectl Quick Reference page is a good one to know well.

Timed practice task (5 minutes)

Using cluster kind-cka:

  1. Make ops the default namespace for the current context. Create the namespace first if it does not exist.
  2. Create a Deployment api with image httpd:2.4, 2 replicas, and a container memory limit of 128Mi.
  3. Expose it as a ClusterIP Service api-svc on port 8080, targeting port 80.

Solution:

k config use-context kind-cka
k create ns ops
k config set-context --current --namespace=ops
k create deployment api --image=httpd:2.4 --replicas=2 $do > api.yaml
# edit api.yaml: under the container add
#   resources:
#     limits:
#       memory: 128Mi
k apply -f api.yaml
k expose deployment api --name=api-svc --port=8080 --target-port=80
k get deploy,svc,endpoints

If you forget where limits goes, run k explain deployment.spec.template.spec.containers.resources.

What the exam asks about this

No task asks you to explain these habits, but every task depends on them. Expect to:

  • ssh to a named host and work in a named cluster or namespace.
  • Produce correct objects quickly.
  • Look up unfamiliar fields in the allowed docs.

If you skip the context instruction, you can lose marks for an otherwise correct answer.

Key takeaways

  • kind create cluster --name X --config file.yaml builds a multi-node lab. Its context is kind-X.
  • kubectl config use-context switches clusters. kubectl config set-context --current --namespace=NS sets the default namespace.
  • --dry-run=client -o yaml turns imperative commands into editable manifests.
  • kubectl explain <path> --recursive finds fields without leaving the terminal.
  • The allowed docs are kubernetes.io/docs (plus the blog), helm.sh/docs and gateway-api.sigs.k8s.io.

A 6-question knowledge check follows this lesson in the app, free and without an account.

Keep going: levels 3 and 4 are free

Create a free account to open the next two levels, try the AI tutor, save your progress and pick up where you left off. No card needed.

Next: Level 3, RBAC I: Roles, RoleBindings and kubectl auth can-i.

Create a free account →See the whole course

More like this