简体   繁体   中英

Get a list of all running pods for own service

The situation

  • I have a Spring Boot service running inside Kubernetes
  • Multiple instance are running (the kubernetes deployment has multiple replicas configured)
  • I have an incoming HTTP from outside the cluster that hits on of the instances
  • I want to relay the fact to all other instances

Question

Is there an easy, no external things needed, solution to find a list of "all pods of my service"? I think I remember that this could be possible via DNS resolution but my Google-Fu seems to weak to find something.

Of course I can always to this via some other solution (eg Redis, Kafka, whatever) but since I only need this for a one-off feature I would like to not introduce any moving parts and keep it as simple as possible.

Internal K8s DNS would only get you the IP-address of the service. Luckily, there is the Endpoints resource exactly for the purpose of getting the Pods that back a Service.

With kubectl you can check endpoints out like this:

kubectl get endpoints nginx -o yaml

With this command you get hold of the Pod names:

kubectl get endpoints nginx -o=jsonpath='{.subsets[*].addresses[*].targetRef.name}'

To execute the same thing from your Spring Boot app, you can try to make use of the official Kubernetes Java client library.

You can list all pods behind a service by running

$ kubectl get endpoints <service-name> -o=jsonpath='{.subsets[*].addresses[*].ip}' | tr ' ' '\n' | xargs -I % kubectl get pods --field-selector=status.podIP=%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM