skip to Main Content

In the Kubernetes documentation List All Container Images Running in a Cluster we can list all containers by:

kubectl get pods --all-namespaces -o jsonpath="{..image}" |
tr -s '[[:space:]]' 'n' |
sort |
uniq -c

Here is an example of output:

  1 cdkbot/addon-resizer-amd64:1.8.1
  1 cdkbot/hostpath-provisioner-amd64:1.0.0
  1 cdkbot/registry-amd64:2.6
  1 coredns/coredns:1.6.6
  1 docker.io/cdkbot/addon-resizer-amd64:1.8.1
  1 docker.io/cdkbot/hostpath-provisioner-amd64:1.0.0
  1 docker.io/cdkbot/registry-amd64:2.6
  1 docker.io/coredns/coredns:1.6.6
  1 docker.io/grafana/grafana:6.4.3
  2 docker.io/istio/citadel:1.5.1
  2 docker.io/istio/examples-bookinfo-details-v1:1.15.0
  2 docker.io/istio/examples-bookinfo-productpage-v1:1.15.0
  2 docker.io/istio/examples-bookinfo-ratings-v1:1.15.0
  2 docker.io/istio/examples-bookinfo-reviews-v1:1.15.0
  2 docker.io/istio/examples-bookinfo-reviews-v2:1.15.0
  2 docker.io/istio/examples-bookinfo-reviews-v3:1.15.0
  2 docker.io/istio/galley:1.5.1
  4 docker.io/istio/kubectl:1.5.1
  4 docker.io/istio/mixer:1.5.1
  2 docker.io/istio/pilot:1.5.1
 34 docker.io/istio/proxyv2:1.5.1
  2 docker.io/istio/sidecar_injector:1.5.1
  2 docker.io/jaegertracing/all-in-one:1.16
  1 docker.io/kubernetesui/dashboard:v2.0.0
  1 docker.io/kubernetesui/metrics-scraper:v1.0.4
  2 docker.io/library/nginx:latest
  2 docker.io/prom/prometheus:v2.12.0
  1 docker.io/radial/busyboxplus:curl
  1 grafana/grafana:6.4.3
  2 k8s.gcr.io/metrics-server-amd64:v0.2.1
  1 kubernetesui/dashboard:v2.0.0
  1 kubernetesui/metrics-scraper:v1.0.4
  2 nginx
  2 quay.io/kiali/kiali:v1.9
  1 radial/busyboxplus:curl

Unfortunately the size of the docker image is missing. Is there a way to get the container image size? Pretty much like docker image ls. Copy of the output from this example for convenience:

$ docker image ls
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu            latest              3556258649b2        7 days ago          64.2 MB
docker.io/alpine            latest              b7b28af77ffe        2 weeks ago         5.58 MB
docker.io/centos            latest              9f38484d220f        4 months ago        202 MB
docker.io/hello-world       latest              fce289e99eb9        7 months ago        1.84 kB

5

Answers


  1. On my k8s system kubectl describe pod ABC doesn’t show the image size, but you can create a script that:

    1. gets the name of the image (I assuming you have one container in the pod, otherwise the script will be slightly more complicated depending on what you want to print actually)
    2. runs docker image ls on the same machine. This assumes that you actually have that docker image on the machine you’re running this command.
    3. You slight format the output of that command to “catch” the image size

    All-in-all it looks like this:

    docker image ls `kubectl get pod YOUR_POD_NAME_GOES_HERE  -o jsonpath='{.spec.containers[0].image}'` --format='{{println .Size}}'
    
    Login or Signup to reply.
  2. Kubernetes doesn’t provide image size information in its outputs. Even though we able to get a list of images running in a cluster, it is not a straight forward process to get the size of an image in a multi-node cluster. But in such cases where we run a single node cluster, we could achieve this by running a docker or DinD (Docker in Docker) container in Kubernetes cluster. Still, it is a good approach for testing purposes only. I have nothing to say about running this on critical environments. It depends.

    Spin up a pod by mounting host’s /var/lib/docker.sock file inside and exec as shown below.

    apiVersion: v1
    kind: Pod
    metadata:
      labels:
        run: dind
      name: dind
    spec:
      containers:
      - image: docker:dind
        name: dind
        command: ["cat"]
        tty: true
        resources: {}
        volumeMounts:
        - mountPath: /var/run/docker.sock
          name: docker
          readOnly: true
      volumes:
      - name: docker
        hostPath:
          path: /var/run/docker.sock
          type: File
      dnsPolicy: ClusterFirst
      restartPolicy: Never
    status: {}
    

    $ kubectl exec -i dind -n dev sh -- docker images nginx 
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              4571e56e27f0        11 hours ago        132MB
    
    $kubectl exec -i dind -n dev sh -- docker image ls
    REPOSITORY                                                       TAG                 IMAGE ID            CREATED             SIZE
    nginx                                                            latest              4571e56e27f0        21 hours ago        132MB
    
    Login or Signup to reply.
  3. If you have the correct permissions, get the nodes not the pods.

    kubectl get nodes -o json | jq '.items[].status.images[] | .names[1], .sizeBytes'
    
    Login or Signup to reply.
  4. Check if which container runtime is used in the Kubernetes platform. For example if ‘microk8s’, here is the command:

    sudo microk8s ctr image ls
    
    Login or Signup to reply.
  5. I needed similar thing (but primarily list images and what pods are using them). So I created kubectl plugin which lists images (and their sizes as well) and also pods/containers that use them.
    This is the link – https://github.com/pete911/kubectl-image

    kubectl image list -A
    
    registry: registry.k8s.io
      coredns/coredns
        Tag/ID: v1.9.3      Size: 13.42MB
        ID:     sha256:b19406328e70dd2f6a36d6dbe4e867b0684ced2fdeb2f02ecb54ead39ec0bac0
                [namespace] kube-system [container] coredns [pod] coredns-565d847f94-kfs8m [pod-phase] Running
                [namespace] kube-system [container] coredns [pod] coredns-565d847f94-r9wvh [pod-phase] Running
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search