Main Menu

Search

KUBERNETES: Kubectl Port-Forward Command To Test Request Forwarding To Pods To Debug Network Issues

KUBERNETES: Kubectl Port-Forward Command To Test Request Forwarding To Pods To Debug Network Issues

kubectl port-forward command can be used to debug network issues connecting to pods.

Following is syntax for kubectl port forwarding

kubectl port-forward --address <local machine address> <target pod name> -n <namespace> <local machine port>:<target pod app listening port>

In above command, <local machine address> is the IP address of the local machine where you are running the kubectl test, <local machine port> is port you want to use on local machine where you are running kubectl test to start the port forwarding test. <target pod listening port> should be the pod port on which apps are listening. <target pod name> is the podman and <namespace> is the namespace under which pod is running in Kubernetes.

To demonstrate this test, lets take a example of starting nginx pod which has index HTML page with app listening on port 80. On one of the Kubernetes nodes lets forward the requests coming to port 8080 on the node to forward to port 80 on nginx pod. Below are steps for testing.

1) Create a nginx pods using below command.

kubectl create -f https://k8s.io/examples/application/deployment.yaml

You will have below nginx pods deployed.

NAME                                READY   STATUS    RESTARTS   AGE   IP            NODE            NOMINATED NODE   READINESS GATES
nginx-deployment-6595874d85-hhhrs   1/1     Running   0          9s    10.244.1.28   cne14-worker1   <none>           <none>
nginx-deployment-6595874d85-n6rww   1/1     Running   0          9s    10.244.1.29   cne14-worker1   <none>           <none>

2) On one of the Kubernetes nodes, do port forwarding to forward requests on port 8080 on that kubernetes nodes to port 80 on one of the nginx pods. For example below command.

kubectl port-forward --address 10.10.10.10 nginx-deployment-6595874d85-n6rww 8080:80

You will see console output as follows:

kubectl port-forward nginx-deployment-6595874d85-n6rww 8080:80
Forwarding from 10.10.10.10:8080 -> 80
Forwarding from [::1]:8080 -> 80
 
Please note that if you do not specify the  --address flag by default the port forwarding will listen on localhost 127.0.0.1 IP. You would output as follows:

kubectl port-forward nginx-deployment-6595874d85-n6rww 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80

3) From any other node which has access to the node where you are doing the kubectl port forward test, open SSH session to that node and run below command

wget -p 10.10.10.10:8080

You will see output as follows which indicates nginx index.html file is accessible on port 8080 (forwarded port)

 wget -p 10.10.10.10:8080
--2024-03-21 18:07:29--  http://127.0.0.1:8080/
Connecting to 10.10.10.10:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 612 [text/html]
Saving to: ‘10.10.10.10:8080/index.html’

100%[=========================================>] 612         --.-K/s   in 0s      

2024-03-21 18:07:29 (1.23 MB/s) - ‘10.10.10.10:8080/index.html’ saved [612/612]

FINISHED --2024-03-21 18:07:29--

At the same time of wget test when you see the window where you have kubectl port-forward running you will see snippets as follows which says that connection is being handled.

kubectl port-forward nginx-deployment-6595874d85-n6rww 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
Handling connection for 8080
 
At the same time of wget test when you see the window where you have kubectl port-forward running you will see snippets as follows which says that connection is being handled. Also as part of this test if you are doing kubectl port forwarding test to a HTTP webapp on the pod, you can even access the app from browser using the listen address and port you specified in kubectl port forward command. In case of this article example, it would be http://10.10.10.10:8080 


No comments:

Post a Comment