6 Essential kubectl Tricks to Debug Kubernetes Deployments
After migrating from Docker to Docker Swarm and finally to Kubernetes, the author shares six practical kubectl troubleshooting techniques—including inspecting deployments, viewing events, streaming logs, exporting YAML, scaling pods, and port‑forwarding—to quickly identify and fix common cluster issues.
Having moved from Docker to Docker Swarm and then to Kubernetes, the author presents six highly useful kubectl troubleshooting techniques that help identify and resolve deployment problems.
1. Inspect Deployments and Pods
Use kubectl get deploy (optionally with -n <namespace> or --all-namespaces) to verify that a deployment is accepted and shows the expected replica count (e.g., 1/1 or 2/2). Then check the associated pods with kubectl get pod to ensure they started correctly.
2. View Cluster Events
Run kubectl get events --sort-by=.metadata.creationTimestamp to list recent events in a namespace, which is useful for spotting pod crashes or image‑pull failures. The related kubectl describe command (e.g., kubectl describe deploy/figlet -n openfaas) provides detailed object information, including node‑level resource constraints.
3. Stream Logs
Retrieve logs with kubectl logs deploy/cert-manager -n cert-manager. Add -f to follow the log stream. You can also filter by label, for example kubectl logs -l app=nginx. Tools like stern or kail can enhance log searching, though they may be distracting.
4. Export YAML Manifests
When working with resources generated by Helm or other tools, use kubectl get deploy/nginx-1 -o yaml to view the full manifest, including image versions and annotations. Add --export to save the YAML locally for editing, or use kubectl edit (setting VISUAL=nano if you prefer a simpler editor).
5. Scale Deployments
Temporarily stop a service by scaling its deployment to zero replicas: kubectl scale deploy/nginx-1 --replicas=0. Restore it with kubectl scale deploy/nginx-1 --replicas=1, which creates a new pod and restarts the application.
6. Port Forwarding
Expose a service locally without making it public on the Internet using kubectl port-forward deploy/nginx-1 8080:80. This works for both deployments and services, allowing you to test production‑like configurations. For external exposure, consider a LoadBalancer service or
kubectl expose deployment nginx-1 --port=80 --type=LoadBalancer.
These six commands form a practical toolbox for diagnosing and fixing issues in real Kubernetes clusters.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
