Master Advanced kubectl Tricks: Boost Your Kubernetes Workflow
This article presents a collection of practical kubectl techniques—including API debugging, status‑based pod filtering, node‑specific listings, pod distribution counting, and proxy usage—to help Kubernetes users streamline cluster management and avoid writing custom client code.
kubectl is the official command‑line tool for Kubernetes, enabling convenient cluster operations. This article shares several advanced kubectl usage patterns for users with basic Kubernetes experience.
1. Print the API version in use
# Print API interactions 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 on a specific node
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
","Node","Count"); for (n in count) printf("%-35s: %d
",n,count[n]); }'5. Using kubectl proxy
# Start proxy on port 8080
KUBECONFIG=~/.kube/config-symv3 kubectl proxy -p 8080
kubectl get ns
# Adjust proxy restrictions
kubectl proxy -p 8080 --keepalive 3600s --reject-paths=''kubectl is a powerful tool; the commands above illustrate practical ways to debug, filter, and manage resources without writing 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.
