Skip to Content
Asterrr's Handbook
Cheat sheets

kubectl commands

The kubectl commands and flags that cover most CKA tasks, grouped by purpose.

Setup: alias k=kubectl, export do="--dry-run=client -o yaml", export now="--force --grace-period=0".

Create

ObjectCommand
Podk run web --image=nginx:1.27 --port=80 --labels=a=b
Deploymentk create deploy web --image=nginx:1.27 --replicas=3
Servicek expose deploy web --port=80 --target-port=8080 --type=NodePort
Jobk create job j --image=busybox:1.36 -- date
CronJobk create cronjob c --image=busybox:1.36 --schedule="*/5 * * * *" -- date
ConfigMapk create cm app --from-literal=K=V --from-file=f
Secretk create secret generic s --from-literal=pw=x
ServiceAccountk create sa ci
Role and bindingk create role r --verb=get,list --resource=pods then k create rolebinding rb --role=r --user=u
Ingressk create ingress i --class=nginx --rule="host/path*=svc:80"
Quotak create quota q --hard=pods=10,cpu=4
YAML onlyadd $do > file.yaml

Change

GoalCommand
Image, env, resourcesk set image deploy/web web=nginx:1.28, k set env deploy/web K=V, k set resources deploy/web --limits=cpu=1
Replicask scale deploy/web --replicas=5
Labels, annotationsk label pod p k=v --overwrite, k annotate pod p n=v, trailing - removes
Patchk patch deploy web -p '{"spec":{"replicas":4}}'
Editk edit deploy web
Rolloutsk rollout status, history, undo or restart deploy/web, --to-revision=2
Replace a Podk replace --force -f pod.yaml
Apply, diffk apply -f f.yaml, k diff -f f.yaml, k apply -k dir/

Read

GoalCommand
Wide, labels-o wide, --show-labels, -L app
Single field-o jsonpath='{.items[*].metadata.name}'
Columns-o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName
Sort--sort-by=.metadata.creationTimestamp
Filter-l app=web, --field-selector status.phase=Running
Everywhere-A
Fieldsk explain pod.spec.containers.securityContext, --recursive
Kindsk api-resources, k api-versions

Debug

GoalCommand
Logsk logs p -c c --previous --tail=50 -f --since=10m
Shellk exec -it p -c c -- sh
Throwaway Podk run t --rm -it --image=busybox:1.36 --restart=Never -- sh
Tunnelk port-forward svc/web 8080:80
Node shellk debug node/n -it --image=busybox:1.36
Copyk cp ns/pod:/path ./local
Usagek top node, k top pod -A --sort-by=memory
Eventsk get events -A --sort-by=.lastTimestamp
Waitk wait --for=condition=Ready pod/p --timeout=60s
Test RBACk auth can-i verb resource --as=user -n ns

Contexts

k config get-contexts, k config use-context <name>, k config set-context --current --namespace=<ns>, k config view --minify.

On this page