// REF. 07 / ORCHESTRATION

Kubectl cheat sheet

Kubernetes has a reference page for every verb and a short name for every noun, and nobody remembers either under pager pressure. This mat is the kubectl you type at 2am: read, debug, roll out, roll back, plus the handful of YAML fields that cause most of the tickets. Keys omit the kubectl prefix. Checked against the v1.37 reference and the official quick reference.

Habits worth keeping: alias k=kubectl and lean on the short names (po, svc, deploy); set a default namespace with config set-context --current so you stop typing -n; and run diff -f before apply -f, because the cluster is not a staging environment, even when it is.

↓ Download the printable PDF free · letter size · no email needed

Context

config get-contexts
clusters you can talk to
config use-context <c>
switch cluster for every command
config set-context
--current --namespace=x: default ns
-n <ns> / -A
one namespace / all namespaces

Read

get po -o wide
pods with node and ip
describe po <name>
details plus recent events
get po -w
keep watching for changes
get po -l app=web
filter by label
get po <name> -o yaml
the full object, as the api sees it
-o jsonpath='{...}'
pluck a field; -o custom-columns= too
events --types=Warning
what has gone wrong lately

Debug

logs -f <pod>
stream; -c container, -p previous run
logs -l app=web
logs from every pod with that label
--since=1h --tail=100
only the recent lines
exec -it <pod> -- sh
shell into the first container
port-forward svc/x 80:80
local port to a pod behind the service
top po --sort-by=memory
hungriest pods; needs metrics-server
debug -it <pod> --image
ephemeral debug container; --image=busybox
run -it --rm x --image=i
throwaway interactive pod; add -- sh

Apply & create

apply -f app.yaml
create or update, declaratively
apply -k overlays/prod
kustomize build, then apply
diff -f app.yaml
what apply would change
--dry-run=client -o yaml
print the object, send nothing
edit deploy/<name>
open in $KUBE_EDITOR, applied on save
delete po <name> --now
delete with a 1s grace period
create deploy x --image
=nginx: a deployment without yaml
create secret generic x
--from-literal=k=v or --from-file=f

Rollout

scale --replicas=3 <res>
e.g. deploy/web; 0 switches it off
rollout status deploy/x
wait for the rollout to finish
rollout restart deploy/x
roll every pod, no manifest change
rollout undo deploy/x
previous revision; --to-revision=N
rollout history deploy/x
revisions; --revision=N for detail
set image deploy/x a=i:2
new image for container a, rolls out

Nodes & RBAC

cordon <node>
no new pods here; uncordon reverses
drain <node>
evict pods, cordon; --ignore-daemonsets
taint node x k:NoExecute
evict and repel pods without toleration
auth can-i delete po
am I allowed? --as to check someone else

YAML

apiVersion kind metadata
every object starts with these three
selector.matchLabels
must match template.metadata.labels
resources: requests
cpu: 250m, memory: 64Mi; scheduler uses it
resources: limits
ceiling; cpu throttles, memory OOM-kills
livenessProbe: httpGet
fails: restart; readinessProbe: no traffic
restartPolicy: Always
default; OnFailure or Never for jobs
valueFrom: secretKeyRef
env from a secret; configMapKeyRef too

Short names

api-resources
the full list, with short names
po svc deploy
pods, services, deployments
rs sts ds
replicasets, statefulsets, daemonsets
cm ns no
configmaps, namespaces, nodes
ing pv pvc
ingresses, persistent volumes and claims
sa cj ev
serviceaccounts, cronjobs, events

Here be dragons

delete ns prod
takes everything inside with it
--force --grace-period=0
no graceful shutdown; data loss possible
config use-context prod
now every command is in prod
create then apply
apply wants --save-config on the create
Kubectl Field Manual Desk Mat

// KEEP IT UNDER YOUR KEYBOARD

Kubectl Field Manual Desk Mat

from $34 · printed to order in the USA

See the desk mat