<p>I would like to access my application via localhost with <code>kubectl port-forward</code> command. But when I run  <code>kubectl port-forward road-dashboard-dev-5cdc465475-jwwgz 8082:8080</code> I received an below error.</p>

<pre><code>&gt; Forwarding from 127.0.0.1:8082 -&gt; 8080 Forwarding from [::1]:8082 -&gt;
&gt; 8080 Handling connection for 8082 Handling connection for 8082 E0124
&gt; 14:15:27.173395    4376 portforward.go:400] an error occurred
&gt; forwarding 8082 -&gt; 8080: error forwarding port 8080 to pod
&gt; 09a76f6936b313e438bbf5a84bd886b3b3db8f499b5081b66cddc390021556d5, uid
&gt; : exit status 1: 2020/01/24 11:15:27 socat[9064] E connect(6, AF=2
&gt; 127.0.0.1:8080, 16): Connection refused
</code></pre>

<p>I also try to connect pod in cluster via exec -it but it did not work as well.What might be the missing point that I ignore?</p>

<pre><code>    node@road-dashboard-dev-5cdc465475-jwwgz:/usr/src/app$ curl -v localhost:8080
* Rebuilt URL to: localhost:8080/
*   Trying ::1...
* TCP_NODELAY set
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* connect to 127.0.0.1 port 8080 failed: Connection refused
* Failed to connect to localhost port 8080: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 8080: Connection refused
</code></pre>

<p>kubectl get all out is below.I am sure that Container port value is set 8080.</p>

<pre><code>NAME                                      READY   STATUS    RESTARTS   AGE
pod/road-dashboard-dev-5cdc465475-jwwgz   1/1     Running   0          34m
pod/road-dashboard-dev-5cdc465475-rdk7g   1/1     Running   0          34m

NAME                         TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/road-dashboard-dev   NodePort   10.254.61.225   &lt;none&gt;        80:41599/TCP   18h

NAME                                 READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/road-dashboard-dev   2/2     2            2           18h

NAME                                            DESIRED   CURRENT   READY   AGE
replicaset.apps/road-dashboard-dev-5cdc465475   2         2         2       34m


Name:               road-dashboard-dev-5cdc465475-jwwgz
Namespace:          dev
Priority:           0
PriorityClassName:  &lt;none&gt;
Node:               c123
Containers:
  road-dashboard:
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 24 Jan 2020 13:42:39 +0300
    Ready:          True
    Restart Count:  0
    Environment:    &lt;none&gt;
</code></pre>

semural
<p>To debug your issue you should let the port forward command tuning in foreground and curl from a second terminal and see what output you get on the port-forward prompt.</p>

<pre><code>$ kubectl get all -o wide
NAME           READY   STATUS    RESTARTS   AGE    IP            NODE         NOMINATED NODE   READINESS GATES
pod/nginx      1/1     Running   2          112m   10.244.3.43   k8s-node-3   &lt;none&gt;           &lt;none&gt;

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/kubernetes   ClusterIP   10.96.0.1       &lt;none&gt;        443/TCP        11d   &lt;none&gt;
service/nginx        NodePort    10.96.130.207   &lt;none&gt;        80:31316/TCP   20m   run=nginx
</code></pre>

<p>Example :</p>

<pre><code>$ kubectl port-forward nginx 31000:80
Forwarding from 127.0.0.1:31000 -&gt; 80
Forwarding from [::1]:31000 -&gt; 80
</code></pre>

<p>Curl from second terminal window curl the port forward  you have.</p>

<pre><code>$ curl localhost:31000
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to nginx!&lt;/title&gt;
&lt;style&gt;
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Welcome to nginx!&lt;/h1&gt;
&lt;p&gt;If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.&lt;/p&gt;

&lt;p&gt;For online documentation and support please refer to
&lt;a href="http://nginx.org/"&gt;nginx.org&lt;/a&gt;.&lt;br/&gt;
Commercial support is available at
&lt;a href="http://nginx.com/"&gt;nginx.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you for using nginx.&lt;/em&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>You should see that on first terminal the portforward promt list that it is handeling a connection like below note new line <code>Handling connection for 31000</code></p>

<pre><code>$ kubectl port-forward nginx 31000:80
Forwarding from 127.0.0.1:31000 -&gt; 80
Forwarding from [::1]:31000 -&gt; 80
Handling connection for 31000
</code></pre>

<p>So if like i have wrong port forwarding as below (note i have mode the port 8080 for nginx container exposing port 80)</p>

<pre><code>$ kubectl port-forward nginx 31000:8080
Forwarding from 127.0.0.1:31000 -&gt; 8080
Forwarding from [::1]:31000 -&gt; 8080
</code></pre>

<p>The curl will result clear error on port forward prompt indicating the connection was refused from container when getting to port 8080 as its not correct. and we get a empty reply back.</p>

<pre><code>$ curl -v localhost:31000
* Rebuilt URL to: localhost:31000/
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 31000 (#0)
&gt; GET / HTTP/1.1
&gt; Host: localhost:31000
&gt; User-Agent: curl/7.47.0
&gt; Accept: */*
&gt;
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server

$ kubectl port-forward nginx 31000:8080
Forwarding from 127.0.0.1:31000 -&gt; 8080
Forwarding from [::1]:31000 -&gt; 8080
Handling connection for 31000
E0124 11:35:53.390711   10791 portforward.go:400] an error occurred forwarding 31000 -&gt; 8080: error forwarding port 8080 to pod 88e4de4aba522b0beff95c3b632eca654a5c34b0216320a29247bb8574ef0f6b, uid : exit status 1: 2020/01/24 11:35:57 socat[15334] E connect(5, AF=2 127.0.0.1:8080, 16): Connection refused
</code></pre>
