How to Set Up kubectl Port Forwarding?

Photo of author

By admin

If you are working on Kubernetes, you must be well aware of the kubectl command line. The question here is “how to set up kubectl port forwarding”. Before diving right into the problem, first, you must understand the basic terms to grasp the concept better and why you are doing what you are doing.

There are several components of Kubernetes that make it the best in the containerization world. One of such tools is the kubectl command line. Every user who is running Kubernetes uses kubectl.

Do not worry if you are new to Kubernetes and have no experience with kubectl. Kubectl is a command-line tool used to manage Kubernetes clusters. Every machine on which Kubernetes is installed runs this software which manages the configuration, deployment, and management of nodes within a cluster.

But this article is about port forwarding, so let’s dive deeper into it. Port forwarding is a way to connect to a server or service in a network. Kubectl port-forwarding helps you access and communicate with your internal Kubernetes cluster process. By setting up kubectl port forwarding, you can establish a connection from your localhost.

In this guide, you will find the answers to all your questions about kubectl port forwarding.

What Do You Need?

There are certain prerequisites that will be required to set up port-forwarding.

  • A Kubernetes cluster
  • A kubectl command-line tool
  • MongoDB Shell

Check your version and ensure that it is the Kubernetes v1.10 or higher.

How Does Port Forwarding Work in Kubernetes?

In this automated system, there are certain setups that you need to implement manually and kubectl port forwarding is one of them. Kubectl comes with a built-in port-forwarding feature, but once a pod instance fails, you must manually establish a connection.

So, this is how it works.

  • The user has a kubectl command line on his local machine. He wants to connect to a cluster resource directly.
  • The user uses the port-forward command which uses cluster resource name and port number for port-forwarding.
  • Your localhost and the cluster resource will have a single HTTP connection established between them.
  • Now the user can access the pod and perform necessary actions like debugging or diagnosing problems.

How To Create MongoDB Deployment And Service?

If you do not have a deployment running MongoDB, you can create one by following these steps.

1. Create a MongoDB Deployment

Execute this command.

kubectl apply -f

The output will look like-

deployment.apps/mongo created

To check if it is ready, enter-

kubectl get pods

The output should be-


mongo-75f59d57f4-4nd6q 1/1 Running 0 2m4s

The deployment status can be checked with-

kubectl get deployment

The created deployment will be reflected-


mongo 1/1 1 1 2m21s

This deployment also takes care of the ReplicaSet. You can view it with-

kubectl get replicaset

It will reflect the output as-


mongo-75f59d57f4 1 1 1 3m12s

2. Create A Service

The next step is to create a Service that will reflect MongDB on the network. Use the command-

kubectl apply -f

The output should look like-

service/mongo created

A Service is also created which can be checked by-

kubectl get service mongo

It will reflect the result as-


mongo ClusterIP <none> 27017/TCP 11s

The last step is to check whether the MongoDB server is running on Pod. It should be listening on port 27017 which is default port for MongoDB server.

kubectl get pod [name of pod] –template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{“\n”}}’

With these steps, you will be able to easily deploy MongoDB. Now, let’s come to the question “how to set up a kubectl port-forwarding”.

How To Setup kubectl Port-Forwarding?

The “port-forward” command helps you to establish a connection between a pod and your local host wherein you need to define your resource cluster and local and remote ports.

You must know the name of the pod name to directly link to it. To find the name of the pods, run the command-

kubectl -n yournamespace get pods

It will reflect the different pods within the namespace. Learn the one you want to port forward to. Say it is mongo-fg-t245fu7gt and the port number is 5762. Your local host is 8080.

All of these commands written below are the same and will port-forward to a specific pod.

Kubectl port-forward mongo-fg-t245fu7gt 8080:5762


kubectl port-forward pods/ mongo-fg-t245fu7gt 8080:5762


kubectl port-forward deployment/mongo 8080:5762


kubectl port-forward replicaset/ mongo-fg-t245fu7gt 8080:5762


kubectl port-forward service/mongo 8080:5762

All of the above commands will yield results like

Forwarding from -> 5762

Forwarding from [::1]:8080 -> 5762

Using Random Local Port

If you wish to establish a connection to a specific port from a random local port, use the command-

kubectl port-forward pod/ mongo-fg-t245fu7gt :5762

Using Local and Remote Ports

To listen to and forward data in a specific pod locally with identical ports, use-

kubectl port-forward pod/ mongo-fg-t245fu7gt 8080 5762

Random Local IP address

To forward data to port 5762 in specified pod while listening on 8080, use-

kubectl port-forward –address pod/ mongo-fg-t245fu7gt 8080:5762

Specified Local IP Address

To forward data to port 5762 in specified port using a defined IP, while listening to port 8080, use-

kubectl port-forward –address localhost,[defined IP], pod/ mongo-fg-t245fu7gt 8080:5762

Deployment To Choose Port-forward Pod

Use the deployment to listen and forward data by using-

kubectl port-forward deployment/mydeployment 8080 5762

Use Service to Choose Port-Forward Pod

Use the service to listen and forward data using the same ports in the pod.

kubectl port-forward service/myservice 8080 5762


Kubectl port-forwarding is helpful in many ways. It allows you to look into problems and debug them locally. It helps you establish a secure connection and control the data traffic. The connection between the two ports is also encrypted so you are completely safe. The ways to set up kubectl port-forwarding are simple. All you need to do is decide how you want to connect, whether you will use a local port, a random local port, an IP address or another method.

This guide covers all the ways to set up kubectl port-forwarding. With a Kubernetes cluster, a kubectl command line, and MongoDB, you can set it up in seconds. This guide also covers the instructions to create a MongoDB deployment and service.

By simply following the commands, it is a cakewalk to set up Port-forwarding. Just make sure that while typing the commands, you type the correct names of resource clusters, IP addresses, ports, etc. The guide contains various answers to your question “How to setup kubectl port-forwarding?”

Leave a Comment