In this blog post, we will show you how to use port forwarding on a single node Kubernetes cluster and forward traffic to a pod.
Using port forwarding rather than creating service is probably a more straightforward and clean method for quick testing of applications.
Let’s get started with deploying a pod and using port-forwarding to access it using a browser. In this demo, I will be using the nginx official image to deploy a pod.
Deploy a Pod
Let’s go ahead and deploy a pod using the command below.
kubectl run nginx --image=nginx
Please wait a few seconds for the pod to come up, and now let’s port-forward traffic to the pod. In my case, I will open port 8080 from the cluster.
kubectl port-forward pod/nginx 8080:80
If I open a web browser and browse to HTTP://localhost:8080 (I’m using Docker Desktop with Kubernetes). If your Kubernetes host is not running on localhost, use the host’s IP address with port number 8080.
Once you close the connection from the console the port forwarding rule will be removed so there is nothing to delete after.