Introduction
Sometimes we have a requirement to find a particular image which any pod is using. But you are not sure about the image. In that case we have the describe command to check and see all the information about the pod. The problem occurs when you have multiple pods and you can't write multiple describe commands to find that image. So we'll do it differently using a loop like Pro.
Prerequisites
You need to have the Kubernetes installed on your machine and it's up and running. You can check it by running below command.
kubectl get pods
If you see some error like command not found then probably you don't have Kubernetes installed on your machine so you need to get it installed.
Let's start
First we'll create multiple Pods on our cluster with different images. Code is below:
for img in nginx3 alpine alpine2 alpine3
do
kubectl run $img --image=$img
done
Results:
pod/nginx3 created
pod/alpine created
pod/alpine2 created
pod/alpine3 created
So I used a for loop in bash script and passed multiple images name and created multiple Pods at once.
Now I am checking the status of pods. Since i used wrong images so there must be multiple pods in not running status.
kubectl get pods
NAME READY STATUS RESTARTS AGE
alpine 0/1 CrashLoopBackOff 4 (56s ago) 3m10s
alpine2 0/1 ImagePullBackOff 0 3m10s
alpine3 0/1 ImagePullBackOff 0 3m10s
nginx 1/1 Running 0 3h38m
nginx2 1/1 Running 0 3h33m
nginx3 0/1 ImagePullBackOff 0 3m10s
So we have multiple Pods now, we can look for images using our favourite for loop.
Let's list Pod and it's image.
for pds in $(kubectl get pods --no-headers -o custom-columns=":metadata.name")
do
echo "*************Pod_Name: $pds**************"
kubectl describe pods $pds | grep -i image:
done
Results:
********************Pod_Name: alpine***************
Image: alpine
********************Pod_Name: alpine2**************
Image: alpine2
********************Pod_Name: alpine3**************
Image: alpine3
*******************Pod_Name: nginx*****************
Image: nginx
*******************Pod_Name: nginx2****************
Image: nginx
*******************Pod_Name: nginx3****************
Image: nginx3
Now we can see which Pod is using which image. But this solution is still not feasible because this will give me all the pods and images in default namespace. I need to search one image ie. alpine3 among all Pods.
for pds in $(kubectl get pods --no-headers -o custom-columns=":metadata.name")
do
echo "************Pod_Name: $pds**************"
kubectl describe pods $pds | grep -i "image: alpine3"
done
Just search alpine3 in your grep command and see results below. You can use if else to remove extra print from the output.
for pds in $(kubectl get pods --no-headers -o custom-columns=":metadata.name")
do
if kubectl describe pods $pds | grep -q -i "image: alpine3"
then
echo "***********Pod_Name: $pds*****************"
else
echo
fi
done
*************Pod_Name: alpine3**************
Conclusion
Combining your shell scripting skill can be very useful in Kubernetes. You can get multiple things done quickly. This was example to find images in multiple Pods but you can use above script to find other properties as well like variables and configs. Using shell script you can automate lot of stuff on Kubernetes.