Connect Refused Kubernetes Service Discovery Using Spring Boot

4/13/2020

I am trying to setup up a simple discovery using spring-boot-kubernetes but every time getting connection refused when trying to excess a service by service name.

I have included:

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

And added the @EnableDiscoveryClient annotation.

Below is the deployment.yml of the service that I am trying to call from the other service via name.

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

Below is the deployment.yml for the discovery service inside which I am trying to call the above service via name.

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

Code to call the service :

@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();
    }

Everytime i am trying to call the endpoint below error comes on the logs and ui.

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]

Output of 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>
-- Siddharth Chaurasia
docker
java
kubernetes
spring-boot

1 Answer

4/13/2020

The issue here is endpoints is empty for service sample-rest.You should check that the spec.selector field of your Service actually selects for metadata.labels values on your Pods. There is app: sample-rest as spec.selector but the pod label have app.kubernetes.io/name: sample-rest which is not matching.

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

Also you can try connecting to POD IP directly bypassing service to isolate if this is a problem with the service.

-- Arghya Sadhu
Source: StackOverflow