【问题标题】:Connect Refused Kubernetes Service Discovery Using Spring Boot使用 Spring Boot 连接被拒绝的 Kubernetes 服务发现
【发布时间】:2020-07-26 06:13:26
【问题描述】:

我正在尝试使用 spring-boot-kubernetes 设置一个简单的发现,但每次尝试通过服务名称超出服务时都会拒绝连接。

我已包含:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes</artifactId>
        </dependency>

并添加了@EnableDiscoveryClient 注解。

下面是我试图通过名称从其他服务调用的服务的 deployment.yml。

kind: Service
apiVersion: v1
metadata:
  name: sample-rest
spec:
  selector:
    app: sample-rest
  ports:
    - protocol: TCP
      port: 8771
      targetPort: 8771
  type: NodePort

---

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: sample-rest
  name: sample-rest
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: sample-rest
  template:
    metadata:
      labels:
        app.kubernetes.io/name: sample-rest
    spec:
      containers:
       - name: sample-rest
         image: kryptonian/sample-rest:1.0
         ports:
         - containerPort: 8771

下面是发现服务的deployment.yml,我试图在其中通过名称调用上述服务。

kind: Service
apiVersion: v1
metadata:
  name: sample-discovery
spec:
  selector:
    app: sample-discovery
  ports:
    - protocol: TCP
      port: 8772
      targetPort: 8772
  type: NodePort

---

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: sample-discovery
  name: sample-discovery
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: sample-discovery
  template:
    metadata:
      labels:
        app.kubernetes.io/name: sample-discovery
    spec:
      containers:
       - name: sample-discovery
         image: kryptonian/discovery-sample:1.0
         ports:
         - containerPort: 8772

调用服务的代码:

@GetMapping("/test")
    public String getHelloMessage() {

        RestTemplate restTemplate = new RestTemplate();

        String url = "http://sample-rest:8771/sample";

        ResponseEntity<String> result = restTemplate.getForEntity(url, String.class);

        return result.getBody();
    }

每次我尝试调用以下端点时,日志和用户界面都会出现错误。

2020-04-13 16:08:04.187 ERROR 1 --- [nio-8772-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://sample-rest:8771/sample": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)] with root cause

java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]

kubectl describe svc sample-rest 的输出

Name:                     sample-rest
Namespace:                default
Labels:                   <none>
Annotations:              kubectl.kubernetes.io/last-applied-configuration:
                            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"sample-rest","namespace":"default"},"spec":{"ports":[{"port":8771...
Selector:                 app=sample-rest
Type:                     NodePort
IP:                       10.35.250.117
Port:                     <unset>  8771/TCP
TargetPort:               8771/TCP
NodePort:                 <unset>  31406/TCP
Endpoints:                <none>
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

【问题讨论】:

  • 添加 kubectl describe svc sample-rest 的输出

标签: java spring-boot docker kubernetes


【解决方案1】:

这里的问题是服务sample-rest 的端点为空。您应该检查您的服务的spec.selector 字段是否实际选择了您的Pod 上的metadata.labels 值。 app: sample-restspec.selector 一样,但 pod 标签有 app.kubernetes.io/name: sample-rest 不匹配。

https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/#does-the-service-have-any-endpoints

如果这是服务的问题,您也可以尝试直接绕过服务连接到 POD IP 以隔离。

【讨论】:

  • 如何获得?如果我试图将 app.kubernetes.io/name 更改为 app 它不会让我这样做,反之亦然
  • apiVersion:apps/v1 kind:部署元数据:标签:app.kubernetes.io/name:sample-rest 名称:sample-rest 规范:replicas:1 选择器:matchLabels:app:sample-rest模板:元数据:标签:app:sample-rest 无效值:v1.LabelSelector{MatchLabels:map[string]string{"app":"sample-rest"},MatchExpressions:[]v1.LabelSelectorRequirement(nil)}:字段是不可变的
  • 你可以有多个标签。添加 app: sample-rest 标签以及在 spec.template 部分
  • 非常感谢 Arghya,工作完美。 :)
猜你喜欢
  • 2019-06-01
  • 1970-01-01
  • 2021-09-13
  • 1970-01-01
  • 2016-05-02
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
相关资源
最近更新 更多