Free reference

kubectl and Docker cheat sheet

The commands, plus the part no cheat sheet bothers with: a column that maps each pod state to what is actually wrong with it.

KUBECTL

kubectl get pods -o wide
kubectl get pods -A
kubectl describe pod NAME
kubectl logs -f NAME
kubectl logs --previous NAME
kubectl logs -l app=api --tail=100
kubectl exec -it NAME -- sh
kubectl get events --sort-by=.lastTimestamp
kubectl top pod and top node
kubectl port-forward svc/NAME 8080:80
kubectl rollout status deploy/NAME
kubectl rollout restart deploy/NAME
kubectl rollout undo deploy/NAME
kubectl scale deploy/NAME --replicas=3
kubectl get pod NAME -o yaml
kubectl apply -f file.yaml
kubectl run tmp --rm -it --image=busybox -- sh
kubectl config use-context NAME

WHY IS THE POD NOT RUNNING

PendingNothing will schedule it. No node has the CPU or memory, a taint blocks it, or a volume claim is unbound.
ContainerCreating, stuckA volume will not mount, or the image is enormous. Read the events.
ImagePullBackOffWrong tag, wrong registry, or no pull secret.
CrashLoopBackOffIt starts and exits. logs --previous is the only place the reason lives.
OOMKilled, exit 137It wanted more memory than its limit. Raise the limit or fix the leak.
Running but not ReadyThe readiness probe is failing. curl the probe path from inside.

PROBES AND LIMITS

livenessProbe failsThe container gets restarted.
readinessProbe failsTraffic stops. The container stays up.

DOCKER

docker ps add -a for stopped
docker logs -f --tail 100 NAME
docker exec -it NAME sh
docker system df
docker system prune -af destructive
docker build -t name:tag .
docker run --rm -it name:tag sh
docker cp NAME:/path ./here
docker compose up -d
docker compose logs -f
docker compose down -v also deletes volumes

EXIT CODES

0clean exit
1application error
125the run command was wrong
126not executable
127command not found
137SIGKILL, usually out of memory
143SIGTERM, a normal stop

The parts people get wrong

These are the distinctions that cost real time. They are the reason this sheet exists rather than another list of commands.

Why deleting a pod does not fix anything

Deleting a pod that belongs to a Deployment gets you a new pod, not a fix. The controller just makes another one.

kubectl describe vs kubectl logs

describe first for scheduling problems. logs first for crash problems. Doing it the other way round wastes the outage.

Kubernetes requests vs limits

Requests decide where a pod lands. Limits decide when it gets killed. Setting only limits is how you get surprise evictions.

What docker system prune -af actually deletes

prune -af removes every unused image, not just the dangling ones. On a build host that is a very long afternoon.

Container exits immediately after starting

A container whose main process exits immediately is not crashing. It did what you told it to do.

CPU limit vs memory limit in Kubernetes

Over the CPU limit means throttled and slow. Over the memory limit means killed, exit 137. Only one of those pages you.

Using kubectl logs --previous after a crash

logs shows the current container. After a crash the reason is in the one before it. That is what --previous is for.

Other cheat sheets