Unlock Hidden kubectl Tricks: Boost Your Kubernetes Workflow
This article shares a collection of practical kubectl commands and tips—including API debugging, pod filtering and deletion, node‑wise pod statistics, and proxy usage—to help Kubernetes users work more efficiently and avoid writing custom client code.
kubectl is the official command‑line tool for Kubernetes, enabling convenient interaction with a cluster. The following tips assume readers already have basic Kubernetes experience.
1. Print the current API calls
# kubectl primarily talks to the API server; this command helps debug API interactions.
$ kubectl get ns -v=92. Filter pods by status and delete them
Use jq together with kubectl to generate delete commands for pods in a specific phase (e.g., Pending) across all namespaces.
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)"'
# Delete Completed pods in a single namespace
kubectl -n default get pods | grep Completed | awk '{print $1}' | xargs kubectl -n default delete pods3. List all pods on a specific node
kubectl supports label and field selectors; the following command shows pods scheduled on pve-node1:
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=pve-node14. Count pod distribution across nodes
Combine awk with kubectl to tally how many pods of a given app run on each node.
kubectl -n default get pods -o wide -l app="nginx" | awk '{print $7}' \
| awk '{ count[$0]++ } END { printf("%-35s: %s
","Word","Count"); for(ind in count){ printf("%-35s: %d
",ind,count[ind]); } }'
# Sample output
Word : Count
NODE : 1
pve-node1 : 1
pve-node2 : 15. Using kubectl proxy
The proxy acts as a lightweight API server gateway, allowing direct API calls without authentication. Starting a proxy on port 8080 lets you inspect traffic with high verbosity.
# Start proxy
KUBECONFIG=~/.kube/config-symv3 kubectl proxy -p 8080
kubectl get ns
NAME STATUS AGE
default Active 127d
# Proxy options (default restricts exec/attach)
--accept-hosts='^localhost$,^127\.0\.0\.1$,^\[::1\]$'
--reject-paths='^/api/.*/pods/.*/exec,^/api/.*/pods/.*/attach'
# To allow exec, clear reject‑paths
kubectl proxy -p 8080 --keepalive 3600s --reject-paths='' -v=9While some may think kubectl proxy is unnecessary, it becomes valuable when debugging components like the Kubernetes dashboard, as it logs full request details.
Summary
kubectl is a powerful CLI; the commands above illustrate a few advanced uses. You don’t need to memorize them all, but knowing they exist can save time when tackling ad‑hoc tasks without diving into the client API.
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.
