简体   繁体   中英

How to implement Kubernetes POD to POD Communication?

This question has been asked and answered before on stackoverflow but because I'm new to K8, I don't understand the answer.

Assuming I have two containers with each container in a separate POD (because I believe this is the recommend approach), I think I need to create a single service for my two pods to be apart of.

  1. How does my java application code get the IP address of the service?
  2. How does my java application code get the IP addresses of another POD/container (from the service)?
  3. This will be a list of IP address because these are stateless and they might be replicated. Is this correct?
  4. How do I select the least busy instance of the POD to communicate with?

Thanks Siegfried

How does my java application code get the IP address of the service?

You need to create a Service to expose the Pod's port and then you just need to use the Service name and kube-dns will resolve the Pod's IP address

How does my java application code get the IP addresses of another POD/container (from the service)?

Yes, using the service's name

This will be a list of IP address because these are stateless and they might be replicated. Is this correct?

The Service will load balance between all pods that matches the selector, so it could be 0, 1 or any number of Pods

How do I select the least busy instance of the POD to communicate with?

Common way is round robin policy but here are other specific balancing policies https://kubernetes.io/docs/concepts/services-networking/service/#proxy-mode-ipvs

Cheers ;)

You don't need to get any IP, you use the service name (DNS). So if you called your service "java-service-1" and exposed port 80, you can access it this way from inside the cluster:

http://java-service-1

If the service is in a different namespace, you have to add that as well (see https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/ )

You also don't select the least busy instance yourself, a service can be configured as LoadBalancer, Kubernetes does all of this for you (see https://kubernetes.io/docs/concepts/services-networking/ )

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