Share with friends
In the Kubernetes world, connecting different nodes and pods to internal and external sources is a day-to-day maintenance work we do. This is where port forwarding comes into the picture. There is a fundamental command to achieve this in Kubernetes by using the command kubectl port-forward.
This article will explore the ins and outs of kubectl port-forward - delving into its various use cases, command syntax, and best practices.
By the end, you'll have a solid understanding of how to harness this tool's full potential and make your Kubernetes deployment experience even more efficient.
So what is this command, where do you apply it, and how it works? We will talk about all this and more, but what is Port Forwarding all about?
What is Port Forwarding?
Port forwarding is a technique used in networking to allow all kinds of devices and network components to communicate with applications running on a local network.
It involves redirecting the traffic from one particular port on a router to another within the same network.
Why is Port Forwarding Needed?
In a local network, devices communicate with each other using private IP addresses that are not accessible from the internet.
However, when you want to access a service on a specific device from the internet, you need to forward the necessary ports.
Also Read: Kubectl Exec Tutorial
How does Port Forwarding Work?
Every device connected to a network has an IP address, which is an identifier assigned to it. The devices also have ports associated with them. They act like virtual channels that allow different applications to communicate with the device.
Port forwarding involves configuring the firewall to allow incoming traffic from and to ports configured to the application endpoints.
This tells the router that any requests it receives on a particular port should be directed to a device and application within a network.
Once these rules are set up, incoming traffic from the internet is directed to a particular application, allowing external services to access applications running on a network.
Now that we understand how the generic port forwarding concept in networking works, let's see how this concept applies in Kubernetes.
Also Read: What is Internal Developer Platform?
What is Kubectl Port Forwarding
“kubectl port-forward” is a powerful command-line tool that allows you to securely access resources running within a cluster. It creates a secure tunnel between your local machine and a pod running within a Kubernetes cluster.
Kubectl port-foward allows you to access services running within the cluster as if they were running on your local machine.
Whether you need to interact with a pod, service, or deployment, it simplifies the process by establishing a secure tunnel between your local machine and the desired resource.
This functionality allows you to work on troubleshooting network issues, and perform debugging without the need for complex networking setups.
How to Start Using Kubectl Port Forward
You need to have kubectl installed and configured to connect to your Kubernetes (K8s) cluster.
Once you are done installing kubectl, identify the pod(s) you want to forward ports from.
Use the kubectl get pods command to list the available pods in the cluster.
Now that you're ready, use the kubectl port-forward command followed by the name of the pod and the desired local and remote ports.
The syntax of kubectl port-foward command is as follows:
kubectl port-forward <pod-name> <local-port>:<remote-port>
<local-port>
is the port number on your local machine where you want to receive the forwarded traffic, and <remote-port>
is the port number on the pod that you want to access.
Once the port forwarding command is executed, a secure tunnel is created between your local machine and your pod.
You can now access the service running on the pod by sending requests to localhost on a local port.
Some use cases of port forwarding in Kubernetes are as follows:
- accessing a database running in a pod,
- connecting to a debugging or monitoring interface, or
- troubleshooting network-related issues within the cluster.
How does Kubectl Port-Forward Work?
When you run the port-forwarding command in kubectl, it sets up a tunnel between your local machine and the Kubernetes cluster.
It listens on the specified local port (for example, 8081) and forwards any traffic to the target port (for example, 80) on the specified service or pod.
Once this tunnel is created, you can access the Kubernetes service or pod using the local port you specified in the command (as specified earlier, http://localhost:8081).
Traffic sent to the local port is forwarded to the target port on Kubernetes and the application gets loaded on the 8081 port. This is also known as port mapping.
Also Read: A Complete Guide on Kubectl Config Set Context
Syntax of Kubectl Port-Forward
The syntax for the kubectl port-forward command in Kubernetes is as follows:
kubectl port-forward [OPTIONS] RESOURCE [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]
Here's a breakdown of the different components of the port forward command in Kuberenetes.
-
OPTIONS: Additional options you can specify for the port forwarding command.
-
RESOURCE: The Kubernetes resource name has to be provided. This is the cluster where you want to perform port forwarding. This can be a component of a Kubernetes cluster such as a pod, service, or any other resource that has a network interface.
-
[LOCAL_PORT:]REMOTE_PORT: This refers to the port mapping specification. You can specify one or more mappings separated by spaces. Each mapping consists of a local port and a remote port.
-
LOCAL_PORT: The local port on your machine that you want to use for port forwarding. If not specified, Kubernetes will automatically assign an available local port.
-
REMOTE_PORT: The port on the Kubernetes resource (pod or service) that you want to access.
Let's look at a few examples of the kubectl port-foward command.
The following command forwards traffic from local port 8080 to port 80 on the "my-pod" pod.
kubectl port-forward pod/my-pod 8080:80
The following port-forward command forwards traffic from local port 8888 to port 80 on the "my-service" service.
kubectl port-forward service/my-service 8888:80
Here's an example of how to forward deployment to multiple local ports.
kubectl port-forward deployment/my-deployment 8080:80 8443:443
This forwards traffic from local port 8080 to port 80 on the "my-deployment" deployment and from local port 8443 to port 443 on the same deployment.
If you are stuck somewhether, you can find more information about the kubectl port-forward command and its options by using the "--help" flag.
kubectl port-forward --help
Also Read: How to Work with DaemonSet in Kubernetes?
Basic Kubectl Port Forward Commands with Examples
Let's look at some basic operations using the kubectl port-foward command.
How to Port Forward a Pod to a Local Port?
Port forward a pod to a local port:
kubectl port-forward pod/my-pod 8081:80
This command forwards traffic from local port 8081 to port 80 on the "my-pod" pod. You can access the pod's service or application by navigating to http://localhost:8081 on your local machine.
How to Port Forward a Kubernetes Service to a Local Port?
Use the following command to port forward a service to a local port:
kubectl port-forward service/my-service 8888:80
This command forwards traffic from local port 8888 to port 80 on the "my-service" service. You can access the service by navigating to http://localhost:8888 on your local machine.
How to Port Forward Deployment to a Local Port?
The following command will port forward a deployment to a local port:
kubectl port-forward deployment/my-deployment 8081:80
This command forwards traffic from local port 8080 to port 80 on the pods managed by the "my-deployment" deployment.
Access the deployment's services or applications by navigating to http://localhost:8081 on your local machine.
How to Port Forward to a Specific Pod?
Use the below command to port forward to a specific pod within a deployment:
kubectl port-forward deployment/my-deployment 8081:80 --pod-selector=app=my-app
This command forwards traffic from local port 8081 to port 80 on a specific pod within the "my-deployment" deployment.
It uses the --pod-selector flag to specify a label selector uniquely identifying the desired pod.
How to Port Forward to a Specific Namespace?
Use the following command to port forward to a specific namespace in Kubernetes:
kubectl port-forward -n my-namespace pod/my-pod 8081:80
This command forwards traffic from local port 8081 to port 80 on the "my-pod" pod in the "my-namespace" namespace. It uses the -n or --namespace flag to specify the namespace.
Remember to replace my-pod, my-service, my-deployment, my-app, and my-namespace with the actual names of your pods, services, deployments, app labels, and namespaces.
Also Read: Kubernetes Liveness Probe Tutorial - Examples & Best Practices
How to Stop Kubectl Port Forward?
To stop the kubectl port-forward command, you can use one of the following methods.
Keyboard Interrupt
Press Ctrl + C in the terminal where the port forwarding command is running. This sends an interrupt signal to the command and terminates it.
It will stop the port forwarding and return you to the command prompt.
Kill the process
You can find the process ID (PID) of the kubectl port-forward process and use the kill command to terminate it.
First, identify the process ID by running the following command:
ps aux | grep kubectl port-forward
Look for the line that corresponds to the kubectl port-forward command. The second column will be the PID. Once you have the PID, use the following command to kill the process:
kill <PID>
PID is the process ID. Both methods will effectively stop the port forwarding and free up the local port that was being used for the forwarding.
What Port does Kubectl Use?
The kubectl command-line tool itself does not have a specific default port. It is used to interact with the Kubernetes API server, which typically runs on port 6443 for secure HTTPS communication.
When you execute kubectl commands, it communicates with the Kubernetes API server using the configured connection details, including the API server address and port.
By default, kubectl uses the HTTPS protocol to communicate with the API server on port 6443.
However, it's worth noting that the exact port can vary depending on your specific Kubernetes cluster configuration.
In some cases, the API server might be configured to use a different port for communication, and you would need to specify that port explicitly when setting up the kubectl context.
To check the current kubectl context and its associated API server details, you can use the following command:
kubectl config view --minify
Look for the server field in the output, which will indicate the API server address and port being used by kubectl for the current context.
Also Read: A Thorough Comparison of Consul, Istio, and Linkerd
What is kubefwd?
kubefwd (Kubernetes port forwarding) is a cmd utility that simplifies and automates the process of port forwarding to Kubernetes components such as Kubernetes services.
It allows you to access applications running in a Kubernetes cluster directly from your local machine, without the need to manually execute individual kubectl port-forward commands.
Kubefwd provides a more streamlined and efficient approach to port forwarding by handling multiple port-forwarding rules concurrently.
It establishes port forwarding for all services or pods with specific labels which helps you from additional configurations needed to set up individual port-forwarding commands for each resource.
Also Read: How to Install & Use Prometheus Operator in Kubernetes?
Features of kubefwd
Here are some key features of kubefwd.
Automatic port forwarding
Kubefwd automatically sets up port forwarding for all services or pods that match your specified criteria.
Concurrent forwarding
It handles multiple port-forwarding rules concurrently, allowing you to access multiple resources simultaneously.
Namespace or label-based selection
You can specify a namespace or use labels to define the set of services or pods to forward.
Streamlined command-line interface
Kubefwd provides a straightforward command-line interface for managing port forwarding, with options for starting, stopping, and listing active forwarding rules.
Also Read: How to Install and Setup Kubernetes Dashboard?
How to Use kubefwd in Kubernetes?
Kubefwd simplifies the process of accessing Kubernetes services and pods during local development, debugging, and testing scenarios.
It can be a useful tool to streamline your workflow when working with Kubernetes clusters.
Follow steps mentioned below to use kubefwd.
Step 1: Install kubefwd
Start by installing kubefwd on your local machine. You can find installation instructions in the official kubefwd repository.
Step 2: Start port forwarding
Once kubefwd is installed, you can use it to start port forwarding.
Here's an example command:
kubefwd svc -n my-namespace
This command starts port forwarding for all services in the "my-namespace" namespace. It automatically forwards all ports exposed by those services to the corresponding ports on your local machine.
Step 3: Access the forwarded service
With the port forwarding in place, you can now access the forwarded services as if they were running on your local machine.
For example, if there is a service named "my-service" listening on port 8080 in the my-namespace namespace, you can access it at http://localhost:8080.
Similarly, if there is a pod with the label "app=my-app" listening on port 8000, you can access it at http://localhost:8000.
Step 4: Stop port forwarding
To stop the port forwarding, you can simply interrupt the kubefwd process by pressing Ctrl + C in the terminal where it's running. This will stop the port forwarding and release the forwarded ports.
Port Fowarding in Kubernetes - Final Words
Kubectl port forwarding is the fundamental concept of Kubernetes networking. Once you master how it works, it will get easier to work with and deploy complex infrastructures on Kubernetes.
Share with friends