Cloud Native 7 min read

Unlock Advanced kubectl Tricks to Master Kubernetes Clusters

This article shares practical kubectl tips—including printing API calls, filtering and deleting pods, counting pod distribution, and using kubectl proxy—to help experienced Kubernetes users work more efficiently and troubleshoot clusters with powerful command‑line shortcuts.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Unlock Advanced kubectl Tricks to Master Kubernetes Clusters

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

1. Print the current API interactions

# Show detailed API calls, useful for debugging
kubectl get ns -v=9

2. Filter pods by status and delete them

Example commands to force‑delete evicted or terminated pods using jq and xargs:

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

Other variations allow selecting different statuses or deployments, and a simpler single‑namespace deletion:

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

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
","Node","Count");\
  for (n in count) printf("%-35s: %d
", n, count[n]);\
}'

Result example:

Node                               : Count
 pve-node1                         : 1
 pve-node2                         : 1

5. Using kubectl proxy

The proxy adds a layer in front of the API server, allowing direct API calls without authentication. After starting the proxy, you can debug requests, e.g., for the Kubernetes dashboard.

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

By default the proxy restricts certain APIs (e.g., exec/attach). You can relax these limits:

kubectl proxy -p 8080 --keepalive 3600s --reject-paths='' -v=9

In summary, kubectl is a powerful CLI tool. The commands above are practical explorations rather than a checklist to memorize; they illustrate how kubectl can solve ad‑hoc tasks without writing 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.

Cloud NativeKubernetesDevOpsTipskubectlcommand-line
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.