Running Spring cloud (Zuul, Eureka and 2 rest backend service) with Kubernetes

12/25/2018

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.

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

Githube repo - https://github.com/dhananjay12/spring-microservice-k8s

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

K8s files - https://github.com/dhananjay12/spring-microservice-k8s/tree/master/k8s

To forward request I have setup Ingress-nginx.

I started the service by running follwoing:

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

then

minikube addons enable ingress

Ingress-service.yaml:

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

The request goes to zuul but following errors comes in logs:

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

If I set eureka.instance.preferIpAddress=true I get the following exception:

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

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?

-- Dhananjay
docker
kubernetes
netflix-eureka
nginx-ingress
spring-cloud

1 Answer

12/27/2018

As @a.l. pointed out that the SPRING_EUREKA value should be the cluster-ip:

value: http://eureka-cluster-ip-service:8761/eureka

Also, all service preferIpAddress should be true:

eureka:
  instance:
    preferIpAddress: true
-- Dhananjay
Source: StackOverflow