Zuul unable to route traffic to service on kubernetes

二次信任 提交于 2019-12-01 06:51:28

I think you've a mismatch between the host name being registered to Zuul and the one on which the service is reachable in k8s. Do 'kubectl get services' to find the names of your k8s services. Zuul will get the name that is registered to it from eureka (it seems you are registering names and not IPs). If you change the name of your Service in your k8s yaml for resource-service to be 'resource-service' then I'd expect it to work.

In your docker-compose I expect you have a section that defines the resource-service and it is presumably named resource-service. The equivalent for k8s is the name of the Service that matches to the Pods of that particular Deployment.

Edit: A spring boot app by default registers with eureka the Pod's hostname (if not using IP). So to override this it's necessary to set eureka.instance.hostname to match the Service name.

I faced the same problem when I went to deploy eureka server and zull proxy to the Google Kubernet Cluster, the solution is divided into two part :
first part: client side as mentioned we need to add : eureka: instance: hostname: resource-service .
second part: is Eureka server deployment, in kubernet we should use statfulset as a kind of deployment to guarantee stable network . you find below me eureka yml deployment :

              ---
apiVersion: v1
kind: Service
metadata:
  name: eureka-server
  labels:
    app: eureka-server
spec:
  ports:
  - port: 9000
    name: eureka-server
  clusterIP: None
  selector:
    app: eureka-server
---    
apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  name: eureka-server
spec:
  serviceName: "eureka-server"
  replicas: 1
  selector:
    matchLabels:
      app: eureka-server
  template:
    metadata:
      labels:
        app: eureka-server
    spec:
      containers:
      - name: eureka-server
        image: # eureka image
        ports:
        - containerPort: 9000
        env:
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
          # Due to camelcase issues with "defaultZone" and "preferIpAddress", _JAVA_OPTIONS is used here
        - name: _JAVA_OPTIONS
          value: -Deureka.instance.preferIpAddress=false -Deureka.client.serviceUrl.defaultZone=http://eureka-0.eureka-server:9000/eureka/
        - name: EUREKA_CLIENT_REGISTERWITHEUREKA
          value: "true"
        - name: EUREKA_CLIENT_FETCHREGISTRY
          value: "true"
        # The hostnames must match with the the eureka serviceUrls, otherwise the replicas are reported as unavailable in the eureka dashboard      
        - name: EUREKA_INSTANCE_HOSTNAME
          value: ${MY_POD_NAME}.eureka-server

The fact that Zuul registers with Eureka makes Zuul a client of Eureka.

server.port=8762
spring.application.name=zuul-server
eureka.instance.preferIpAddress=true
eureka.client.registerWithEureka=true
**eureka.client.fetchRegistry=true**
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}

fetchRegistry property enables Zuul (as a client of Eureka) to fetch the registry directory i.e. list of registered services in Eureka.

Once the Zuul server is up, you can see which routes are open using http://localhost:8762/routes url.

These routes were created by Zuul through fetched registry and relevant service ids. You can change the routes for every service id the way you want by configuring each service id in Zuul application properties i.e.

zuul.routes.student-service.path=/student-api/**
zuul.routes.student-service.serviceId=STUDENT-SERVICE

Also, the preferable way is to rely on IPs and not host names. i.e. use preferIpAddress property as suggested by others in other answers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!