78

How can I use kubectl or the API to retrieve the current image for containers in a pod or deployment?

For example, in a deployment created with the below configuration, I want to retrieve the value eu.gcr.io/test0/brain:latest.

apiVersion: v1
   kind: Deployment
   metadata:
     name: flags
   spec:
     replicas: 6
     template:
       metadata:
      labels:
        app: flags
       spec:
         containers:
         - name: flags
           image: eu.gcr.io/test0/brain:latest
Andy Hume
  • 40,474
  • 10
  • 47
  • 58

15 Answers15

104

From kubectl 1.6 the -o wide option does this, so

kubectl get deployments -o wide

will show the current image in the output.

Daniel Perez
  • 6,335
  • 4
  • 24
  • 28
56

You can use kubectl's jsonpath output option to achieve this:

kubectl get deployment flags -o=jsonpath='{$.spec.template.spec.containers[:1].image}'
Pixel Elephant
  • 20,649
  • 9
  • 66
  • 83
  • 4
    You'll need single or double quotes surrounding the JSONpath filter if you're using the zsh shell. It's a good practice to always use them. – Antoine Cotten Aug 23 '16 at 09:04
  • This is actually the only expression that worked for me to find out the image used in a multicontainer pod. This command shows only the image of the first container in contrast to all containers when you use "{..image}". And in my case I needed exactly this behavour! – Jeff Mar 18 '21 at 23:25
18

to get just the image uri for all pods (in all namespaces, for example):

kubectl get pods --all-namespaces -o jsonpath="{..image}"

(see https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/ for more detail)

eversMcc
  • 1,086
  • 11
  • 15
  • work for me. For a single image: kubectl get deploy/deployment-name -o jsonpath="{..image}" – Shalkam Apr 12 '19 at 13:43
  • That shows you the images on the currently running pods. If your deployment has failed, for example if you have a crash in your app code that prevents the pod from starting, you may have a different image in the deployment than you have on the pods that are still running. – Aeon May 13 '22 at 22:25
13

For a single deployment use this:

kubectl get deploy/deployment-name -o jsonpath="{..image}"

It can work for pod too

kubectl get pod/pod-name -o jsonpath="{..image}"
Shalkam
  • 733
  • 6
  • 12
  • Here's the [Kubernetes reference for JSONPath-support](https://kubernetes.io/docs/reference/kubectl/jsonpath/) – Dominik Sep 09 '22 at 15:52
9

You can list all deployments' image tag in a list:

kubectl get deployment -o=jsonpath="{range .items[*]}{'\n'}{.metadata.name}{':\t'}{range .spec.template.spec.containers[*]}{.image}{', '}{end}{
end}"

Sample output:

deployment-a:   docker-registry.com/group/image-a:v1,
deployment-b:   docker-registry.com/group/image-b:v2,
deployment-c:   docker-registry.com/group/image-c:v3,
deployment-d:   docker-registry.com/group/image-d:v4,
realhu
  • 935
  • 9
  • 12
  • command without line breakes `kubectl get deployment -o=jsonpath="{range .items[*]}{'\n'}{.metadata.name}{':\t'}{range .spec.template.spec.containers[*]}{.image}{', '}{end}{end}"` – klapshin Nov 15 '21 at 16:31
7

the following worked for me:

kubectl get deployment -o=jsonpath='{$.items[:1].spec.template.spec.containers[:1].image}'

..my deployment config was clearly different (with 'items' element at the start) for some reason.

UPDATE: The 'items' element (which is just a list of deployment elements) will appear if just doing:

kubectl get deployment -o=json

whereas if I specify the deployment name, there'll be no items element in the returned json, e.g.:

kubectl get deployment [deploymentName] -o=json
eversMcc
  • 1,086
  • 11
  • 15
3

You can use,

kubectl get pod <pod_name> -o yaml| grep image:

And from deployment,

kubectl get deploy <deployment_name> -o yaml| grep image:

Sachin Arote
  • 907
  • 13
  • 22
3

Approaches mentioned in other answers like

