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:
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:
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:
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.
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.