kubectl is the official K8s command-line tool for conveniently operating K8s clusters. This article mainly introduces some unconventional kubectl usages. I hope readers have basic K8s usage experience.
One article also introduces some tricks; I happened to find it while writing this blog, so I’ll share it too:
Ready-to-use commands and tips for kubectl
Printing the APIs Currently Used
1 | # kubectl's main job is interacting with the ApiServer; we can print the interaction process like this. |

Filtering Containers by Status and Deleting Them
This is a command I learned here: Force Delete Evicted / Terminated Pods in Kubernetes
1 | kubectl get pods --all-namespaces --field-selector status.phase=Pending -o json | \ |
Counting All Pods Running on a Specific Machine
kubectl can use two kinds of selectors: label and field. See the official site’s introductions:
1 | # it's a kind of selector that can be combined with the awk or xargs above |
Counting the Pod Distribution Across Machines
I don’t know whether readers have seen my article: Implementing fine-grained pod control on a kubernetes-based PaaS platform.
The prerequisite for balanced distribution work is knowing the pod distribution across machines. The best way is simply counting after obtaining pod information —
this work can be done with awk.
1 | kubectl -n default get pods -o wide -l app="nginx" | awk '{print $7}'|\ |
Using kubectl proxy
You can understand this command as proxying K8s’s ApiServer; using this proxy you can call APIs directly without authentication.
After starting it, you can even achieve kubectl matryoshka. Here’s an example:
1 | # when you call kubectl directly without setting kubeconfig |
The default-started proxy blocks certain APIs and has some restrictions — e.g. you can’t use exec to enter pods. Use
kubectl proxy --helpto see, for example:
1 | # only allow local access |
Some say this kubectl proxy may be useless — that may just mean you don’t yet have an actual application scenario.
For example, when I want to debug K8s dashboard code: using the kubeconfig file directly, I can’t see the concrete request process.
If you add a proxy forwarding layer and set -v=9, you automatically get a logging tool — quite useful when debugging.
Summary
kubectl is a powerful command-line tool. Above I only introduced a bit of my exploration of its usage at work. I don’t encourage everyone to memorize these commands — I just hope that when readers need it, they can remember kubectl may have similar features, so there’s no need to study the client-api for a few temporary requirements.