← All one-liners·#052·Kubernetes Operations·self contained·beginner

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 -nr
What each stage does
  1. [01] kubectlkubectl get pods -A -o json
    Fetches all pods across all namespaces (-A) and outputs the result in JSON format.
  2. [02] jqjq -r '.items[].spec.containers[].image'
    Parses the JSON to extract the image name for every container in every pod, outputting raw strings (-r).
  3. [03] sortsort
    Sorts the list of images alphabetically, which is required before using uniq.
  4. [04] uniquniq -c
    Counts consecutive identical lines, prefixing each unique image with its occurrence count.
  5. [05] sortsort -nr
    Sorts 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.