Kubernetes cheatsheet
Context and namespace
| Command | Description |
|---|---|
kubectl config get-contexts | List contexts |
kubectl config use-context NAME | Switch cluster |
kubectl config set-context --current --namespace=ns | Default namespace |
kubectl get ns | List namespaces |
Get and describe
| Command | Description |
|---|---|
kubectl get pods -A | All pods, all namespaces |
kubectl get pods,svc,deploy -n app | Multiple resource types |
kubectl get pods -o wide | Node, IP, status |
kubectl describe pod POD -n app | Events, conditions, mounts |
kubectl get events -n app --sort-by=.lastTimestamp | Recent events |
Logs and exec
| Command | Description |
|---|---|
kubectl logs POD -n app -f | Follow pod logs |
kubectl logs POD -c CONTAINER -n app | Specific container |
kubectl logs POD --previous -n app | Logs from crashed container |
kubectl exec -it POD -n app -- sh | Shell in running pod |
Apply, scale, rollout
| Command | Description |
|---|---|
kubectl apply -f manifest.yaml | Create or update resources |
kubectl delete -f manifest.yaml | Delete from manifest |
kubectl scale deploy/APP --replicas=3 -n app | Scale deployment |
kubectl rollout status deploy/APP -n app | Wait for rollout |
kubectl rollout undo deploy/APP -n app | Rollback deployment |
Debugging
kubectl run tmp --rm -it --image=busybox -- sh
kubectl debug -it POD -n app --image=busybox --target=CONTAINER
kubectl port-forward svc/myapp 8080:80 -n app
kubectl top pods -n app
kubectl top nodes
krew — install and common plugins
# Install krew: https://krew.sigs.k8s.io/docs/user-guide/setup/install/
kubectl krew install stern ctx ns tree view-secret neat popeye
# stern — multi-pod log tail (essential for debugging)
stern . -n app # all pods in namespace
stern -l app=web -n app -c nginx # by label, one container
stern POD_PREFIX -n app --since 5m # recent logs only
# ctx / ns — fast context and namespace switch (kubectx / kubens)
kubectl ctx # list contexts
kubectl ctx prod-cluster
kubectl ns app
# tree — resource hierarchy
kubectl tree deploy/myapp -n app
# view-secret — decode secrets on CLI
kubectl view-secret secret/mysecret -n app
# neat — cleaner YAML output (remove managed fields noise)
kubectl get deploy myapp -o yaml | kubectl neat
# popeye — cluster hygiene report
kubectl popeye -n app
Other helpful CLIs
| Tool | Description |
|---|---|
| k9s | Terminal UI for cluster navigation |
| helm | Chart installs — see Helm lab |
| kubectl debug | Ephemeral debug containers (built-in) |
Cluster health
| Command | Description |
|---|---|
kubectl get nodes | Node Ready status |
kubectl get componentstatuses | Deprecated; use node/control-plane checks |
kubectl cluster-info | API server and kube-dns addresses |
kubectl auth can-i create pods -n app | RBAC check |
Pro tips
kubectl describeEvents section explains most pod failures--previouslogs show why the last container crashed- Install stern via krew — faster than looping
kubectl logs - Always set
-n NAMESPACE— wrong namespace is a common mistake - Helm releases: use the Helm lab, not raw YAML guesswork
Practice scenarios
Hands-on Kubernetes scenarios on live Linux VMs: kubernetes