Kubernetes Cheat Sheet

Quick reference for common Kubernetes commands (v1.27+). Always check the official Kubernetes docs for the most accurate details.

1. Show Kubernetes and kubectl Version

kubectl version
Displays both client and server versions.

2. List All Contexts

kubectl config get-contexts
Manage multiple clusters.

3. Switch Contexts

kubectl config use-context <context>
Swap clusters/namespaces quickly.

4. Inspect Current Config

kubectl config view
View merged kubeconfig settings.

5. List All Nodes

kubectl get nodes
Check node status and roles.

6. List Pods

kubectl get pods
List pods in the current namespace or use -n for others.

7. Describe a Pod

kubectl describe pod <pod-name>
In-depth details on a specific pod.

8. Create or Update from a Manifest

kubectl apply -f <file.yml>
Declaratively manage cluster objects.

9. Edit a Live Resource

kubectl edit <resource> <name>
Opens in default editor to adjust running config.

10. Update a Container Image

kubectl set image deployment/<name> <container-name>=<image:tag>
Update container image in a Deployment.

11. Check Rollout History

kubectl rollout history deployment/<name>
View revision history of a deployment.

12. Undo Rollout

kubectl rollout undo deployment/<name>
Roll back to a previous stable version.

13. View Logs

kubectl logs <pod-name>
Check pod logs; add -f to follow, or -n for a specific namespace.

14. Execute a Command Inside a Pod

kubectl exec -it <pod-name> -- /bin/bash
Start an interactive shell in a container.

15. Port Forwarding

kubectl port-forward <pod/deployment/service> <local-port>:<remote-port>
Expose a local port to a cluster resource.

16. Monitor Resource Usage

kubectl top nodes or kubectl top pods
View CPU/Memory usage (metrics-server required).

17. Label a Resource

kubectl label pod <pod-name> environment=production
Add or update labels.

18. Annotate a Resource

kubectl annotate pod <pod-name> description='my pod'
Attach metadata to resources.

19. Scale a Deployment

kubectl scale deployment <name> --replicas=<count>
Adjust the number of pod replicas.

20. Safely Drain a Node

kubectl drain <node-name> --ignore-daemonsets --force --delete-emptydir-data
Move pods off a node before maintenance.

2025