6 Essential kubectl Tricks to Master Kubernetes Troubleshooting
Discover six practical kubectl commands—from inspecting deployments and events to streaming logs, editing YAML, scaling pods, and port-forwarding—that empower you to quickly diagnose and resolve common Kubernetes issues in real-world clusters effectively.
After migrating from Docker to Docker Swarm and then to Kubernetes, and handling countless API changes over the years, I’m eager to discover deployment issues and fix them.
Today I’ll share what I consider the most useful troubleshooting tips and additional tricks for using kubectl as a Swiss‑army‑knife.
kubectl – The Swiss Army Knife
kubectl is our Swiss Army knife; we often use it when problems arise, and knowing how to use it is crucial. Let’s start with real‑world cases to see how to apply it during issues.
Scenario: My YAML is accepted, but the service fails to start or run properly.
1. kubectl get deployment/pods
This command shows concise, useful information without overwhelming output.
kubectl get deploy
kubectl get deploy -n <namespace>
kubectl get deploy --all-namespaces # or -AIdeally you see something like 1/1 or 2/2, indicating the deployment was accepted and attempted.
Next, you may check kubectl get pod to see if the backing pods started correctly.
2. kubectl get events
This command prints events in a given namespace, perfect for finding key problems such as crashed pods or image‑pull failures.
$ kubectl get events --sort-by=.metadata.creationTimestampA related command is kubectl describe, which works with the object name, e.g.: kubectl describe deploy/figlet -n openfaas It provides detailed information, including node‑level issues that prevented pod startup.
3. kubectl logs
Many users misuse this command by first trying to locate the full pod name. You can log directly from a deployment:
kubectl logs deploy/cert-manager -n cert-managerAdd -f to follow the log stream:
kubectl logs deploy/cert-manager -n cert-manager -fYou can also filter by label, for example: kubectl logs -l app=nginx Tools like stern or kail can help match patterns, though they may be distracting.
4. kubectl get -o yaml
When working with YAML generated by other projects or Helm, you often need to inspect it. For example, to run a quick test:
kubectl run nginx-1 --image=nginx --port=80 --restart=AlwaysThen retrieve its manifest: kubectl get deploy/nginx-1 -o yaml You can add --export to save the YAML locally for editing and re‑apply, or use kubectl edit (set VISUAL=nano if you’re uncomfortable with vim).
5. kubectl scale
The scale command can shrink a deployment to zero replicas, effectively killing all pods, and then scale back up to recreate them.
kubectl scale deploy/nginx-1 --replicas=0
kubectl scale deploy/nginx-1 --replicas=16. Port forwarding
Port‑forwarding lets you expose a service from your local or remote cluster on any configured port without exposing it to the Internet. kubectl port-forward deploy/nginx-1 8080:80 This works not only for deployments or pods but also for services, which better simulate production configurations.
If you need to expose a service externally, use a LoadBalancer service or run:
kubectl expose deployment nginx-1 --port=80 --type=LoadBalancerTry these six commands and tricks on a real cluster; they should prove useful for diagnosing and fixing issues.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
