Microservice calling another microservice in same kubernetes cluster

11/22/2019

If java/spring boot microservice A (deployed in its own container & with its own Kubernetes Service of type ClusterIP) needs to send a REST request to Java/spring boot microservice B (has its own Kubernetes Service of type ClusterIP) in the same Kubernetes cluster, what's the best way for A to determine B's kubernetes service IP (especially if B is redeployed)? Note: internal call where B doesn't have a NodePort or LoadBalancer nor an Ingress.

-- Meta
java
kubernetes
service

3 Answers

11/22/2019

Keep a URL of Service B in the environment variables of service A's kubernetes deployment file, When tasks in your service A are done, call that URL so that it takes the request to service B.

The is sync communication.

For Async communication you can use kafka, where you can just publish an event over kubernetes network and make the other microservices subscribe to that event on the need basis.

In the first case, you can do something like

- env
  name: service-B-url 
  value: http://service-name:port-number.cluster.svc.local/{uri-path}

Its better to use nslookup service name to get FQDN of the service to be called

-- Tushar Mahajan
Source: StackOverflow

11/22/2019

The right way to do this is to have a Service for B, and have A use the name of that service to access B. This way, B will be accessible to A regardless of its address.

-- Burak Serdar
Source: StackOverflow

11/22/2019

Use service name instead of the service IP. service-A can invoke service-B using a name service-B. Kubernetes will take care of routing service-B calls to appropriate POD and also load balance incase of multiple PODs. You dont worry about restarts or which POD actually services the request.

-- techuser soma
Source: StackOverflow