Kubernetes

Kubectl Port Forward

Forwarding a port using kubectl is relatively easy, although it only operates with individual pods but not with services. Port forwarding is a valuable tool for debugging different applications and deployments in the Kubernetes cluster. For illustration, if one of your pods is acting strangely, you will need to link to it directly. As this is a microservice setting, you can utilize port forwarding to communicate with a back-end service that would otherwise be hidden. The Kubelet delivers all information entered into the stream to the destination pod and port. When designing Kubernetes applications, it’s common to wish for immediate use of a service from the surrounding environment without exposing it via a load balancer or perhaps an ingress resource.

We can use kubectl to create a proxy that forwards all traffic from a local port to a port linked to our chosen Pod. The kubectl port-forward instruction can be utilized to accomplish this. The kubectl port-forward sends an appeal to the Kubernetes API. That implies the machine that runs it requires access to the API server, and all communication is tunneled through a single HTTP connection. By passing one (or more) local ports to a pod, we can access container content with this command. This command performs effectively when you are required to debug a malfunctioning pod. We are going to talk about a step-by-step method to check port forwarding using kubectl.

Pre-requisites

To use kubectl to forward a port, we must first verify the operating system. In our case, we are running Ubuntu 20.04 on a laptop. You can, however, look into alternative Linux distributions to see if they meet your needs. Minikube cluster is required for running Kubernetes services on Linux. You need to start up a minikube cluster on your system to implement this tutorial effectively.

Method to Forward Port Using Kubectl in Kubernetes

To forward port using Kubectl in Kubernetes, you must follow the prescribed steps explained in this tutorial.

To set up a minikube cluster on your Ubuntu 20.04 system, use the command line terminal. You can use one of two methods to open it:

  • Search for “Terminal” in the Ubuntu 20.04 system’s application search section.
  • Use the shortcut key “Ctrl+Alt+T“.

You can efficiently launch the terminal by selecting one of the methods described above. Once you are done with the opening of the terminal, you have to run the appended below command to start the minikube cluster:

$ minikube cluster

It is recommended that you do not exit the terminal until the minikube has started because the entire procedure will take a few minutes. The most significant step in this tutorial is to create a list of all the information about the pods in the system. The following listed command should run on the terminal for listing pods:

$ kubectl get pods

You may see the names, status, restarts, and ages of all pods operating in your system. You can individually list pods within a namespace to find the specific pod name by executing the following command:

$ kubectl -n <namespace> get pods

The <namespace> can be replaced with any of your desired namespaces. The output will display the list of pods included in the Kube-system namespace.

$ kubectl port-forward <podname> 8080:5762

The <podname> can be changed to your desired one. The output displays that the port is forwarding effectively.

Conclusion

For a Kubernetes pod, you’ve configured port forwarding. You can use this method to debug deployments by addressing ports that are not ordinarily exposed. Inside your container deployments, you can add this fundamental technique for activities like database, application, or network troubleshooting. I hope you can easily forward the port using kubectl.

About the author

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content