Kubectl is a command-line tool used for managing Kubernetes clusters. It provides a way to interact with the Kubernetes API, deploy applications, and monitor resources running on a Kubernetes cluster.
One of the valuable features of kubectl is the port forwarding functionality, which allows users to access services running inside a Kubernetes cluster from their local machine. In this article, we will explore kubectl port forwarding and provide examples of how to use it.
What is Kubectl Port Forwarding?
Kubectl port forwarding allows you to forward a port from a service running on a Kubernetes cluster to your local machine.
This means that you can access a service running on a Kubernetes cluster as if it were running on your local machine.
This is useful for testing and debugging applications running on a Kubernetes cluster and accessing internal resources that are not exposed to the public internet.
Kubectl port forwarding creates a secure tunnel between your local machine and a service running on a Kubernetes cluster. The tunnel allows traffic to flow between the two endpoints, enabling you to access the service from your local machine.
How to Use Kubectl Port Forwarding To use kubectl port forwarding, you need to have kubectl installed on your local machine and have access to a Kubernetes cluster. Once you have these requirements, you can use the following command to create a port forward:
kubectl port-forward <pod-name> <local-port>:<remote-port>
The above command will create a port forward from the remote port on the pod with the specified name to the local port on your machine. You can then access the service by localhost:<local-port>
.
If the service is backed by a web server/framework and listens to HTTP traffic, opening a web browser and navigating to http://localhost:<local-port>
would also work
Example 1: Simple Kubectl Port Forward from Service to Local
Suppose you have a Kubernetes cluster running a service named my-service
that listens on port 8080
.
You can use kubectl port forwarding to forward traffic from port 8080
on the my-service
to port 8000
on your local machine by running the following command:
kubectl port-forward my-service 8000:8080
After running this command, you can access the my-service
service on localhost:8000
If the service is backed by a web server/framework and supports HTTP traffic, opening a web browser and navigating to http://localhost:8000
would also work
Example 2: Forwarding a Port from POD to Local
Suppose you have a Kubernetes pod named my-pod
that runs a web server on port 80
. You can use kubectl port forwarding to forward traffic from port 80
on the my-pod
pod to port 8080
on your local machine by running the following command:
kubectl port-forward my-pod 8080:80
After running this command, you can access the web server running on the my-pod
pod by opening a web browser and navigating to http://localhost:8080
.
Example 3: Forwarding Multiple Ports at the same time to Local
Suppose you have a Kubernetes service named my-service
that listens on ports 8080
and 8443
.
You can use kubectl port forwarding to forward traffic from both ports on the my-service
pod to ports 8000
and 8443
on your local machine by running the following command:
kubectl port-forward my-service 8000:8080 8443:8443
After running this command, you can access the my-service
service by opening a web browser and navigating to http://localhost:8000
or https://localhost:8443
.
Conclusion Kubectl port forwarding is a powerful tool that allows you to access services and pods running on a Kubernetes cluster from your local machine. It is an essential feature for testing and debugging applications and for
Example 4: Forwarding with a Single Port
So far we have seen examples of forwarding traffic between pod/service and the local system with different ports for local and remote
But you do not have to mention the local port all the time, If you leave the local port unspecified, Kubetl will try to use the remote port number as the local port as well.
kubectl port-forward pod/mypod 5000 kubectl port-forward service/myservice 5000
In the preceding example snippet, we are using defining a single port number which would be considered for both remote and local port
In case the defined port is unavailable or occupied locally, kubectl would throw an error.
Example 5: Selecting the Container Name with Kubectl Port forward
When you are having multiple containers on the pod with patterns like sidecar, ambassador. You have to explicitly define which container to be chosen
so far we have just mentioned the pod name and the port number that we want to forward, but when you are having multiple containers in a pod
Kubectl would ask you to specify the container name inside the pod, to which the port forward should be applied.
Let's suppose that you have a pod with NodeJS + NGINX in side car pattern. How would you get the port forward to be done to either one of this container?
If you are familiar with kubectl already, where ever you need to specify the container name you must have used -c
. the same applies here
here is the kubectl command to forward a port by defining the container name
# To select the NGINX Container kubectl port-forward pod/pod-xyz -c nginx 8080:80 # To select the NodeJS Container kubectl port-forward pod/pod-xyz -c myapp 5000
Example 6: Kubectl Port forward with deployments directly
So far we have seen how to port forward from POD or service to Local, but you can do the same with deployments as well.
Yes, Instead of service or pod name you can use deployment name with the port-forward command and it would redirect that port of the randomly selected (singled) pod from the deployment
I have tried to explain this with the following illustration
here is the command to port forward with the deployment name
kubectl port-forward deployment/my-app 5000:5000
Conclusion
In this article, we have learnt how to use Kubernetes port forwarding with kubectl through examples.
We also learnt how to port forward by defining a single port instead of multiple. We have also seen how to port forward services, pods, and deployments to localhost.
Hope this helps.
Read our other Kubernetes articles here
If you have any questions let me know in the comments section
Cheers
Sarav AK
Follow me on Linkedin My Profile Follow DevopsJunction onFacebook orTwitter For more practical videos and tutorials. Subscribe to our channel
Signup for Exclusive "Subscriber-only" Content