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
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"
}
From kubectl
1.6 the -o wide
option does this, so
kubectl get deployments -o wide
will show the current image in the output.
You can use kubectl's jsonpath output option to achieve this:
kubectl get deployment flags -o=jsonpath='{$.spec.template.spec.containers[:1].image}'
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)
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,
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}"
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
You can use,
kubectl get pod <pod_name> -o yaml| grep image:
And from deployment,
kubectl get deploy <deployment_name> -o yaml| grep image: