Unlock Advanced kubectl Tricks to Master Kubernetes Clusters
This article shares practical kubectl tips—including printing API calls, filtering and deleting pods, counting pod distribution, and using kubectl proxy—to help experienced Kubernetes users work more efficiently and troubleshoot clusters with powerful command‑line shortcuts.
kubectl is the official command‑line tool for Kubernetes, enabling convenient cluster operations. This article presents several advanced kubectl usages for readers with basic K8s experience.
1. Print the current API interactions
# Show detailed API calls, useful for debugging
kubectl get ns -v=92. Filter pods by status and delete them
Example commands to force‑delete evicted or terminated pods using jq and xargs:
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 -cOther variations allow selecting different statuses or deployments, and a simpler single‑namespace deletion:
kubectl -n default get pods | grep Completed | awk '{print $1}' | xargs kubectl -n default delete pods3. List all pods running 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
","Node","Count");\
for (n in count) printf("%-35s: %d
", n, count[n]);\
}'Result example:
Node : Count
pve-node1 : 1
pve-node2 : 15. Using kubectl proxy
The proxy adds a layer in front of the API server, allowing direct API calls without authentication. After starting the proxy, you can debug requests, e.g., for the Kubernetes dashboard.
# Start proxy on port 8080
KUBECONFIG=~/.kube/config-symv3 kubectl proxy -p 8080
# Access API via kubectl
kubectl get nsBy default the proxy restricts certain APIs (e.g., exec/attach). You can relax these limits:
kubectl proxy -p 8080 --keepalive 3600s --reject-paths='' -v=9In summary, kubectl is a powerful CLI tool. The commands above are practical explorations rather than a checklist to memorize; they illustrate how kubectl can solve ad‑hoc tasks without writing custom client 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.
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.
