Master Advanced kubectl Tricks for Faster Kubernetes Debugging
This article presents a collection of powerful kubectl commands and techniques—including API inspection, status‑based pod filtering, node‑specific pod listing, distribution counting with awk, and proxy usage—to help Kubernetes users troubleshoot and manage clusters more efficiently.
1 Print the current API version
# kubectl primarily interacts with the ApiServer; the following command prints detailed API information, useful for debugging custom APIs.
$ kubectl get ns -v=92 Filter pods by status and delete them
This snippet shows how to locate pods in a specific phase (e.g., Pending) across all namespaces, generate delete commands with jq, and execute them via xargs. Variants can target other phases such as Failed or specific deployments.
$ kubectl get pods --all-namespaces --field-selector status.phase=Pending -o json \
| jq '.items[] | "kubectl delete pods \(.metadata.name) -n \(.metadata.namespace)"' \
| xargs -n 1 bash -c
# Example for running pods
$ kubectl get pods --all-namespaces --field-selector status.phase=Running -o json \
| jq '.items[] | "kubectl get pods \(.metadata.name) -o wide -n \(.metadata.namespace)"'3 List all pods running on a specific node
Using a field selector you can directly query pods scheduled on a given node.
$ kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=pve-node14 Count pod distribution across nodes
An awk pipeline extracts the node name column and aggregates occurrences, providing a quick view of how many pods run on each machine.
$ kubectl -n default get pods -o wide -l app="nginx" \
| awk '{print $7}' \
| awk '{ count[$0]++ }
END {
printf("%-35s: %s
","Node","Count");
for (n in count) printf("%-35s: %d
", n, count[n]);
}'
# Sample output
Node : Count
pve-node1 : 1
pve-node2 : 15 Using kubectl proxy
The proxy creates a local HTTP proxy to the Kubernetes API server, allowing direct API calls without additional authentication. After starting the proxy, you can issue curl requests against localhost:8080 and increase verbosity for debugging.
# Start the proxy on port 8080
$ KUBECONFIG=~/.kube/config-symv3 kubectl proxy -p 8080
# Verify the API server is reachable
$ curl -k -v -X GET -H "Accept: application/json,*/*" \
-H "User-Agent: kubectl/v1.21.3 (linux/amd64) kubernetes/ca643a4" \
'http://localhost:8080/api?timeout=32s'The default proxy blocks certain APIs (e.g., /api/.../pods/.../exec ) and only allows local host access. You can relax these restrictions with options such as: <code># Allow any host --accept-hosts='^localhost$,^127\.0\.0\.1$,^\[::1\]$' # Remove path restrictions to enable exec/attach --reject-paths='' $ kubectl proxy -p 8080 --keepalive 3600s --reject-paths='' -v=9 </code>
Even if the proxy seems unnecessary for simple tasks, it becomes valuable when you need to inspect raw API requests, for example while debugging the Kubernetes Dashboard.
6 Summary
kubectl is a versatile CLI tool; the commands above illustrate several advanced usages that can simplify debugging and routine operations without diving into the client‑API library.
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.
