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
On my k8s system kubectl describe pod ABC doesn’t show the image size, but you can create a script that:
All-in-all it looks like this:
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.If you have the correct permissions, get the nodes not the pods.
Check if which container runtime is used in the Kubernetes platform. For example if ‘microk8s’, here is the command:
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