Get current image of kubernetes deployment

KubernetesKubectl

Kubernetes Problem Overview


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

Kubernetes Solutions


Solution 1 - Kubernetes

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

kubectl get deployments -o wide

will show the current image in the output.

Solution 2 - Kubernetes

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

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

Solution 3 - Kubernetes

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)

Solution 4 - Kubernetes

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,

Solution 5 - Kubernetes

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}"

Solution 6 - Kubernetes

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

Solution 7 - Kubernetes

You can use,

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

And from deployment,

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

Solution 8 - Kubernetes

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

Solution 9 - Kubernetes

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}'

Solution 10 - Kubernetes

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

Solution 11 - Kubernetes

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"
}

Solution 12 - Kubernetes

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

Solution 13 - Kubernetes

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}' 

Solution 14 - Kubernetes

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

Solution 15 - Kubernetes

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndy HumeView Question on Stackoverflow
Solution 1 - KubernetesDaniel PerezView Answer on Stackoverflow
Solution 2 - KubernetesPixel ElephantView Answer on Stackoverflow
Solution 3 - KuberneteseversMccView Answer on Stackoverflow
Solution 4 - KubernetesEvanView Answer on Stackoverflow
Solution 5 - KubernetesShalkamView Answer on Stackoverflow
Solution 6 - KuberneteseversMccView Answer on Stackoverflow
Solution 7 - KubernetesSachin AroteView Answer on Stackoverflow
Solution 8 - KubernetesMahendra BagulView Answer on Stackoverflow
Solution 9 - KubernetesAbdul RaufView Answer on Stackoverflow
Solution 10 - KubernetesSandeep AroraView Answer on Stackoverflow
Solution 11 - KubernetesChris StryczynskiView Answer on Stackoverflow
Solution 12 - KubernetesMikeView Answer on Stackoverflow
Solution 13 - KubernetesImranRazaKhanView Answer on Stackoverflow
Solution 14 - KubernetesVineet GuptaView Answer on Stackoverflow
Solution 15 - KubernetesGANESH CHOKHAREView Answer on Stackoverflow