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.
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=92. 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 pods3. 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-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
","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=9kubectl 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.
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.
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.
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.
