<p>How can an application running in a pod on Kubernetes cluster find the currently running number of pods of the same image (instances of the same image)? Is there also a way to uniquely identify each pod in a collection of pods of the same type?</p>

<p>For Eg. If I have 3 pods of the same image running in my Kubernetes Cluster, I want my application running in the pods to know there are 3 instances running at the moment and possibly be able to identify them as 0 or 1 or 2 in the collection of 3 pods based on may be start time.</p>

<p>If your application needs to know that information, you will need the following:</p>

<ul>
<li>Access the Kubernetes API from within your application and ask for the information. Depending on the language you are using, you can find different client libraries: <a href="https://github.com/kubernetes-client" rel="nofollow noreferrer">https://github.com/kubernetes-client</a></li>
<li>An easy way to count the pods of the same collection would be using labels. However, if you are using the API, you can directly parse the information in your application and filter whatever you need.</li>
<li>In order to access the information from the API, the pod will need a service account with the proper privileges, otherwise the RBAC default directives will not allow your application to retrieve that information. This link will help you: <a href="https://docs.bitnami.com/kubernetes/how-to/configure-rbac-in-your-kubernetes-cluster/" rel="nofollow noreferrer">https://docs.bitnami.com/kubernetes/how-to/configure-rbac-in-your-kubernetes-cluster/</a></li>
</ul>
