Kubernetes communication between pods using REST

1/12/2019

Right now I got an architecture like this:

          internet
             |
   [ IngressController ]
        |          |
   [ Ingress A] [ Ingress B]
   --|-----|--  --|-----|--
   [ Service A] [ Service B]
         |            |
     [ Pod A]     [ Pod B]

So if Service A request data from Service B it is using the full qualified name, e.g.

ResponseEntity<Object> response = restTemplate.exchange(host.com/serviceB, HttpMethod.POST, entity, Object.class);

As all of them are in the same cluster I would try to change the architecture to improve communication between the services. I imagined something like this:

          internet
             |
   [ IngressController ]
        |          |
   [ Ingress A] [ Ingress B]
   --|-----|--  --|-----|--
   [ Service A]-[ Service B]
         |            |
     [ Pod A]     [ Pod B]

So the services would be allowed to request each other via the name only or something. I am just not sure how to realize this using REST-Services.

-- elp
java
kubernetes
rest
resttemplate

1 Answer

1/13/2019

You need to call Service B by it's Kubernetes DNS name and it should connect directly without going through the ingress.

To clarify Service A doesn't talk to Service B, but rather Pod A talks to Service B and Pod B talks to Service A. So, long all pods and services are in the same Kubernetes namespace, you can communicate with the service with the service name as the hostname and that will resolve to the internal IP address of the service and then forward the traffic to the pod.

If the pods happen to be in different namespaces you would connect with the namespace name added to the service: <service-name>.<namespace-name> or with <service-name>.<namespace-name>.svc.cluster.local

Hope it helps.

-- Rico
Source: StackOverflow