kubectl Cheat Sheet
kubectl talks to the cluster's API server. Almost every command reads kubectl <action> <resource type> [name].
Overview
| Task | Command |
|---|---|
| Nodes and their status | kubectl get nodes -o wide |
| Pods in every namespace | kubectl get pods -A |
| Create or update from a manifest | kubectl apply -f file.yml |
| Start a throwaway pod | kubectl run test-nginx --image=nginx --restart=Never |
| Why a pod is stuck | kubectl describe pod <pod> (read the Events at the bottom) |
| Logs, following | kubectl logs -f <pod> (--previous for the crashed container) |
| Shell in a pod | kubectl exec -it <pod> -- sh |
| Recent cluster events | kubectl get events -A --sort-by=.lastTimestamp |
Details
Nodes and Labels
kubectl get nodes --show-labels
kubectl label nodes <node> gpu=nvidia-gtx1070
kubectl label nodes <node> disk=ssd --overwrite # re-runnable: no error if the label exists
kubectl get nodes -l gpu # nodes with any value for "gpu"
kubectl get nodes -l gpu=nvidia-gtx1070 # a specific value
Maintenance (drain, cordon, uncordon) is in Drain and Shut Down a Bare-Metal Kubernetes Node.
The kubelet on a Node
The kubelet is a systemd service, not a pod: inspect it on the node itself.
sudo systemctl status kubelet
sudo journalctl -u kubelet -f
sudo systemctl restart kubelet
Files, Scaling and Restarts
kubectl cp ./file.mkv media/<pod>:/data/media/movies/ # local -> pod; needs tar in the image
kubectl exec -n media deploy/jellyfin -- ls /data/media # target a Deployment, not a pod name
kubectl scale deploy <app> -n <ns> --replicas=0 # stop without deleting
kubectl rollout restart deploy/<app> -n <ns> # recreate the pods
kubectl logs -n <ns> -l job-name=<job> # output of a Job's pod
kubectl describe node <node> | grep nvidia.com/gpu # GPUs the node advertises
kubectl cp streams a tar archive through the API server: it fails if the image has no tar, shows no progress and can't resume. When I copied a macOS folder with spaces in its name, it returned at once and copied nothing, so rename such folders first. For large transfers, write to the storage behind the volume instead. When the pod is crash-looping, see A Crash-Looping Pod Can't Be Exec'd Into, So Repair Its Volume from a One-Off Job.
Scripts and Debugging
kubectl --request-timeout=5s get nodes # fail fast when the API server is down
kubectl logs -n <ns> -l app=<app> --tail=50 -f # follow by label: survives pod renames
kubectl rollout status deploy/<app> -n <ns> # wait until a rollout completes
kubectl run test-ping --image=busybox --restart=Never \
--overrides='{"spec":{"nodeName":"<node>"}}' -- sleep 3600 # pin a test pod to one node
kubectl exec test-ping -- ping -c 3 <pod-ip-on-another-node> # cross-node pod networking
sudo crictl ps # on a node: containers as the kubelet sees them
Without --request-timeout, a script calling kubectl hangs as long as the control plane is down, instead of failing and falling back to SSH. On a node, crictl talks to the CRI socket like the kubelet does; ctr is containerd's lower-level client (Kubernetes containers live in its k8s.io namespace); docker sees none of them. What rollout restart really does: A Rolling Restart Starts New Pods Before Stopping Old Ones, So Readiness Decides Whether Users Notice.
References
- Kubernetes documentation: kubectl Quick Reference - the official cheat sheet.
- Kubernetes documentation: Labels and Selectors -
-lselectors. - Kubernetes MOC - hub for Kubernetes notes.
Review cards
Flashcard Why use `kubectl apply -f` rather than `kubectl create -f` for manifests you keep?
apply creates the objects or updates them to match the file, so it can run again. create fails with AlreadyExists once they exist.
Flashcard A pod is stuck in Pending or ContainerCreating. Where do you look first?
kubectl describe pod <pod>, at the Events at the bottom: scheduling failures, image pulls, volume mounts.
Flashcard Why doesn't `kubectl get pods -A` show the kubelet, and how do you read its logs?
The kubelet is a systemd service on each node, not a pod. On the node: systemctl status kubelet and journalctl -u kubelet -f.