
Set Up Your Terminal for the Exam
Set Up Your Terminal for the Exam
Part 2 of the CKA roadmap · Phase 0 — a cluster of your own
This is the smallest task in the roadmap and the one that pays back fastest.
Over a two-hour exam you type kubectl something like 150 times. At roughly two seconds each for the long form, that is five minutes spent on spelling a command name. Completion matters more still: resource names in the exam are long and easy to mistype, and one wrong character means the task is done against the wrong object.
So the goal here is double. Set up your practice environment today — and be able to retype the whole setup from memory, in under 60 seconds, at the start of the exam.
The task
Before reading the first question: make
ka working alias forkubectlwith tab completion, add a shortcut that turns anykubectlcommand into a YAML skeleton, add one for deleting a pod without waiting, and make vim safe for editing YAML. Then retype all of it without notes in under a minute.
Check before you configure
The exam environment usually has alias k=kubectl and completion already set up. Do not bet on it. The first thing to type is:
k versionIf that says command not found, paste the setup below before you read anything.
Remember too that aliases live in the current shell on the current machine. The moment you ssh to another node, they are gone.
The solution
An alias that completes
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl kThe complete line is the one people leave out. Without it, kubectl get po<TAB> completes and k get po<TAB> does not — the alias works but you have thrown away the half that matters.
Order matters too. __start_kubectl is a function defined by kubectl completion bash, so the source line has to run before the complete line.
Completion also needs the bash-completion package on the machine. Check for it, and install it if it is missing:
type _init_completion >/dev/null 2>&1 && echo OK || sudo apt install -y bash-completionCompletion is worth more than the alias. It fills in resource names — k get pod ngin<TAB> — and flag names, which is where typos actually happen.
$do — a YAML skeleton from any command
export do='--dry-run=client -o yaml'Use it to generate manifests instead of writing them:
k run nginx --image=nginx $do > pod.yaml
k create deploy web --image=nginx --replicas=3 $do > deploy.yaml
k create svc clusterip web --tcp=80:80 $do > svc.yamlThis is how you handle any task along the lines of "create a pod with …" where kubectl run has no flag for what is asked: generate the skeleton, edit it, then k apply -f.
$now — delete without waiting
export now='--force --grace-period=0'k delete pod nginx $nowA normal pod deletion waits out terminationGracePeriodSeconds, which defaults to 30 seconds. In the exam you delete and recreate things constantly — thirty seconds, ten times over, is five minutes of watching a terminal.
vim that does not break YAML
YAML refuses tabs, and vim uses them by default. Paste a block into a manifest and the indentation is wrong in a way that takes longer to fix than to retype.
cat > ~/.vimrc <<'VIM'
set expandtab
set tabstop=2
set shiftwidth=2
set number
VIMThree keystrokes worth knowing by heart when editing long YAML:
:set paste— turns off auto-indent before you paste, so each line stops drifting further rightyyp— duplicate the current line, for adding another container or env entryddandu— delete a line, undo
Two more aliases worth having
| Add | Why |
|---|---|
alias kn='kubectl config set-context --current --namespace' | Tasks jump between namespaces; kn dev beats typing the full command |
alias kx='kubectl config use-context' | The exam has several clusters, and every task names the context to use |
export KUBE_EDITOR=vim | Makes sure k edit opens vim, not an unfamiliar editor |
The one block
For your practice machines, append everything to ~/.bashrc once. Run it on the control plane, and again on worker if you SSH there often:
cat >> ~/.bashrc <<'RC'
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k
export do='--dry-run=client -o yaml'
export now='--force --grace-period=0'
export KUBE_EDITOR=vim
RC
source ~/.bashrcIn the exam, type it straight into the shell
Editing ~/.bashrc and sourcing it is an extra step you do not need there. You only need the setup to last for the session, so type the lines directly.
Verification
All four must behave:
k version --client
k get po<TAB><TAB>
echo $do
echo $nowThe first proves the alias; the second proves completion (the <TAB> means press the key twice, not type it); the last two should print --dry-run=client -o yaml and --force --grace-period=0.
Then try it on a real task, end to end:
k run nginx --image=nginx $do > p.yaml && vim p.yaml && k apply -f p.yamlTraps
Quoting $do
Write $do, never "$do". Inside double quotes it becomes one argument — --dry-run=client -o yaml as a single string — and kubectl rejects it. Word-splitting is the whole reason the shortcut works.
Losing the setup after ssh
Aliases and exports belong to one shell. Set them up on the control plane, SSH to a worker to fix a kubelet, and k no longer exists there. Either set it up again on that node or use the full kubectl.
Inventing more aliases
It is tempting to add kgp, kgd, kgs and a dozen more. Two reasons not to. The exam does not have them, so every one is something extra to retype at the start. And if you practise with kgp, typing k get po in the exam becomes slower and more error-prone than it would have been.
Practise with what the exam gives you. That is the entire point of this task.
Taking --force to work
--force --grace-period=0 removes the pod from the API server even if its container has not actually stopped. In a lab or an exam that is fine. On a real cluster it is a dangerous habit — don't carry it back to your day job.
Speed
The target is simple, and you should only tick it off when it is true:
The docs page
If you blank on the complete line, the kubectl quick reference on kubernetes.io has the completion and alias setup near the top — and it is a page you can open during the exam. Know where it is before you need it.
Previous: Build a two-node kubeadm cluster on GCP · Back to the CKA roadmap