Skip to content

Kubectl Filtering Masterclass

Stop scrolling through 500 lines of YAML. The CKA requires you to answer specific data questions like "Which nodes have taints?" or "What image is the kube-scheduler using?" instantly.

1. JSONPath: The Scalpel

The -o jsonpath flag is your surgical tool.

Syntax: kubectl get [resource] -o jsonpath='{.path.to.field}'

Recipe: Get Internal IP of all Nodes

bash
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'

Recipe: Get Pod Name & Image List

bash
kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[*].image}{"\n"}{end}'

2. Custom Columns: The Report Generator

Easier than JSONPath for simple tables.

Recipe: Scheduling Audit Table Shows Pod Name, Node Name, and QOS Class.

bash
kubectl get pods -A -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName,QOS:.status.qosClass

3. Sorting (--sort-by)

Kubernetes can sort output based on any field.

Recipe: Find the Oldest Pod

bash
kubectl get pods --sort-by=.metadata.creationTimestamp

Recipe: Find the Pod Restarting the Most (Note: This requires scripting as restartCount is inside the container status list, but simple sorting works well for top-level fields).

4. The "No-Headers" Trick

When you need to count things. kubectl get pods --no-headers | wc -l