Cloud Native 7 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Unlock Hidden kubectl Tricks: Advanced Commands for Kubernetes Mastery

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=9

2. 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 pods

3. List all pods on a specific node

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

4. 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 ns

The 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=9

Conclusion: 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.

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.

CLIOperationsKubernetescommandskubectl
Liangxu Linux
Written by

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.)

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.