kubectl Cheat Sheet
A practical kubectl cheat sheet for daily Kubernetes cluster work. The commands reflect the current CLI and common API resources.
Context and Access
kubectl config get-contexts
kubectl config current-context
kubectl config use-context prod-cluster
kubectl cluster-info
kubectl version --short
Namespace and Basic Navigation
kubectl get ns
kubectl create ns dev
kubectl config set-context --current --namespace=dev
kubectl api-resources
kubectl explain deployment
kubectl explain deployment.spec.template.spec.containers
Viewing Resources
kubectl get all -n dev
kubectl get pods -A
kubectl get deploy,svc,ingress -n dev
kubectl get pod <pod> -o wide
kubectl get pod <pod> -o yaml
kubectl describe pod <pod>
kubectl top nodes
kubectl top pods -n dev
Applying Manifests
kubectl apply -f deployment.yaml
kubectl apply -f k8s/
kubectl diff -f k8s/
kubectl delete -f deployment.yaml
kubectl kustomize overlays/prod
kubectl apply -k overlays/prod
Working with Pods
kubectl logs <pod>
kubectl logs -f <pod>
kubectl logs <pod> -c app
kubectl exec -it <pod> -- sh
kubectl port-forward pod/<pod> 8080:80
kubectl cp ./file.txt <pod>:/tmp/file.txt
Deployments and Rollouts
kubectl get deploy
kubectl scale deploy web --replicas=3
kubectl rollout status deploy/web
kubectl rollout history deploy/web
kubectl rollout restart deploy/web
kubectl rollout undo deploy/web
kubectl set image deploy/web app=myapp:1.4.2
Services, Ingress, and Endpoints
kubectl get svc
kubectl describe svc web
kubectl get endpoints web
kubectl get ingress
kubectl describe ingress web
Useful Debug Commands
kubectl describe pod <pod>
kubectl logs <pod> --previous
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl debug node/<node> -it --image=busybox
kubectl debug pod/<pod> -it --image=busybox --target=<container>
Label, Annotate, Patch
kubectl label pod <pod> app=web
kubectl annotate pod <pod> owner=ivan
kubectl patch deploy web -p '{"spec":{"replicas":4}}'
Common One-Liners
kubectl get pods -A -o wide
kubectl get pods -n dev --field-selector=status.phase=Running
kubectl get pod <pod> -o jsonpath='{.status.podIP}'
kubectl get secret app-secret -o yaml
kubectl delete pod <pod>
Practice
kubectl applyfits declarative management, whilekubectl editandkubectl patchare useful for emergency changes but do not replace GitOps.- Always review
describe,logs, andeventstogether because each one on its own is often incomplete. - Keep separate
contextandnamespacesettings for production clusters to reduce the risk of accidental changes.