Kubernetes Cheat Sheet
Kubernetes reference with kubectl commands for pods, deployments, services, configmaps, and debugging. Copy-ready examples.
Cluster
| Command | Description | Example |
|---|---|---|
| Show cluster endpoint info | kubectl cluster-info | |
| List all nodes | kubectl get nodes -o wide | |
| Show node details | kubectl describe node worker-1 | |
| Show node resource usage | kubectl top nodes | |
| List available contexts | kubectl config get-contexts | |
| Switch cluster context | kubectl config use-context production | |
| List all resource types | kubectl api-resources --namespaced=true |
Pods
| Command | Description | Example |
|---|---|---|
| List pods in current namespace | kubectl get pods -o wide | |
| List pods in all namespaces | kubectl get pods --all-namespaces | |
| Show pod details and events | kubectl describe pod api-7d8f9-abc12 | |
| View pod logs | kubectl logs -f api-7d8f9 --tail=100 | |
| Logs from specific container | kubectl logs mypod -c sidecar | |
| Execute command in pod | kubectl exec -it api-7d8f9 -- bash | |
| Forward local port to pod | kubectl port-forward api-7d8f9 8080:3000 | |
| Delete a pod | kubectl delete pod api-7d8f9 --grace-period=0 | |
| Show pod resource usage | kubectl top pods --sort-by=memory | |
| Copy file to/from pod | kubectl cp ./config.yml api-7d8f9:/app/ | |
| Run a pod quickly | kubectl run debug --image=busybox -it --rm -- sh |
Deployments
| Command | Description | Example |
|---|---|---|
| List deployments | kubectl get deploy -o wide | |
| Create deployment | kubectl create deploy api --image=myapp:1.0 | |
| Apply configuration from file | kubectl apply -f deployment.yaml | |
| Delete resources from file | kubectl delete -f deployment.yaml | |
| Scale deployment | kubectl scale deploy api --replicas=3 | |
| Update deployment image | kubectl set image deploy api app=myapp:2.0 | |
| Watch rollout progress | kubectl rollout status deploy api | |
| Rollback to previous version | kubectl rollout undo deploy api --to-revision=2 | |
| View rollout history | kubectl rollout history deploy api | |
| Restart deployment (rolling) | kubectl rollout restart deploy api | |
| Create horizontal pod autoscaler | kubectl autoscale deploy api --min=2 --max=10 --cpu-percent=80 |
Services
| Command | Description | Example |
|---|---|---|
| List services | kubectl get svc -o wide | |
| Create service for deployment | kubectl expose deploy api --port=80 --target-port=3000 --type=LoadBalancer | |
| Show service endpoints | kubectl get ep api | |
| Forward to service | kubectl port-forward svc/api 8080:80 | |
| List ingress resources | kubectl get ingress -o wide |
Config & Secrets
| Command | Description | Example |
|---|---|---|
| Create configmap | kubectl create configmap app-config --from-file=config.yaml | |
| List configmaps | kubectl get cm | |
| Create secret | kubectl create secret generic db-creds --from-literal=password=s3cr3t | |
| List secrets | kubectl get secrets | |
| Show secret metadata | kubectl describe secret db-creds | |
| Decode secret value | kubectl get secret db-creds -o jsonpath='{.data.password}' | base64 -d |
Namespaces
| Command | Description | Example |
|---|---|---|
| List namespaces | kubectl get ns | |
| Create namespace | kubectl create ns staging | |
| Run command in namespace | kubectl -n production get pods | |
| Set default namespace | kubectl config set-context --current --namespace=staging |
Storage
| Command | Description | Example |
|---|---|---|
| List persistent volumes | kubectl get pv | |
| List persistent volume claims | kubectl get pvc -n production | |
| Show PVC details | kubectl describe pvc db-storage | |
| List storage classes | kubectl get sc |
Jobs
| Command | Description | Example |
|---|---|---|
| List jobs | kubectl get jobs | |
| Create one-off job | kubectl create job migrate --image=myapp -- npm run migrate | |
| List cron jobs | kubectl get cj |
Debugging
| Command | Description | Example |
|---|---|---|
| Show cluster events | kubectl get events --sort-by=.metadata.creationTimestamp | |
| List all resources in namespace | kubectl get all -n production | |
| Preview changes before applying | kubectl diff -f deployment.yaml | |
| Show resource documentation | kubectl explain pod.spec.containers | |
| Output as YAML | kubectl get pod api-7d8f9 -o yaml | |
| Check permissions | kubectl auth can-i create pods | |
| Safely evict pods from node | kubectl drain worker-1 --ignore-daemonsets | |
| Mark node unschedulable/schedulable | kubectl cordon worker-1 |
Frequently asked questions
What is a Pod?
A Pod is the smallest deployable unit in Kubernetes - it wraps one or more containers that share networking and storage. Pods are ephemeral (they can be killed and recreated). You rarely create Pods directly - use Deployments, which manage Pods for you with desired state, rolling updates, and self-healing.
What's the difference between a Deployment and a StatefulSet?
Deployments are for stateless apps - pods are interchangeable and can be scaled/replaced freely. StatefulSets are for stateful apps (databases, message queues) - each pod gets a stable hostname, persistent storage, and ordered startup/shutdown. Use Deployments unless you specifically need state.
How do Services work?
Services provide stable network endpoints for a set of Pods (selected by labels). ClusterIP: internal-only. NodePort: exposes on each node's IP. LoadBalancer: provisions external load balancer. Services handle load balancing and service discovery - pods come and go, but the Service IP stays constant.
How do I debug a pod that keeps crashing?
1) kubectl describe pod name - check Events for errors. 2) kubectl logs name --previous - see logs from the crashed container. 3) kubectl get events - check for node issues. 4) kubectl run debug --image=busybox -it --rm -- sh - run a debug container in the same namespace.
What are ConfigMaps and Secrets?
ConfigMaps store non-sensitive configuration (env vars, config files). Secrets store sensitive data (passwords, API keys) - base64 encoded (not encrypted by default). Both can be mounted as files or injected as environment variables into pods. Use external secret managers for production.
How does horizontal pod autoscaling work?
HPA automatically adjusts replica count based on metrics (CPU, memory, custom). Create with: kubectl autoscale deploy api --min=2 --max=10 --cpu-percent=80. HPA checks metrics every 15 seconds and scales up/down to maintain the target. Requires metrics-server installed.
Go from reference to real skills
Cheat sheets are great for quick lookups. Our in-depth courses take you from the fundamentals to professional-level mastery.
Browse all courses