1
votes

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.

2
You will need to contact the Kubernetes master using the API.rjdkolb
what is the use case for an application to find out how many instances running ?Here_2_learn
@Here_2_learn for each pod to split the total work without using another application for work-splitting and orchestration.Chinmay Nerurkar
its the duty of kubernetes, your app has nothing to do with work-splitting and orchestration.Here_2_learn

2 Answers

1
votes

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/
0
votes

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.