Spring boot microservices are getting registered to Eureka with the pod name as the hostname in the Kubernetes cluster - docker desktop

Viewed 961

The microservices are getting registered to Eureka with the pod name as hostname, this causing UnknownHostException, when the Zull API gateway trying to forward the request to the service.

The complete setup working fine with the docker-compose, the issues are happening when I am trying to use the Kubernetes.

For example, the order microservice running with the pod name as "oc-order-6b64576f4b-7gp85" and the order microservice getting register to to Eureka with "oc-order-6b64576f4b-7gp85" as the hostname. Which is causing

java.net.UnknownHostException: oc-order-6b64576f4b-7gp85: Name does not resolve

I am running just one instance of each micro-services as a POC and one instance of the Eureka server. How to fix, how micro-service can register them self with the right hostname.

I want to use the Eureka as discovery service as it is well integrated with spring boot and I do not want to do code changes for Kubernetes deployment.

1 Answers

Add the below property to your spring properties file in each service project:

eureka.instance.hostName=${spring.application.name}

or (if you want a different name than the spring application name)

eureka.instance.hostName=<custom-host-name>

Spring boot will now register with the provided hostname in Eureka

Related