Unlock Hidden kubectl Tricks: Advanced Commands for Kubernetes Mastery
This guide presents a collection of advanced kubectl techniques—including printing API details, filtering and deleting pods by status, counting pods per node, analyzing pod distribution across machines, and leveraging kubectl proxy—providing practical command examples and explanations for experienced Kubernetes users.
1. Print the API version in use
The following command shows detailed API interactions, which is useful for debugging custom API calls.
# kubectl interacts with the API server; this command prints detailed API calls for debugging
$ kubectl get ns -v=92. Filter pods by status and delete them
This one‑liner retrieves pods in a specific phase (e.g., Pending) across all namespaces, converts the JSON output into delete commands with jq, and executes each command via xargs. Variations can target other phases such as Running 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 (list only)
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
Use a field selector to show every pod scheduled on a given node.
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=pve-node14. Count pod distribution across nodes
Combine kubectl with awk to produce a simple table of how many pods run on each node for a particular label.
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 adds a local HTTP layer in front of the API server, allowing you to call APIs without authentication and to capture request logs for debugging (e.g., when working on the Kubernetes dashboard).
# When no kubeconfig is set, kubectl defaults to http://localhost:8080
kubectl get ns -v=9
# Start a proxy listening on port 8080
KUBECONFIG=~/.kube/config-symv3 kubectl proxy -p 8080
kubectl get ns
# Proxy options (default restricts hosts and certain paths)
--accept-hosts='^localhost$,^127\.0\.0\.1$,^\[::1\]$' # allow only local hosts
--reject-paths='^/api/.*/pods/.*/exec,^/api/.*/pods/.*/attach' # block exec/attach
# Disable path rejection to allow exec
kubectl proxy -p 8080 --keepalive 3600s --reject-paths='' -v=9Conclusion
kubectl is a powerful command‑line interface for Kubernetes. The commands above illustrate less‑known usages that can streamline debugging, cleanup, and inspection tasks. While memorizing every option isn’t required, knowing that these capabilities exist helps you avoid writing custom client code for occasional needs.
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.
