Cloud Native 7 min read

Unlock Hidden kubectl Tricks: Boost Your Kubernetes Workflow

This guide showcases advanced kubectl techniques—including printing API details, filtering and deleting pods by status, counting node‑wise pod distribution, and leveraging kubectl proxy—to help Kubernetes users streamline debugging and routine cluster management tasks.

Efficient Ops
Efficient Ops
Efficient Ops
Unlock Hidden kubectl Tricks: Boost Your Kubernetes Workflow

kubectl is the official command-line tool for Kubernetes, enabling convenient cluster operations. This article presents several advanced kubectl usages for readers with basic Kubernetes experience.

1. Print the API version in use

# Show API interaction details, useful 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 running on a specific node

# Use field selector for node name
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(ind in count){ printf("%-35s: %d
",ind,count[ind]); } }'

5. Using kubectl proxy

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

# Proxy options
--accept-hosts='^localhost$,^127\.0\.0\.1$,^\[::1\]$'
--reject-paths='^/api/.*/pods/.*/exec,^/api/.*/pods/.*/attach'
# Disable reject paths
kubectl proxy -p 8080 --keepalive 3600s --reject-paths='' -v=9

kubectl is a powerful tool; the commands above are examples of practical tricks that can save time when debugging or performing ad‑hoc tasks, without needing to write 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.

Kuberneteskubectlcommand-line
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.