Kubernetes

Kubectl Port Forwarding: Easy Kubernetes Ports Access

Dive deep into Kubernetes port access with kubectl port forward command, your key to accessing, debugging, and developing cluster services.

The kubectl port forward command is a great command that admins can use to test things with Kubernetes clusters and make sure network traffic is reaching the app along with testing to make sure the app is working. Let’s see how you can use the kubectl port forward command.

What is the Kubectl command?

The kubectl command is arguably the most used command in Kubernetes. It allows Kubernetes admins to have a swiss army knife type tool that can do or test just about anything. It is the go to tool for carrying out most day to day operations for a K8s admin checking and testing things.

The kubectl tool connects to and interacts with the Kubernetes API, which is the primary interface forย managing all aspects of the Kubernetesย cluster. Then, kubectl sends requests to the API Server, which triggers the API to perform the request operation or get the dataย requested from the kubectl command.

Examples using the kubectl command line tool

A typical kubectl command follows the syntax:

kubectl [command] [TYPE] [NAME] [flags]

Here, the command refers to the task or command you wish to do like get, describe, delete, TYPE is the resource type, NAME is the resource’s name, and flags are optional extra parameters you can define.

If you want to get information about your Kubernetes cluster nodes you can use the following command that is well known:

kubectl get nodes
Kubectl get nodes command
Kubectl get nodes command

Creating an NGINX pod kubectl command:

kubectl create depoy nginx --image nginx
Deploy NGINX using kubectl
Deploy NGINX using kubectl

You can check the deployment of the pod below:

kubectl get deployment -A
Looking at the deployment using kubectl
Looking at the deployment using kubectl

For example, if you want to get details about a pod named my-pod, you would use the following command:

kubectl get pod my-pod
Kubectl get pod in a deployment
Kubectl get pod in a deployment

How it works

The Kubernetes API server creates a single HTTP connection that forwards traffic from a specific local port to a pod within the cluster via the cluster IP service. So, you configure an IP address and port number to use to be forwarded into the K8s cluster.

It eliminates the need for the service to be able to be accessed from the public internet, which is an advantage in certain cases like debugging applications or accessing services not publicly available.

Kubectl Port Forward Command Syntax

Mastering the kubectl port forward command requires understanding its basic syntax:

kubectl port forward TYPE/NAME [LOCAL_PORT:]REMOTE_PORT

The TYPE/NAME in the above command signifies the target resource name, be it a pod, service, or deployment name. LOCAL_PORT is the port on your local machine, whereas REMOTE_PORT is the port on the target pod or service in the Kubernetes cluster.

Multiple ports can be forwarded in Kubernetes by specifying different ports separated by a space within the command. Here’s an illustrative command: kubectl port forward deployment/myapp 5000 6000. This command forwards local ports 5000 and 6000 to corresponding ports on the pods created by the myapp deployment.

Real-World Applications

As an example, there’s a web server operating within a pod in the Kubernetes cluster. Running the kubectl port forward command and specifying the pod name and the necessary ports enables you to forward traffic from your local machine to this web server.

As such, you can access the server without exposing it to the public internet โ€“ a feature for services needing protection from outside access.

Another practical application of port forwarding is when you need to debug applications within the Kubernetes cluster. The command facilitates direct interaction with the running pods, making it an efficient tool for understanding the application’s behavior without exposing it to the outside world.

Kubernetes API Serverโ€™s Role

The Kubernetes API server plays a pivotal role in port forwarding. By establishing a single HTTP connection, it enables traffic from a specific port on your local machine to be forwarded to a designated port within a pod in the Kubernetes cluster. This mechanism provides a secure, controlled, and straightforward way to access internal resources on specific cluster ports.

Services and Deployments

You can also use and run the kubectl port forward command to access Kubernetes services and deployments. By using the kubectl port forward svc/[service-name] or kubectl port forward deploy/[deployment-name] command, admins and others/developers can access services or deployments.

For example, to forward traffic to a service named my-service that’s listening on port 8080, the command would be the following:

kubectl port forward svc/my-service 8080:80

For a deployment named my-deployment running on the same port, the command would be the following:

kubectl port forward deploy/my-deployment 8080:80
Kubectl port forwarding to a deployment
Kubectl port forwarding to a deployment
Curling the NGINX web page that is port forwarded
Curling the NGINX web page that is port forwarded

Secure Tunnel

The kubectl port forward command uses tunneling between yourself and the Kubernetes cluster. It helps to make sure that your connection is protected.

All the HTTP traffic that passes through the tunnel is encrypted and this is a priority to make sure that data is not exposed, even when it is copying across the network.

Wrapping up

kubectl port forward is a tool that can be used for many different purposes, but debugging or troubleshooting/testing is one of the main purposes of this tool. You can easily forward in traffic to your pod to test the application before configuring the permanent load balancer IP, etc.

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.