Kubernetes Cheat Sheet

Essential kubectl commands and Kubernetes concepts. Pods, deployments, services, configmaps, secrets, namespaces, and common troubleshooting commands.

Cluster Info Pods Deployments Services & Networking ConfigMaps & Secrets Namespaces & Context Troubleshooting Common Patterns

Cluster Info

kubectl cluster-info Display cluster endpoint info
kubectl get nodes List all nodes
kubectl get nodes -o wide List nodes with extra details
kubectl top nodes Show node resource usage
kubectl api-resources List all resource types
kubectl api-versions List all API versions
kubectl version --short Show client and server versions
kubectl config current-context Show current context

Pods

kubectl get pods List pods in current namespace
kubectl get pods -A List pods in all namespaces
kubectl get pods -o wide List pods with node and IP info
kubectl describe pod Show detailed pod info
kubectl logs View pod logs
kubectl logs -c Logs from specific container
kubectl logs -f Stream logs (follow)
kubectl exec -it -- /bin/sh Shell into pod
kubectl port-forward 8080:80 Forward local port to pod
kubectl delete pod Delete a pod

Deployments

kubectl create deployment --image= Create deployment
kubectl get deployments List deployments
kubectl scale deployment --replicas=3 Scale to 3 replicas
kubectl set image deployment/ =:v2 Update container image
kubectl rollout status deployment/ Watch rollout progress
kubectl rollout history deployment/ View rollout history
kubectl rollout undo deployment/ Rollback to previous
kubectl rollout undo deployment/ --to-revision=2 Rollback to specific version

Services & Networking

kubectl expose deployment --port=80 --type=ClusterIP Create ClusterIP service
kubectl expose deployment --port=80 --type=NodePort Create NodePort service
kubectl expose deployment --port=80 --type=LoadBalancer Create LoadBalancer service
kubectl get svc List services
kubectl get endpoints List service endpoints
kubectl get ingress List ingress resources
kubectl describe svc Show service details

ConfigMaps & Secrets

kubectl create configmap --from-literal=key=val Create ConfigMap from literal
kubectl create configmap --from-file=config.yaml Create ConfigMap from file
kubectl get configmaps List ConfigMaps
kubectl create secret generic --from-literal=pw=123 Create Secret from literal
kubectl create secret generic --from-file=creds.json Create Secret from file
kubectl get secrets List Secrets
kubectl get secret -o jsonpath="{.data.pw}" | base64 -d Decode secret value

Namespaces & Context

kubectl get namespaces List all namespaces
kubectl create namespace Create namespace
kubectl config set-context --current --namespace= Switch default namespace
kubectl config get-contexts List all contexts
kubectl config use-context Switch context
kubectl get all -n List all resources in namespace

Troubleshooting

kubectl logs --previous Logs from crashed container
kubectl describe pod Check events and conditions
kubectl get events --sort-by=.metadata.creationTimestamp Recent cluster events
kubectl top pod Show pod CPU/memory usage
kubectl debug -it --image=busybox Debug with ephemeral container
kubectl run debug --image=busybox -it --rm -- sh Temporary debug pod
kubectl get pod -o yaml Export pod spec as YAML

Common Patterns

kubectl apply -f deployment.yaml Apply manifest file
kubectl apply -f ./dir/ Apply all manifests in directory
kubectl delete -f deployment.yaml Delete resources from file
kubectl get pods -l app=web Filter by label selector
kubectl get pods -o json | jq ... JSON output for scripting
kubectl get pod -o jsonpath="{.status.phase}" Extract field with jsonpath
kubectl diff -f updated.yaml Preview changes before apply
kubectl create deployment --image= --dry-run=client -o yaml Generate YAML template
Step-by-Step Guide

How to Build Docker Compose

Read Guide →

More Cheat Sheets