Unlock Hidden kubectl Tricks: Advanced Commands for Kubernetes Mastery
This article presents a collection of advanced kubectl techniques—including API inspection, status‑based pod filtering and deletion, node‑specific pod listing, distribution counting, and proxy usage—to help experienced Kubernetes users solve ad‑hoc tasks more efficiently.
kubectl is the official command‑line tool for Kubernetes, and this article shares several lesser‑known usages for readers who already have basic experience with K8s.
1. Print the API version used
# Show interaction with the API server for debugging
kubectl get ns -v=92. Filter pods by status and delete them
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 get pods --all-namespaces -o wide --field-selector spec.nodeName=pve-node14. Count pod distribution across nodes
kubectl -n default get pods -o wide -l app="nginx" | awk '{print $7}' | \
awk '{count[$0]++} END {printf("%-35s: %s
","Word","Count"); for (i in count) printf("%-35s: %d
",i,count[i])}'5. Using kubectl proxy
kubectl proxy adds a local HTTP proxy in front of the API server, allowing direct API calls without authentication. Example:
KUBECONFIG=~/.kube/config-symv3 kubectl proxy -p 8080
kubectl get nsThe proxy defaults to localhost only and rejects certain paths (e.g., exec, attach). Flags can relax these restrictions:
kubectl proxy -p 8080 --keepalive 3600s --reject-paths='' -v=9Conclusion: kubectl is a powerful tool; the commands shown are optional to memorize, but knowing they exist can help you address temporary needs without writing custom client‑API code.
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.
