Kubernetes cheat sheet
Everyday kubectl. Add -n <namespace> to any command, or set a default with kubectl config set-context --current --namespace=<ns>.
See what is running
kubectl get pods- List pods (add -o wide for node and IP)
kubectl get all- Pods, services, deployments in the namespace
kubectl get nodes- List cluster nodes
kubectl describe pod <pod>- Details and recent events of a pod
kubectl get events --sort-by=.lastTimestamp- Recent cluster events
kubectl get namespaces- List namespaces
Logs and debugging
kubectl logs <pod>- Pod logs (-f to follow, --previous after a crash)
kubectl exec -it <pod> -- sh- Open a shell in a pod
kubectl port-forward <pod> 8080:80- Reach a pod on localhost:8080
kubectl top pods- CPU and memory use (needs metrics-server)
kubectl run tmp --rm -it --image=busybox -- sh- Throwaway debugging pod
Create and change
kubectl apply -f file.yaml- Create or update from a file
kubectl delete -f file.yaml- Delete what a file defines
kubectl create deployment web --image=nginx- Quick deployment
kubectl expose deployment web --port=80- Create a service for it
kubectl edit deployment <name>- Edit the live object in your editor
kubectl set image deployment/web nginx=nginx:1.27- Change the container image
Scale and roll out
kubectl scale deployment web --replicas=3- Change the number of pods
kubectl rollout status deployment/web- Wait for a rollout to finish
kubectl rollout history deployment/web- Revision history
kubectl rollout undo deployment/web- Go back to the previous revision
kubectl rollout restart deployment/web- Restart all pods gradually
Config and context
kubectl config get-contexts- List clusters you can reach
kubectl config use-context <name>- Switch cluster
kubectl create secret generic app --from-literal=KEY=value- Create a secret
kubectl create configmap app --from-file=app.conf- Create a config map
Deployment YAML (apply with kubectl apply -f)
apiVersion: apps/v1 kind: Deployment- Object type
spec.replicas: 3- Number of pods
spec.selector.matchLabels: {app: web}- Which pods it manages (must match the template labels)
spec.template.spec.containers[].image- Container image to run
kind: Service spec.type: ClusterIP- Stable internal address for pods (LoadBalancer for external)
Learn it properly