Kubernetes pod gives "connection refused" when trying to connect via ClusterIp

5/20/2020

I'm trying to use Spring Gateway to route requests to some Spring Boot pods exposing REST APIs.

I have a search service deployed like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: search
  name: search
spec:
  replicas: 1
  selector:
    matchLabels:
      app: search
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: search
    spec:
      containers:
      - image: <ommited>
        name: search-service
        imagePullPolicy: Always
        resources: {}
      imagePullSecrets:
        - name: <ommited>
status: {}
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: search
  name: search
spec:
  ports:
  - name: stage
    port: 8080
    protocol: TCP
  selector:
    app: search
  type: ClusterIP

It deploys fine and is accessible if I use an ngnix ingress deployment. I want to use Spring Gateway as an entry point however so I deploy it in another pod like so:

<ommiting deployment>
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: gateway
  name: gateway
spec:
  ports:
  - name: stage
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: gateway
  type: LoadBalancer

With a defined route:

.route("search", r -> r.path("/search")
                        .uri("http://search:8080/search"))

It also deploys fine and is accessible externally.

When the gateway pod tries to route the request to the search pod however it gets an explicit "connection refused".

{
  "timestamp": "...",
  "path": "/search",
  "status": 500,
  "error": "Internal Server Error",
  "message": "finishConnect(..) failed: Connection refused: search/10.100.130.149:8080",
  "requestId": "a3af8165-1"
}

Hostname is resolved properly.

Also tried with connecting from gateway to search nc:

kubectl exec -it gateway-7798cb658-ffzvb -- nc -z search 8080
command terminated with exit code 1

Again host is properly resolved, so expect I have misconfigured ports somewhere. I have verified that the search Spring Boot app listens on 8080 and 8080 is exposed via the docker container and the search pod.

Any help appreciated, thanks!

-- Simeon
kubernetes
spring
spring-boot
spring-cloud

1 Answer

5/20/2020

Check the description of the search service using command kubectl describe svc search and verify that port is matching with the port (containerPort) the pod is listening on. Also you can define a targetPort in the service to explicitly point to the port(containerPort) the container is listening on.

-- Arghya Sadhu
Source: StackOverflow