Cloud Native 7 min read

Unlock Advanced kubectl Tricks for Faster Kubernetes Management

This article shares a collection of powerful kubectl commands and tips—including API debugging, status‑based pod filtering and deletion, node‑specific pod listing, distribution counting with awk, and proxy usage—to help experienced Kubernetes users work more efficiently and avoid manual API client coding.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Unlock Advanced kubectl Tricks for Faster Kubernetes Management

kubectl is the official command‑line tool for Kubernetes, and this article presents several advanced usage patterns for users who already have basic K8s experience.

Print the current API version

# kubectl's main role is to interact with the API server.
# This command is especially useful for debugging your own API interfaces.
$ kubectl get ns -v=9

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

For a single namespace you can use a simpler pipeline:

kubectl -n default get pods | grep Completed | awk '{print $1}' | xargs kubectl -n default delete pods

List all pods running on a specific node

kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=pve-node1

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(i in count){ printf("%-35s: %d
",i,count[i]); } }'

Sample output shows each node and the number of matching pods.

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 access the API locally.

KUBECONFIG=~/.kube/config-symv3 kubectl proxy -p 8080
kubectl get ns

By default the proxy restricts certain paths (e.g., exec/attach). You can relax these limits:

kubectl proxy -p 8080 --keepalive 3600s --reject-paths='' -v=9

Running a proxy with high verbosity provides a useful log for debugging tools such as the Kubernetes dashboard.

Conclusion

kubectl is a powerful CLI; the commands above illustrate some of its lesser‑known capabilities. You don’t need to memorize them all, but knowing they exist can save time when tackling ad‑hoc tasks without writing custom client code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLIProxyKubernetesDevOpsShellCluster Managementkubectl
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.