Zuul:Kubernetes中的UnknownHostException

时间:2019-05-28 04:45:53

标签: java spring kubernetes netflix-zuul

我在kubernete和docker中使用微服务,当zuul(网关)将请求数据转发到服务时,我收到UnknownHostException 我无法按Pod名称ping服务容器(但是当我使用docker swarm而不是kubernetes时,我可以正常按主机名ping)

这是我的服务Yaml文件

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: merchantservice
  labels:
    run: merchantservice
spec:
  template:
    metadata:
      labels:
        name: merchantservice
    spec:
      containers:
      - name: merchantservice
        image: merchantservice:latest
        ports:
        - containerPort: 8001
        env:
          - name: EUREKA_SERVER
            value: "eureka1"
          - name: EUREKA_SERVER2
            value: "eureka2"
          - name: CONFIG_SERVER
            value: "configserver"
---
apiVersion: v1
kind: Service
metadata:
  name: merchantservice
spec:
  selector:
    name: merchantservice
  ports:
    - port: 8001
      targetPort: 8001
  type: LoadBalancer

这是错误输出

2019-05-28 04:29:53.443  WARN 1 --- [nio-8444-exec-6] o.s.c.n.z.filters.post.SendErrorFilter   : Error during filtering

com.netflix.zuul.exception.ZuulException: Forwarding error
...
Caused by: com.netflix.client.ClientException: null
...
Caused by: java.lang.RuntimeException: java.net.UnknownHostException: merchantservice-79cc77d9cc-224mf: Try again
    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.3.0.jar!/:2.3.0]
    ... 158 common frames omitted
Caused by: java.net.UnknownHostException: merchantservice-79cc77d9cc-224mf: Try again
...

4 个答案:

答案 0 :(得分:1)

确保已在必须通过zuul路由的微服务的application.properties文件中添加了eureka.instance.preferIpAddress = true。

参考:https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html#spring-cloud-eureka-server-prefer-ip-address

答案 1 :(得分:0)

我在评论中发布@Matthew L Daniel的答案,以提高可见度:

  

kube-dns不会为Pod创建DNS记录,因为它们是   短暂的如果要寻址窗格,请使用其IP或使用服务   –马修·L·丹尼尔

答案 2 :(得分:0)

在openshift中,您不能通过pod-id来访问Pod,但是可以通过本地DNS和服务来访问它。 ..svc.cluster.local: 我认为您的情况也一样。请在下面尝试

CREATE OR REPLACE FUNCTION myschema.f_m_user
-- Returns the oracle error message
(
par_login_id        IN profile.login_id%TYPE,
par_old_user_id     IN profile.user_id%TYPE,
par_new_user_id     IN profile.user_id%TYPE,
par_user_profile_id     IN profile.mod_profile_id%TYPE
)

RETURN VARCHAR2

IS

BEGIN … more code not shown

答案 3 :(得分:0)

在kubernetes的情况下,容器ID或(pod id)似乎不是有效的DNS名称 但是,如果您使用docker-compose,则它是有效的。 我认为首选方法是将此设置添加到将在Eureka中注册的所有服务中 eureka.instance.prefer-ip-address = true