Chinmay Nerurkar
Chinmay Nerurkar

Reputation: 495

How can a container find the number of pods of a particular image running on the Kubernetes cluster

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?

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.

Upvotes: 1

Views: 1483

Answers (2)

Pamir Erdem
Pamir Erdem

Reputation: 186

In order to do that you can debug kubectl command. Kubectl takes a parameter -v9. You can get the endpoints of kubectl execution by making requests.

  • Kubectl get pods -v9 --all-namespaces.
  • kubectl describe deployment -v9

In the deployment output you can get desired state of pods running on your system. With -v9 you can get the endpoints of api server used by kubectl deployment command

Another option is searching on CoreDns and skydns. These always retrieve service list and pods by querying api server. You should take a look the source code of them.

Upvotes: 0

Javier Salmeron
Javier Salmeron

Reputation: 8825

If your application needs to know that information, you will need the following:

  • 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: https://github.com/kubernetes-client
  • 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.
  • 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: https://docs.bitnami.com/kubernetes/how-to/configure-rbac-in-your-kubernetes-cluster/

Upvotes: 2

Related Questions