Kubernetes

Kubernetes Port Forwarding

In this article, we will take a look at the basics of port forwarding and how it can be used in conjunction with Kubernetes. You can learn all about port forwarding and how Kubernetes uses it. Here, you can see all the information as well as screenshots of commands that have been run. Let’s start with the definition of port forwarding first.

What Is Port Forwarding?

Port forwarding is defined as a networking technique that allows the computers to communicate with each other, but these computers should be on the same network. Port forwarding is achieved by creating an address mapping between two ports on two different nodes in the same network. This allows a computer to send the data from one node to another while still keeping the traffic secure. To understand the port forwarding better, let’s consider an example. If you want to access your home web server from outside your local network, you could use the port forwarding so that the traffic could be routed through your router and out onto the internet. Kubernetes utilizes the port forwarding as part of its container orchestration system for services such as HTTP/HTTPS and SSH connections.

What Is Port Forwarding in the Context of Kubernetes?

Kubernetes port forwarding is a way to access the internal cluster resources from outside the cluster. Users can map an external port to a port that is used by a resource such as a pod, deployment, replica set, or service. In this way, the resource becomes accessible from within the local network. Kubectl features a built-in port forwarding functionality.

Prerequisites:

Kubernetes port forwarding is an essential part of any Kubernetes deployment. It allows the users to access the services that run inside a Kubernetes cluster from outside the Kubernetes environment. This process requires a few prerequisites to be successful.

Before attempting the Kubernetes port forwarding, users must first have a working knowledge of how the containers work and how they interact with the other components of a system. They should also have an experience on how to configure the networking on Linux-based operating systems, as well as understand the various networking protocols and ports that are used for communication between different components. Additionally, understanding the differences between service types available within the Kubernetes environment such as Load Balancers, Ingress Controllers, and NodePort services is beneficial when setting up the port forwarding.

Steps to Setup the Port Forwarding

Port forwarding is an important aspect of Kubernetes, allowing the users to securely send the data from one computer to another. Setting up the port forwarding can seem complicated, but several easy steps make it straightforward. This section discusses the necessary steps to set up the port forwarding in Kubernetes. You can get started right away.

Step 1: Start the Minikube

Our first step is about starting the Kubernetes. We run the following command to start the Kubernetes dashboard:

> minikube start

 

After being executed, this command demonstrates that the Kubernetes application is running in a local container with a minikube cluster. You need to take this necessary step first.

Step 2: Create a Deployment

In this next step, we create a Deployment that runs the MongoDB. This is done with the following command:

> kubectl apply -f https://k8s.io/examples/application/mongodb/mongo-deployment.yaml

 

You can see the output right below the previously executed command.

Step 3: Check the Pod’s Status

Now, we check the pod status with the attached command here:

> kubectl get pods

 

In the output, you can see the created pod in the fourth line with the name, “mongo-78c7d87875-sl5g6”.

Step 4: See the Deployment Status

In this step, we view the status of the deployment with the following given command:

> kubectl get deployment

 

The output shows the successful creation of the deployment.

Step 5: View the Status of the ReplicaSet

A ReplicaSet is automatically managed by the Deployment. Use the following command to view the ReplicaSet status:

> kubectl get replicaset

 

The output indicates the successful creation of the replicaSet.

Step 6: Create a Service for MongoDB

Here, we build a network-accessible service for MongoDB. This is done with the following given command:

> kubectl apply -f https://krs.io/examples/application/mongodb/mongo-service.yaml

 

A successful command’s output confirms that the Service is established.

Step 7: Check the Service

In this step, we check the service which is created with the following given command:

> kubectl get service mongo

 

The output shows that the service is created successfully.

Step 8: Check If the Server Is Active

With the following command, we can confirm that the MongoDB server is active in the Pod and is listening on port 27017:

> kubectl get pod mongo-75f59d57f4-4nd6q --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'

 

The TCP port that is assigned to MongoDB on the internet is 27017. It can be seen in the  provided output.

Step 9: Forwarding a Local Port

In this step, let’s see how you can forward a local port to a port on the Pod. To achieve that, the “kubectl port-forward” command is used.

> kubectl port-forward mongo-78c7d87875-sl5g6 28015:27017

 

Verifying the Setup of Port Forwarding

Verifying the setup of Port Forwarding is an important part of the Kubernetes installation process. This article explains how to properly set up and validate the port forwarding for a project that runs on Kubernetes, ensuring that all services are accessible in an expected manner.

Port forwarding involves setting up the network communication paths between two groups of hosts: those inside and outside a private network. By configuring the port forwarding, you can access the services which are located within a private network from outside it, as well as allow the users to access the services which are hosted in your public cloud environment from anywhere. This can be done by mapping the specific ports with specific IP addresses and protocols, allowing the traffic to pass through securely. With Kubernetes, you must ensure that the proper ports are configured correctly before attempting to verify their functionality.

What Are the Benefits of Port Forwarding?

Port forwarding is an important step in using Kubernetes to manage the containerized applications. By understanding and implementing the port forwarding, the users can take advantage of the many benefits that come with Kubernetes’ ability to manage the resources effectively.

    • Port forwarding works by allowing the containers that run on a node to communicate with one another or with external services, making it easier for the users to access those services externally.
    • Port forwarding provides several key advantages when used in combination with Kubernetes. First, it simplifies the external communications by exposing only certain services while leaving the other ports hidden. This improves security and reduces the risk of malicious attacks by preventing the attackers from accessing the restricted ports.
    • Additionally, port forwarding can help balance the traffic across multiple nodes which can improve the performance and scalability as more load is added to the system.

Potential Challenges with Kubernetes Port Forwarding

Kubernetes port forwarding is an important tool to develop and manage the applications. However, it’s important to understand the potential challenges that may arise with Kubernetes port forwarding. Understanding these challenges can help you determine if this process is right for your application development needs.

    • One of the most common issues with Kubernetes port forwarding is security vulnerabilities. It’s important to secure any ports that is opened by the Kubernetes framework to protect your application from attackers.
    • Additionally, you should be aware of potential networking issues such as NAT or firewall configurations that could block the traffic and prevent an access to your application.
    • Finally, resource exhaustion can also be a problem when using the Kubernetes port forwarding since each new connection requires additional resources from the host machine. To avoid this issue, make sure that you monitor the usage closely and adjust the settings accordingly as needed.

Conclusion

Kubernetes port forwarding is a convenient and efficient way to access the applications that run on Kubernetes clusters. As seen in the discussed steps, it is important to have a basic understanding of how the port forwarding works before attempting to use it. This article outlined the necessary steps involved in setting up the port forwarding in a Kubernetes cluster. By following the given guidelines, the users should be able to easily set up and configure the port forwarding and begin accessing their applications with ease. Additionally, using this method can help reduce complexity when working with multiple pods or services that run on different ports.

About the author

Kalsoom Bibi

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