43

I am getting the following error when forwarding port. Can anyone help?

mjafary$ sudo kubectl port-forward sa-frontend 88:82

Forwarding from 127.0.0.1:88 -> 82
Forwarding from [::1]:88 -> 82

The error log :

Handling connection for 88
Handling connection for 88
E1214 01:25:48.704335   51463 portforward.go:331] an error occurred forwarding 88 -> 82: error forwarding port 82 to pod a017a46573bbc065902b600f0767d3b366c5dcfe6782c3c31d2652b4c2b76941, uid : exit status 1: 2018/12/14 08:25:48 socat[19382] E connect(5, AF=2 127.0.0.1:82, 16): Connection refused

Here is the description of the pod. My expectation is that when i hit localhost:88 in the browser the request should forward to the jafary/sentiment-analysis-frontend container and the application page should load

mjafary$ kubectl describe pods sa-frontend

Name:         sa-frontend
Namespace:    default
Node:         minikube/192.168.64.2
Start Time:   Fri, 14 Dec 2018 00:51:28 -0700
Labels:       app=sa-frontend
Annotations:  <none>
Status:       Running
IP:           172.17.0.23
Containers:
  sa-frontend:
    Container ID: docker://a87e614545e617be104061e88493b337d71d07109b0244b2b40002b2f5230967
    Image:          jafary/sentiment-analysis-frontend
    Image ID:       docker-pullable://jafary/sentiment-analysis-frontend@sha256:5ac784b51eb5507e88d8e2c11e5e064060871464e2c6d467c5b61692577aeeb1
    Port:           82/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 14 Dec 2018 00:51:30 -0700
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
  /var/run/secrets/kubernetes.io/serviceaccount from default-token-mc5cn (ro)
Conditions:
  Type           Status
  Initialized    True 
  Ready          True 
  PodScheduled   True 
Volumes:
  default-token-mc5cn:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-mc5cn
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
             node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>
9
  • Hi Jaf, welcome to SO. You will need to post the PodSpec for the Pod you're trying to connect to, as we can't guess whether it actually has something listening on port :82. Separately, can you connect to that Pod from within the cluster (not using port-forward)?
    – mdaniel
    Dec 16, 2018 at 6:51
  • I have added the detail to the description as requested. I am new to Kubernetes and trying to learn. Can you please guide how I can connect to the Pod from within the cluster without using port-forward?
    – Jaf
    Dec 16, 2018 at 21:28
  • 1
    There are two ways, one more helpful than another: kubectl -n default exec -it sa-frontend curl -v localhost:82 and then that same trick from another Pod, either one that is already running or one created expressly for the purpose of debugging. I would expect you can also curl from within minikube's VM: minikube ssh curl -v 172.17.0.23:82 (I don't use minikube, so you may have to separate those into two commands, ssh, then curl)
    – mdaniel
    Dec 16, 2018 at 23:55
  • 1
    so, based just on your output above, I don't see where you are configuring nginx to listen on :82, since the container says it is listening on :80: docker run --rm -it --entrypoint=/bin/grep 'jafary/sentiment-analysis-frontend@sha256:5ac784b51eb5507e88d8e2c11e5e064060871464e2c6d467c5b61692577aeeb1' listen /etc/nginx/conf.d/default.conf listen 80;
    – mdaniel
    Dec 17, 2018 at 0:03
  • 1
    in kubernetes, the ports: array is designed to represent the -p flag you used in docker, except the "left hand side" of the : is a name because in kubernetes very few things should be talking directly to a Pod, rather they should speak to Service whose job it is to map between the long-term contract that service has and the current Pod that fulfills it. So in your case, {"containers":[{"ports":[{"name": "http", "containerPort": 80}]}]} (to use JSON because yaml in comments is terrible) and then the Service would be {"ports":[{"targetPort": "http", "port": 80}]}
    – mdaniel
    Dec 17, 2018 at 1:56

3 Answers 3

19

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

13

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

0
1

As Patrick pointed out correctly. I had this same issue which plagued me for 2 days. So steps would be:

  1. Ensure your Dockerfile is using your preferred port (EXPOSE 5000)
  2. In your pod.yml file ensure containerPort is 5000 (containerPort: 5000)
  3. Apply the kubectl command to reflect the above:

kubectl port-forward pod/my-name-of-pod 8080:5000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.