Kubernetes Access Application via localhost with port-forward

1/24/2020

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

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

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?

    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

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

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   <none>        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:  <none>
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:    <none>
-- semural
kubectl
kubernetes

1 Answer

1/24/2020

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.

$ 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   <none>           <none>

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

Example :

$ kubectl port-forward nginx 31000:80
Forwarding from 127.0.0.1:31000 -> 80
Forwarding from [::1]:31000 -> 80

Curl from second terminal window curl the port forward you have.

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

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

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

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

$ kubectl port-forward nginx 31000:80
Forwarding from 127.0.0.1:31000 -> 80
Forwarding from [::1]:31000 -> 80
Handling connection for 31000

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

$ kubectl port-forward nginx 31000:8080
Forwarding from 127.0.0.1:31000 -> 8080
Forwarding from [::1]:31000 -> 8080

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.

$ curl -v localhost:31000
* Rebuilt URL to: localhost:31000/
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 31000 (#0)
> GET / HTTP/1.1
> Host: localhost:31000
> User-Agent: curl/7.47.0
> Accept: */*
>
* 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 -> 8080
Forwarding from [::1]:31000 -> 8080
Handling connection for 31000
E0124 11:35:53.390711   10791 portforward.go:400] an error occurred forwarding 31000 -> 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
-- DT.
Source: StackOverflow