kubernetesquarkusk3sk3d

Connection refused calling kubernetes ClusterIP service


I am trying to deploy two apps on a local k3d k8s cluster. Service-A exposes a REST endpoint that is consumed by Service-B. This is the configuration:

Service-A

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/name: service-A
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
  name: service-A
spec:
  ports:
  - name: http
    port: 8554
    targetPort: 8554
  selector:
    app.kubernetes.io/name: service-A
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
    app.kubernetes.io/name: service-A
  name: service-A
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/version: 1.0.1-SNAPSHOT
      app.kubernetes.io/name: service-A
  template:
    metadata:
      labels:
        app.kubernetes.io/version: 1.0.1-SNAPSHOT
        app.kubernetes.io/name: service-A
    spec:
      containers:
      - env:
        - name: KUBERNETES_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        image: registry.localhost:12345/service-A
        imagePullPolicy: IfNotPresent
        name: service-A
        ports:
        - containerPort: 8554
          name: http
          protocol: TCP

Service-B

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/name: service-B
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
  name: service-B
spec:
  ports:
  - name: http
    port: 8550
    targetPort: 8550
  selector:
    app.kubernetes.io/name: service-B
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
    app.kubernetes.io/name: service-B
  name: service-B
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/version: 1.0.1-SNAPSHOT
      app.kubernetes.io/name: service-B
  template:
    metadata:
      labels:
        app.kubernetes.io/version: 1.0.1-SNAPSHOT
        app.kubernetes.io/name: service-B
    spec:
      containers:
      - env:
        - name: KUBERNETES_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: SERVICE_A_ADDR
          valueFrom:
            configMapKeyRef:
              key: service_a_addr
              name: app-configmap
        image: registry.localhost:12345/service-B-native
        imagePullPolicy: IfNotPresent
        name: service-B
        ports:
        - containerPort: 8550
          name: http
          protocol: TCP

configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-configmap
data:
  service_a_addr: service-A

As you can see, Service-B expects an env variable whose value is read from a config map that holds the Service-A name.

However, when Service-B tries to call the Service-A, I am getting a Connection refused exception:

Caused by: javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8554 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
    at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.invoke(ManualClosingApacheHttpClient43Engine.java:297)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:491)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:152)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:115)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
    at com.sun.proxy.$Proxy291.getLoginId(Unknown Source)
    at java.lang.reflect.Method.invoke(Method.java:566)
    at org.jboss.resteasy.microprofile.client.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:147)
    at com.sun.proxy.$Proxy292.getLoginId(Unknown Source)

Any idea on what might be happening? The Kubernetes YAML files have been automatically generated by the java framework Quarks.


Solution

  • Connect to localhost:8554 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)

    It was not configured to connect to "service-A", it was configured to connect to localhost? This error message holds the clue why your client is connecting to wrong address.