Tips and tricks
Small habits that save a great deal of time over a week. None of it is required reading, but each one is something you would otherwise discover on Thursday.
Ask Kubernetes Instead of the Internet
You are faster off the cluster than off a search engine. Two commands cover most of it:
kubectl explain pod.spec.containers.resources # what a field means, and its type
kubectl explain pvc.spec --recursive # a whole subtree at onceexplain answers from the API version your cluster is actually running, which is more reliable than a blog post about a different version.
Generate Manifests, Do Not Type Them
You will write a lot of YAML. Get the skeleton from the cluster and edit it, rather than starting from a blank file:
kubectl create deployment web --image=nginx --replicas=3 --dry-run=client -o yaml > web.yaml
kubectl create secret generic db --from-literal=password=hunter2 --dry-run=client -o yaml > db.yaml
kubectl create job hello --image=busybox --dry-run=client -o yaml -- echo hi > job.yaml--dry-run=client -o yaml builds the object and prints it without sending anything to the cluster. It is the fastest way to get the field names right.
Check Before You Apply
kubectl apply --dry-run=server -f web.yaml # would the API accept this?
kubectl diff -f web.yaml # what would change?--dry-run=client only checks that the YAML parses. --dry-run=server sends it to the API server, which validates it against the real schema and tells you about unknown fields and invalid values. When a manifest is silently ignored, this is usually why.
Reading Objects Without Reading Everything
kubectl get pods -o wide # which node, which IP
kubectl get pods --show-labels # what the selectors will match
kubectl get pod web-abc -o jsonpath='{.status.phase}{"\n"}'
kubectl get pods -o custom-columns=NAME:.metadata.name,IP:.status.podIP,NODE:.spec.nodeNamejsonpath and custom-columns are what you reach for when -o wide does not have the column you want.
Watching Things Happen
kubectl get pods -w
kubectl get events --sort-by=.lastTimestamp
kubectl get events --field-selector involvedObject.name=web-abcThe events feed is the first place to look when something is not doing what you expect, and it is almost always more informative than the object’s status.
Namespaces Without The Typing
kubens # interactive namespace switcher (arrow keys)
kubectx # the same for clusters
kubectl config set-context --current --namespace=lab-04An Alias, If You Want One
Many people use k for kubectl. If you do, bring the completion with it, or you will lose autocompletion for command names:
alias k=kubectl
complete -o default -F __start_kubectl kThe material here always writes kubectl in full, so that you can copy any command out of this site and paste it into your terminal unchanged.
When a Lab Goes Wrong
labctl reset 04 # back to a known-good state, instantly
labctl hint 04.2 # a nudge for one checkpoint
labctl status # where am I?Resetting is not starting over — it recreates the lab’s namespace and leaves your YAML files untouched. Break things freely.
YAML, Specifically
- Indentation is two spaces. Never tabs — YAML forbids them and the error message is unhelpful.
- A list item is
-; a map iskey: value. Getting the indentation of a list inside a map wrong shifts the meaning rather than failing outright. - Quoting a value that looks like a number or a boolean (
"8080","yes","on") prevents surprises.onis a boolean in YAML 1.1, which is why some things that look like strings are not. - An empty value is not the same as a missing field.
replicas:with nothing after it meansnull, not “leave it alone”.