List and Sort Running Container Images by Frequency
Extracts all container images from running pods across all namespaces and sorts them by usage frequency.
Setup
- → kubectl configured with cluster access
- → jq installed
Cost per run
Free
The one-liner
$ kubectl get pods -A -o json | jq -r '.items[].spec.containers[].image' | sort | uniq -c | sort -nrWhat each stage does
- [01] kubectl
kubectl get pods -A -o jsonFetches all pods across all namespaces (-A) and outputs the result in JSON format. - [02] jq
jq -r '.items[].spec.containers[].image'Parses the JSON to extract the image name for every container in every pod, outputting raw strings (-r). - [03] sort
sortSorts the list of images alphabetically, which is required before using uniq. - [04] uniq
uniq -cCounts consecutive identical lines, prefixing each unique image with its occurrence count. - [05] sort
sort -nrSorts the counted list numerically (-n) in reverse order (-r) to show the most frequent images first.
Expected output (sample)
42 nginx:1.21.6-alpine 15 fluent/fluent-bit:1.8.11 8 prom/node-exporter:v1.3.1 3 redis:6.2.6 1 postgres:13.5
Caveats & tips
- Requires `get pods` RBAC permissions across all namespaces, typically needing a ClusterRole.
- Does not include initContainers or ephemeral containers unless explicitly added to the jq filter.
- Large clusters with thousands of pods might cause high memory usage in jq or slow API server responses.