FIELD NOTESKUBERNETES

kubectl habits that save minutes

alias k=kubectl with completion, a $do variable for dry-run YAML, a namespace per context and two-space vim: the setup that pays in the CKA and CKAD, and the two exam habits to drop at work.

By Braxx-90004 min readPublished
ON THIS PAGE
Three kubectl habits worth keeping everywhere (the k alias with completion, a $do dry-run variable, two-space vim) beside two that belong to the exam only: a namespace set on the context and a forced delete. Drawn in archhy.

Set up four things and stop there: alias k=kubectl with completion wired to it, a shell variable for --dry-run=client -o yaml, a namespace on the current context, and vim set to two-space indents. In the CKA and CKAD the task hosts already have the first, and the rest live only on the host where you set them, so keep them to one line you paste after each ssh. At work, keep the alias and the YAML generator, and drop the two habits that are only safe in an exam: the forced delete and the sticky namespace.

Based on the article from Gazeek, our sister site for hands-on CKA and CKAD labs: kubectl aliases and settings that save minutes in the CKAD. The whole setup block, a generator line for each common object and the vim settings explained one by one are there.

Short commands are the smallest win#

Typing k instead of kubectl saves six keystrokes on every command. That adds up, but it is not where the time goes. Time goes on three things: writing YAML from memory, working in the wrong namespace, and reading output that is too long to scan. The setup that matters is the one aimed at those.

Shell
alias k=kubectl
source <(kubectl completion bash)
complete -o default -F __start_kubectl k
export do="--dry-run=client -o yaml"

The third line is the one people forget. Without it the alias has no tab completion, and completion is worth more than the alias: it fills in Pod names you would otherwise copy by hand and stops you deleting web-7c9f when you meant web-7c9d. On your own machine these lines go in ~/.bashrc.

The exam works differently. Each task names a host; you ssh to it, do the work, and exit back to the base machine. The hosts come with k and completion, but whatever you add yourself stays on the host where you added it. So keep your own setup to one line, export do="--dry-run=client -o yaml" plus the vim settings below, and paste it on every host before you start.

Generate the YAML, then edit it#

k create deploy web --image=nginx $do > web.yaml prints a correct Deployment without touching the cluster. You add what the generator cannot express (probes, volumes, a security context), then apply. The win is not speed so much as correctness: the selector, the template labels and the indentation are already right, and those are where hand-written manifests fail.

Before you open that file, put set expandtab tabstop=2 shiftwidth=2 in ~/.vimrc. YAML refuses tab characters, and a stray one gives a parse error that is slow to spot under a timer.

When you need a field name, k explain pod.spec.containers.securityContext answers faster than a browser tab, and --recursive shows the whole tree below a field.

Read less output#

A wide table of forty Pods is slow to read and easy to misread. Ask for the columns you want instead:

Shell
k get pods -o custom-columns='NAME:.metadata.name,NODE:.spec.nodeName,IMAGE:.spec.containers[*].image'
k get pods --sort-by=.status.startTime
k get svc web -o jsonpath='{.spec.clusterIP}'

Exam questions often ask you to write a value to a file. -o jsonpath gets you exactly that value, with no table to trim, and -o name gives a list you can pipe into another command.

Where exam habits and work habits split#

Two common exam settings are hazards on a real cluster.

  • The forced delete. --force --grace-period 0 removes the Pod object at once instead of waiting for the containers to stop. In an exam it saves half a minute. On a node that has lost contact, the process can keep running after the API object has gone, and a StatefulSet may start a replacement with the same identity beside it, which the Kubernetes documentation warns can mean split brain and lost data.
  • The sticky namespace. k config set-context --current --namespace=payments writes to your kubeconfig and stays there. In the exam you leave the host when the task ends, so the setting rarely follows you. At work it is still set tomorrow, in a new terminal, when you run a delete you meant for staging. Prefer -n on anything destructive, or show the namespace in your prompt.

Work rewards different flags. --dry-run=server sends the object through validation and admission without saving it, so a policy that would reject it says so now. kubectl diff -f shows what an apply would change before it changes it. Neither is fast, and neither is meant to be.

Settings only save time once your fingers use them without thinking, which means using them in every practice session, not just on the day. Gazeek's CKA mock exams run against a real cluster under a timer, which is where you find out whether they have stuck. For the exam's format, see How the CKAD exam works; for one of the CKA's longer tasks, Backing up etcd; and for kubectl patch doing a release, blue/green with a Service selector.

The diagram#

The habits worth keeping everywhere, beside the two that belong to the exam only. Open it in archhy and press Make a copy to adapt it.

Go deeper#

Gazeek's full article has the complete setup to type at the start of the exam, generator commands for Pods, Jobs, CronJobs and ConfigMaps, and the vim settings for pasting YAML.