☸️

kubectl Cheat Sheet

Essential Kubernetes commands — pods, logs, exec, rollout, and debugging.

Related: kubectl · kubernetes · helm · minikube

Cluster & context

CommandDescription
kubectl config get-contextsList kubeconfig contexts
kubectl config use-context <name>Switch cluster context
kubectl cluster-infoAPI server and services URL
kubectl get nodes -o wideNode status and IPs

Workloads

CommandDescription
kubectl get pods -AAll pods in all namespaces
kubectl get deploy,svc,ingress -n <ns>Common resources in namespace
kubectl describe pod <pod> -n <ns>Events and container state
kubectl logs <pod> -n <ns> -fStream container logs
kubectl logs <pod> -c <container> --previousLogs from crashed container
kubectl exec -it <pod> -n <ns> -- /bin/shShell into running pod

Apply & rollout

CommandDescription
kubectl apply -f deployment.yamlCreate or update resources
kubectl apply -k overlays/prod/Apply Kustomize overlay
kubectl rollout status deploy/<name>Wait for rollout to finish
kubectl rollout undo deploy/<name>Rollback to previous revision
kubectl rollout history deploy/<name>List revision history
kubectl delete -f deployment.yamlRemove resources from file

Debug

CommandDescription
kubectl get events -n <ns> --sort-by='.lastTimestamp'Recent cluster events
kubectl port-forward svc/<name> 8080:80Local access to a service
kubectl top pods -n <ns>CPU/memory (needs metrics-server)