Operations 7 min read

Master Advanced kubectl Tricks: Debug, Filter, and Automate Kubernetes Pods

This article shares a collection of powerful kubectl commands and techniques—including API debugging, status‑based pod filtering and deletion, node‑specific pod listing, pod distribution statistics, and proxy usage—to help Kubernetes operators work more efficiently and avoid manual API scripting.

Efficient Ops
Efficient Ops
Efficient Ops
Master Advanced kubectl Tricks: Debug, Filter, and Automate Kubernetes Pods

Print current API used

# kubectl interacts with the ApiServer; the following command prints the API calls
# Useful for debugging API interfaces.
kubectl get ns -v=9

Filter containers by status and delete

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

List all pods on a specific node

# Use a field selector to show pods scheduled on a given 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
","Word","Count");
      for (node in count) {
        printf("%-35s: %d
",node,count[node]);
      }
    }'

# Sample output
Word                               : Count
NODE                               : 1
pve-node1                          : 1
pve-node2                          : 1

Using kubectl proxy

You can run a local proxy that forwards requests to the Kubernetes API server, allowing you to call the API without authentication. After starting the proxy, you can inspect raw API traffic, which is handy for debugging dashboard code or other components.

# Start a proxy on port 8080
KUBECONFIG=~/.kube/config-symv3 kubectl proxy -p 8080
kubectl get ns

# Default proxy restrictions (e.g., exec is blocked)
# --accept-hosts='^localhost$,^127\.0\.0\.1$,^\[::1\]$'
# --reject-paths='^/api/.*/pods/.*/exec,^/api/.*/pods/.*/attach'

# Remove reject‑paths to allow exec via the proxy
kubectl proxy -p 8080 --keepalive 3600s --reject-paths='' -v=9

Conclusion

kubectl is a powerful command‑line tool; the commands above illustrate a few advanced usages discovered during daily work. You don’t need to memorize every flag, but knowing that these capabilities exist can save time when you face ad‑hoc operational needs, without having to dive into the 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.

CLIOperationsDevOpskubectl
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

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.