Aggregate K8s Pod Counts by Namespace and Phase
Extracts all pods from a Kubernetes cluster and uses DuckDB to aggregate counts by namespace and phase.
Setup
- → kubectl configured with cluster access
- → jq
- → duckdb
Cost per run
Free
The one-liner
$ kubectl get pods -A -o json | \
jq -c '.items[]' | \
duckdb -c "SELECT metadata.namespace, status.phase, count(*) as pod_count FROM read_json_auto('/dev/stdin') GROUP BY ALL ORDER BY pod_count DESC;"What each stage does
- [01] kubectl
kubectl get pods -A -o jsonFetches all pods across all namespaces and outputs the result as a single JSON object. - [02] jq
jq -c '.items[]'Extracts the array of pod objects and streams them as newline-delimited JSON (NDJSON), which DuckDB can easily ingest. - [03] duckdb
duckdb -c "SELECT ... FROM read_json_auto('/dev/stdin')"Reads the NDJSON stream from standard input directly into a DuckDB table, automatically inferring the schema. - [04] duckdb
GROUP BY ALL ORDER BY pod_count DESCAggregates the pod counts by namespace and phase, sorting the highest counts first.
Expected output (sample)
┌─────────────┬─────────┬───────────┐ │ namespace │ phase │ pod_count │ │ varchar │ varchar │ int64 │ ├─────────────┼─────────┼───────────┤ │ kube-system │ Running │ 12 │ │ default │ Running │ 5 │ │ monitoring │ Running │ 3 │ └─────────────┴─────────┴───────────┘
Caveats & tips
- Requires cluster-wide read permissions (get pods at the cluster scope).
- For very large clusters (10,000+ pods), the initial JSON payload might be large; ensure sufficient memory for jq to parse the initial array.