title: Connect Refused Kubernetes Service Discovery Using Spring Boot
body: <p>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.</p>
  
  <p>I have included:</p>
  
  <pre><code>        &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-starter-kubernetes&lt;/artifactId&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>And added the @EnableDiscoveryClient annotation.</p>
  
  <p>Below is the deployment.yml of the service that I am trying to call from the other service via name.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Below is the deployment.yml for the discovery service inside which I am trying to call the above service via name.</p>
  
  <pre><code>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></pre>
  
  <p>Code to call the service :</p>
  
  <pre><code>@GetMapping("/test")
      public String getHelloMessage() {
  
          RestTemplate restTemplate = new RestTemplate();
  
          String url = "http://sample-rest:8771/sample";
  
          ResponseEntity&lt;String&gt; result = restTemplate.getForEntity(url, String.class);
  
          return result.getBody();
      }
  </code></pre>
  
  <p>Everytime i am trying to call the endpoint below error comes on the logs and ui.</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>Output of kubectl describe svc sample-rest</p>
  
  <pre><code>Name:                     sample-rest
  Namespace:                default
  Labels:                   &lt;none&gt;
  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:                     &lt;unset&gt;  8771/TCP
  TargetPort:               8771/TCP
  NodePort:                 &lt;unset&gt;  31406/TCP
  Endpoints:                &lt;none&gt;
  Session Affinity:         None
  External Traffic Policy:  Cluster
  Events:                   &lt;none&gt;
  </code></pre>