<p>I have a microservices architecture with Docker containers and now want to move to deploy using Kubernetes. 
I am not able to run the application with Kubernetes. The gateway server is not connecting to service discovery and forwarding request.</p>

<p>As a demo application I have created 4 services:
- Zuul service
- Eureka service
- Contact service
- User service</p>

<p>Githube repo - <a href="https://github.com/dhananjay12/spring-microservice-k8s" rel="nofollow noreferrer">https://github.com/dhananjay12/spring-microservice-k8s</a></p>

<p>The application runs fine with docker-compose. I have created the images and pushed to Dockerhub. </p>

<p>K8s files - <a href="https://github.com/dhananjay12/spring-microservice-k8s/tree/master/k8s" rel="nofollow noreferrer">https://github.com/dhananjay12/spring-microservice-k8s/tree/master/k8s</a></p>

<p>To forward request I have setup Ingress-nginx.</p>

<p>I started the service by running follwoing:</p>

<pre><code>kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml
</code></pre>

<p>then</p>

<pre><code>minikube addons enable ingress
</code></pre>

<p>Ingress-service.yaml:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: zuul-cluster-ip-service
              servicePort: 8050
</code></pre>

<p>The request goes to zuul but following errors comes in logs:</p>

<pre><code>Caused by: java.lang.RuntimeException: java.net.UnknownHostException: contactus-5448dd4d9c-5gj5r
    at rx.exceptions.Exceptions.propagate(Exceptions.java:57) ~[rxjava-1.3.8.jar!/:1.3.8]
    at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:463) ~[rxjava-1.3.8.jar!/:1.3.8]
    at rx.observables.BlockingObservable.single(BlockingObservable.java:340) ~[rxjava-1.3.8.jar!/:1.3.8]
    at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:112) ~[ribbon-loadbalancer-2.2.5.jar!/:2.2.5]
    ... 127 common frames omitted
</code></pre>

<p>If I set eureka.instance.preferIpAddress=true I get the following exception:</p>

<pre><code>com.netflix.zuul.exception.ZuulException: Forwarding error
    at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.handleException(RibbonRoutingFilter.java:189) ~[spring-cloud-netflix-zuul-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
    at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.forward(RibbonRoutingFilter.java:164) ~[spring-cloud-netflix-zuul-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
    at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.run(RibbonRoutingFilter.java:112) ~[spring-cloud-netflix-zuul-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
    at com.netflix.zuul.ZuulFilter.runFilter(ZuulFilter.java:117) ~[zuul-core-1.3.1.jar!/:1.3.1]
    at com.netflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:193) ~[zuul-core-1.3.1.jar!/:1.3.1]
    at com.netflix.zuul.FilterProcessor.runFilters(FilterProcessor.java:157) ~[zuul-core-1.3.1.jar!/:1.3.1]
    at com.netflix.zuul.FilterProcessor.route(FilterProcessor.java:118) ~[zuul-core-1.3.1.jar!/:1.3.1]
    at com.netflix.zuul.ZuulRunner.route(ZuulRunner.java:96) ~[zuul-core-1.3.1.jar!/:1.3.1]
    at com.netflix.zuul.http.ZuulServlet.route(ZuulServlet.java:116) ~[zuul-core-1.3.1.jar!/:1.3.1]
    at com.netflix.zuul.http.ZuulServlet.service(ZuulServlet.java:81) ~[zuul-core-1.3.1.jar!/:1.3.1]
    at org.springframework.web.servlet.mvc.ServletWrappingController.handleRequestInternal(ServletWrappingController.java:165) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
    at org.springframework.cloud.netflix.zuul.web.ZuulController.handleRequest(ZuulController.java:44) [spring-cloud-netflix-zuul-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:52) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) [spring-webmvc-5.0.7.RELEASE.jar!/:5.0.7.RELEASE]
    at or
</code></pre>

<p>This is probably because services must have registered via pods name. How to handle such situations so that each downstream service registers via its cluster-IP?</p>

Dhananjay
<p>As @a.l. pointed out that the SPRING_EUREKA value should be the cluster-ip:</p>

<pre><code>value: http://eureka-cluster-ip-service:8761/eureka
</code></pre>

<p>Also, all service preferIpAddress should be true:</p>

<pre><code>eureka:
  instance:
    preferIpAddress: true
</code></pre>
