Home > Software engineering >  Kubernetes error when forwarding a port - Connection refused
Kubernetes error when forwarding a port - Connection refused

Time:05-10

I'm trying to port-forward my kubernetes service (through minikube) using the following command:

kubectl port-forward svc/hapi-fhir-server 8080:8080 --address=0.0.0.0

But after trying to reach localhost:8080 I get the following error: "....... an error occurred forwarding 8080 -> 8080: error forwarding port 8080 to pod {PodID, uid ....:E connect(5, AF=2 127.0.0.1:8080, 16): Connection refused"

I checked which port the pod is listening to through the following command kubectl get pod hapi-fhir-server-666b846cbf-lhmr4 --template="{{(index (index .spec.containers 0).ports 0).containerPort}}" resulting in answer 8080

For if this helps, my service & deployment files (having removed unrelated lines)

apiVersion: apps/v1
kind: Deployment
metadata:
  ....
spec:
  replicas: 2
  selector:
    ....
  template:
      ....
    spec:
      containers:
        - image: .........
          name: hapi-fhir-server
          ports:
            - containerPort: 8080
          resources: {}
      restartPolicy: Always
status: {}
apiVersion: v1
kind: Service
metadata:
  ....
spec:
  type: NodePort
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 32069
  selector:
    app: hapi-fhir-server
status:
  loadBalancer: {}

The image being used is a HAPI FHIR server with the following configuration that runs on Apache Tomcat (server.xml):

<Connector port="${server.port}" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
               maxThreads="${server.tomcat.max-threads}"
               minSpareThreads="${server.tomcat.min-spare-threads}" /> 

server.port being 8080.

I can't seem to find an accurate answer to why this is happening, even after going through documentation or similar questions like: Kubernetes Port Forwarding - Connection refused.

Is there something I am missing, not doing correctly or that I am not thinking of? Note: I am relatively new to Kubernetes.

CodePudding user response:

Apparently there was no issue with the Kubernetes or server configuration but rather the Dockerfile that didn't expose port 8080, now it does through the following:

# Dockerfile
......
EXPOSE 8080
......

Thanks to IvanAracki that pointed this out to me through the comments.

  • Related