kubectl get deployment deploymentname -o=jsonpath='{$.spec.template.spec.containers[:1].image}'

return image even if current rollout is unsuccessful. Use this approach if you want to get image from successful/running pod

kubectl get po -l app=deploymentname --field-selector status.phase=Running  -o=jsonpath='{.items[*].spec.containers[*].image}'
Abdul Rauf
  • 5,798
  • 5
  • 50
  • 70
2

To get all the images being used by the deployments, you can use below command.

kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' |\ sort
Dean Winchester
  • 352
  • 3
  • 7
2

You can use custom column to get the image name

kubectl get deploy -o custom-columns=DEPLOYMENT-NAME:.metadata.name,IMAGE-NAME:.spec.template.spec.containers[*].image
GANESH CHOKHARE
  • 316
  • 3
  • 8
1

I often use this for gaining better image insight into a pod:

kubectl get   --output json pods \
  | jq '.items[] .status.containerStatuses[] | { "name": .name, "image": .image, "imageID": .imageID }'

Ouputs:

{
  "name": "app-admin",
  "image": "***docker_app-admin:v1",
  "imageID": "***docker_app-admin@sha256:2ce3208649e72faaf1fe8be59649de01b36f656866498b46790adaf154eefc6b"
}
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
1
kubectl get deployments.apps -n admin2406 -o=custom-columns=DEPLOYMENT:.metadata.name,CONTAINER_IMAGE:.spec.template.spec.containers[*].image,READY_REPLICAS:.status.readyReplicas,NAMESPACE:.metadata.namespace
Sandeep Arora
  • 389
  • 3
  • 4
  • 4
    Hi @Sandeep Arora, please add a comment to explain your answer, Thanks! – baikho Dec 18 '20 at 19:54
  • This command utilize custom-columns option where we can specifically list columns from the json structure of the service description. In my version CONTAINER_IMAGE:.containers[*].image worked. To get correct path of your version use get pods with -output json option to see location of image node in json. – Santhosh V Apr 29 '22 at 05:06
1

If you are looking for deployment, it would be deployment specific, but if one wants to search images in all objects like: deployment, statefulset, demaonset, jobs etc, the following simple command would do the trick:

kubectl get all -n <namespace> -o jsonpath={..image}} | tr ' ' '\n' | sort -u
0

Even though the question has already been answered, I wanted to provide an example using Go templating:

kubectl get deployment $GKE_DEPLOYMENT_NAME \
--namespace=$GKE_DEPLOYMENT_NAMESPACE \
--output=go-template \
--template='{{range .spec.template.spec.containers}}{{.image}}{{"\n"}}{{end}}'

#=>

us.gcr.io/. . ./. . .:xxxxxxx

Note: if your deployment contains more than one container, this command will list all Docker Images; since containers is either a JSON array or YAML sequence, there is no guaranteed order. I.e. selecting by element index isn't guaranteed to return the intended Docker Image Repo. and Tag.

Note: if you are using this command in order to determine what git commit is currently deployed, through Docker Tags or Docker Labels, you will need to introduce string manipulation.

You may want to label your deployment instead:

kubectl label \
--overwrite=true \
deployment $GKE_DEPLOYMENT_NAME \
commit=$(git rev-parse --short HEAD) \
--namespace=$GKE_DEPLOYMENT_NAMESPACE \
--record=true

#=>

deployment.apps/$GKE_DEPLOYMENT_NAME labeled

and access this label, regardless of multiple Docker Images and without string manipulation, using:

kubectl get deployment $GKE_DEPLOYMENT_NAME \
--namespace=$GKE_DEPLOYMENT_NAMESPACE \
--output=go-template \
--template='{{index .metadata.labels "commit"}}{{"\n"}}'

#=>

xxxxxxx
Mike
  • 1,080
  • 1
  • 9
  • 25
0

Accepted answer give hint but if someone is looking for working example as asked in question

kubectl -n dev get sts  -l 'app in (app1,app2)' -o wide --no-headers | awk '{print $5}' 
ImranRazaKhan
  • 1,955
  • 5
  • 33
  • 74