kubectl cheat sheet
The kubectl commands you reach for when something’s wrong: what’s running, why it crashed, getting a shell or a port into it, and rolling it back.
In Snippet Keeper, click Import at the bottom of the sidebar and choose the file. They land in your kubectl app, and ones you already have are skipped. The highlighted parts become fill-in fields: type a value (or pick one), and the finished command is on your clipboard.
Contexts and namespaces
kubectl config current-context
kubectl config get-contexts
kubectl config use-context context
kubectl config set-context --current --namespace=namespace
Looking around
kubectl get pods -n namespace -o wide
kubectl get pods -A
kubectl get pods -n namespace -w
kubectl get pods -n namespace -l label
kubectl describe kind name -n namespace
kubectl get kind name -n namespace -o yaml
kubectl get events -n namespace --sort-by=.lastTimestamp
kubectl explain field
Logs and debugging
kubectl logs -f deployment/deployment -n namespace --since=since
kubectl logs pod -n namespace --previous
kubectl logs pod -c container -n namespace
kubectl exec -it pod -n namespace -- shell
kubectl port-forward svc/service local_port:remote_port -n namespace
kubectl run debug --rm -it --image=image --restart=Never -n namespace -- sh
kubectl top pods -n namespace --sort-by=by
kubectl cp namespace/pod:path dest
Deployments
kubectl diff -f file
kubectl apply -f file
kubectl scale deployment/deployment --replicas=replicas -n namespace
kubectl rollout restart deployment/deployment -n namespace
kubectl rollout status deployment/deployment -n namespace
kubectl rollout undo deployment/deployment -n namespace
kubectl set image deployment/deployment container=image -n namespace
kubectl delete kind name -n namespace
Config and secrets
kubectl create secret generic name --from-literal=key=value -n namespace
kubectl get secret name -n namespace -o jsonpath='{.data.key}' | base64 --decode
kubectl create configmap name --from-file=file -n namespace
Nodes
kubectl get nodes -o wide
kubectl drain node --ignore-daemonsets --delete-emptydir-data
kubectl uncordon node
Keep these one keystroke away.
Snippet Keeper is a Mac app for the commands you keep looking up. Press ⌥⇧Space in any app, type a few letters, fill in the blanks, and it’s on your clipboard.