Cheat sheets /

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.

Download all 34 for Snippet Keeper Get Snippet Keeper

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

Show the current context

kubectl config current-context

List contexts

kubectl config get-contexts

Switch context

kubectl config use-context context

Set the default namespace

kubectl config set-context --current --namespace=namespace

Looking around

Pods with their nodes and IPs

kubectl get pods -n namespace -o wide

Pods in every namespace

kubectl get pods -A

Watch pods change

kubectl get pods -n namespace -w

Pods with a label

kubectl get pods -n namespace -l label

Describe a resource

The Events at the bottom usually say why something isn’t starting.

kubectl describe kind name -n namespace

Get a resource as YAML

kubectl get kind name -n namespace -o yaml

Recent events, newest last

kubectl get events -n namespace --sort-by=.lastTimestamp

Explain a field

kubectl explain field

Logs and debugging

Tail logs for a deployment

kubectl logs -f deployment/deployment -n namespace --since=since

Logs from before a crash

kubectl logs pod -n namespace --previous

Logs from one container

kubectl logs pod -c container -n namespace

Shell into a pod

kubectl exec -it pod -n namespace -- shell

Port-forward a service

kubectl port-forward svc/service local_port:remote_port -n namespace

Throwaway debug pod

Handy for testing DNS and connectivity from inside the cluster.

kubectl run debug --rm -it --image=image --restart=Never -n namespace -- sh

CPU and memory by pod

Needs metrics-server in the cluster.

kubectl top pods -n namespace --sort-by=by

Copy a file out of a pod

kubectl cp namespace/pod:path dest

Deployments

See what apply would change

kubectl diff -f file

Apply a file

kubectl apply -f file

Scale a deployment

kubectl scale deployment/deployment --replicas=replicas -n namespace

Restart a deployment

kubectl rollout restart deployment/deployment -n namespace

Watch a rollout

kubectl rollout status deployment/deployment -n namespace

Roll back a deployment

kubectl rollout undo deployment/deployment -n namespace

Change a container’s image

kubectl set image deployment/deployment container=image -n namespace

Delete a resource

kubectl delete kind name -n namespace

Config and secrets

Create a secret

kubectl create secret generic name --from-literal=key=value -n namespace

Read a secret’s value

kubectl get secret name -n namespace -o jsonpath='{.data.key}' | base64 --decode

Create a ConfigMap from a file

kubectl create configmap name --from-file=file -n namespace

Nodes

List nodes

kubectl get nodes -o wide

Drain a node for maintenance

kubectl drain node --ignore-daemonsets --delete-emptydir-data

Put a node back in service

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.

Snippet Keeper asking for the values in a kubectl command before copying it