Kubectl Troubleshooting Cheat Sheet: Essential Kubernetes Commands
A quick reference to commonly used kubectl commands for troubleshooting Kubernetes clusters, checking logs, inspecting resources, and investigating events. Find the command you need and copy it into your terminal after replacing any placeholders.
Before you run a command: Replace values in <angle brackets>. Check your current Kubernetes context and namespace, especially before commands that change cluster state.
Cluster
| Purpose | Command |
|---|---|
| Check client and API server versions | kubectl version |
| View cluster endpoint information | kubectl cluster-info |
| View kubeconfig settings | kubectl config view |
| List available contexts | kubectl config get-contexts |
| Show the active context | kubectl config current-context |
| Switch Kubernetes context | kubectl config use-context <context-name> |
| List supported API versions | kubectl api-versions |
| List available API resources | kubectl api-resources |
Logs
| Purpose | Command |
|---|---|
| View Pod logs | kubectl logs <pod-name> -n <namespace> |
| Stream Pod logs | kubectl logs -f <pod-name> -n <namespace> |
| Stream logs from a specific container | kubectl logs -f <pod-name> -c <container-name> -n <namespace> |
| Show the last 20 log lines | kubectl logs --tail=20 <pod-name> -n <namespace> |
| Show logs from the past hour | kubectl logs --since=1h <pod-name> -n <namespace> |
| View logs from a previous container instance | kubectl logs --previous <pod-name> -n <namespace> |
| View logs from Pods matching a label | kubectl logs -l app=<app-name> -n <namespace> |
| View logs from a Pod in a Deployment | kubectl logs deployment/<deployment-name> -n <namespace> |
Note: For multi-container Pods, specify -c <container-name> when needed. The Deployment command does not collect logs from every replica; label-based log retrieval may also require additional options for multiple Pods.
Resources
| Purpose | Command |
|---|---|
| View node CPU and memory usage | kubectl top nodes |
| View Pod CPU and memory usage | kubectl top pods |
| List Pods across all namespaces | kubectl get pods -A |
| Inspect Pod details and recent events | kubectl describe pod <pod-name> -n <namespace> |
| List cluster nodes | kubectl get nodes |
| Prevent new Pods from scheduling on a node | kubectl cordon <node-name> |
| Allow scheduling on a node again | kubectl uncordon <node-name> |
| Drain a node for maintenance | kubectl drain <node-name> --ignore-daemonsets |
Metrics note: kubectl top requires a functioning Kubernetes Metrics API, commonly provided by Metrics Server.
Safety: cordon, uncordon and drain change cluster state. Drain evicts eligible workloads and can affect availability; PodDisruptionBudgets or local storage may prevent completion. --ignore-daemonsets does not remove DaemonSet-managed Pods.
Events
| Purpose | Command |
|---|---|
| List events in the current namespace | kubectl get events |
| Filter warning events | kubectl get events --field-selector type=Warning |
| View warning events with the events command | kubectl events --types=Warning |
Reference: kubectl Quick Reference (Kubernetes documentation).