Kubernetes Port Forwarding - Connection refused Kubernetes Port Forwarding - Connection refused kubernetes kubernetes

Kubernetes Port Forwarding - Connection refused


The reason the connection is refused is that there is no process listening on port 82. The dockerfile used to create the nginx image exposes port 80, and in your pod spec you have also exposed port 82. However, nginx is configured to listen on port 80.

What this means is your pod has two ports that have been exposed: 80 and 82. The nginx application, however, is actively listening on port 80 so only requests to port 80 work.

To make your setup work using port 82, you need to change the nginx config file so that it listens on port 82 instead of 80. You can either do this by creating your own docker image with the changes built into your image, or you can use a configMap to replace the default config file with the settings you want


As @Patric W said, the connection is refused because there is no process listening on port 82. That port hasn't been exposed.

Now, to get the port on which your pod is listening to, you can run the commands

NB: Be sure to change any value in <> with real values.

  • First, get the name of the pods in the specified namespace kubectl get po -n <namespace>
  • Now check the exposed port of the pod you'll like to forward.
kubectl get pod <pod-name> -n <namespace>  --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'

Now use the resulting exposed port above to run port-forward with the command

  • kubectl port-forward pod/<pod-name> <local-port>:<exposed-port>

where local-port is the port from which the container will be accessed from the browser ..localhost:<local-port> while the exposed-port is the port on which the container listens to. Usually defined with the EXPOSE command in the Dockerfile

Get more information here