Kubectl Cheatsheet
Essential kubectl commands for inspecting resources, applying manifests, tailing logs, and debugging Pods.
TL;DR
Daily kubectl commands for inspecting cluster state (get, describe, explain), applying manifests (apply, delete), debugging containers (logs, exec, port-forward), and watching deployments (rollout status).
Inspecting resources
kubectl get pods -A # list pods across all namespaces
kubectl get nodes -o wide # show node IPs, OS, and kubelet version
kubectl get svc,ing,endpointslices # inspect Services, Ingresses, and Endpoints
kubectl describe pod <pod-name> # view Events, probes, and container status
kubectl explain deployment.spec # built-in API schema documentationUse kubectl explain <resource>.<field> whenever you forget a YAML field name or type—it queries the cluster's OpenAPI schema directly.
Applying and managing manifests
kubectl apply -f manifest.yaml # create or update resources declaratively
kubectl apply -f k8s/ --dry-run=client # validate YAML locally before sending
kubectl diff -f k8s/ # preview live changes before applying
kubectl delete -f manifest.yaml # remove resources defined in the fileLogs, exec, and port-forward
kubectl logs -f <pod-name> # stream stdout/stderr from a single-container pod
kubectl logs --previous <pod-name> # view logs from the last crashed container
kubectl exec -it <pod-name> -- sh # open a POSIX shell (works on Alpine/BusyBox)
# kubectl exec -it <pod-name> -- /bin/bash # use bash only when installed in the image
kubectl port-forward svc/web 8080:80 # forward local :8080 to Service web:80Minimal images like Alpine or distroless often omit /bin/bash. Default to sh in kubectl exec, and remember that distroless images have no shell at all (use kubectl debug ephemeral containers instead).
Rollouts and debugging
kubectl rollout status deployment/web # watch a rolling update until complete
kubectl rollout history deployment/web # list past ReplicaSet revisions
kubectl rollout undo deployment/web # roll back to the previous revision
kubectl get events --sort-by=.metadata.creationTimestampCheck yourself
Check Yourself
1. Why should you prefer `kubectl exec -it <pod> -- sh` over `/bin/bash` by default?
2. Which command shows recent scheduling errors, image pull failures, and probe failures for a Pod?
Sources
Related Notes
Helm Basics: Charts, Values, Releases, and Kustomize
Package parameterized Kubernetes manifests with Helm charts, manage releases and rollbacks, and know when Kustomize is simpler.
OpenTofu First Project
Write and run a self-contained OpenTofu project using only the local and random providers with variables and outputs.