🐶
Kubernetes

kubectl port-forward: How it Creates Connections

By Jan on 01/19/2025

Learn how kubectl port-forward establishes a secure tunnel between your local machine and a Pod within a Kubernetes cluster.

kubectl port-forward: How it Creates Connections

Table of Contents

Introduction

Accessing applications running within a Kubernetes cluster is often necessary for development and debugging. While Kubernetes offers various methods for service exposure, kubectl port-forward provides a simple and direct way to connect to a pod from your local machine. This article explains how to use kubectl port-forward to interact with your applications running inside Kubernetes pods as if they were running locally.

Step-by-Step Guide

kubectl port-forward lets you access applications running inside Kubernetes pods from your local machine. Here's how it works:

  1. Choose a pod: Identify the pod you want to access. You can use labels, names, or even deployments to select a pod.
    kubectl port-forward pod/mypod 5000:80
  2. Specify ports: Decide which local port should map to which port inside the pod.
    # Forwards local port 5000 to port 80 in the pod
    kubectl port-forward pod/mypod 5000:80 
  3. Run the command: Execute kubectl port-forward with the pod and port information.
  4. Access the application: Now you can access the application running in the pod by going to http://localhost:5000 in your browser (or using other tools) as if it were running locally.

Key points:

  • The connection is active as long as the kubectl port-forward command is running.
  • You can forward multiple ports simultaneously.
    kubectl port-forward pod/mypod 5000:80 6000:8080
  • This is useful for debugging, testing, and interacting with applications without exposing them publicly.
  • Remember that this is for development and testing purposes, not for production environments.

Code Example

This code snippet provides instructions on how to access a Node.js application running inside a Kubernetes pod. It explains how to list pods, forward ports from your local machine to the pod, and access the application through your web browser. The example includes forwarding multiple ports and reminds users to stop port forwarding when finished and to use alternative methods for production environments.

Let's say you have a simple Node.js application running in a pod named "my-nodejs-app" on port 3000. Here's how you can access it locally:

1. Identify the pod:

kubectl get pods

This will list all pods in your current namespace. Find the name of your pod (e.g., "my-nodejs-app").

2. Forward the port:

kubectl port-forward pod/my-nodejs-app 3000:3000

This command forwards your local port 3000 to port 3000 inside the "my-nodejs-app" pod.

3. Access the application:

Open your web browser and go to http://localhost:3000. You should now see your Node.js application running as if it were hosted locally.

4. Multiple ports:

You can forward multiple ports simultaneously. For example, if your application also uses a database on port 3306, you can forward both ports:

kubectl port-forward pod/my-nodejs-app 3000:3000 3306:3306

Now you can access your application on http://localhost:3000 and connect to the database on localhost:3306 using your preferred database client.

Remember:

  • Close the terminal or press Ctrl+C to stop the port forwarding.
  • This is a powerful tool for development and debugging, but avoid using it in production environments. Consider using services and ingresses for exposing applications publicly.

Additional Notes

  • Security: While convenient, kubectl port-forward establishes a direct connection from your machine to the pod. Do not use it for exposing services publicly in production. Services and Ingress are the appropriate tools for that.
  • Connection Termination: The connection remains active only while the kubectl port-forward command is running in your terminal. Closing the terminal or interrupting the process (Ctrl+C) will terminate the port forwarding.
  • Resource Type: You can use kubectl port-forward with other Kubernetes resources besides pods, such as deployments, replica sets, or services. Just specify the resource type and name instead of "pod/mypod".
  • TargetPort: When working with services, you can specify the targetPort instead of the container port. This is useful when the service exposes a named port.
  • Background Processes: For long-running processes, you can run kubectl port-forward in the background using &. However, be mindful of background processes and terminate them when no longer needed.
  • Alternatives: For production environments or more complex scenarios, consider using:
    • Services: Expose your application within the cluster using a ClusterIP or NodePort.
    • Ingress: Expose your application to the outside world with load balancing and TLS termination.
  • Debugging: kubectl port-forward is invaluable for debugging. You can connect your local debugger to an application running inside a pod, inspect network traffic, or interact with the application directly.

Remember that kubectl port-forward is a powerful tool for development and testing, but use it responsibly and be aware of its limitations.

Summary

Feature Description Example
Purpose Access applications running inside Kubernetes pods from your local machine.
Pod Selection Identify the target pod using labels, names, or deployments. kubectl port-forward pod/mypod 5000:80
Port Mapping Define which local port maps to which port inside the pod. kubectl port-forward pod/mypod 5000:80 (maps local port 5000 to pod's port 80)
Multiple Ports Forward multiple ports simultaneously. kubectl port-forward pod/mypod 5000:80 6000:8080
Connection Duration Connection remains active as long as the kubectl port-forward command is running.
Access Method Access the application via http://localhost:5000 (or specified local port) in your browser or other tools.
Use Cases Debugging, testing, and interacting with applications without public exposure.
Environment Primarily for development and testing, not recommended for production.

Conclusion

kubectl port-forward proves invaluable for developers and testers working with Kubernetes. It simplifies access to applications within pods, aiding in debugging, testing, and interaction without public exposure. However, keep in mind its limitations. The connection relies on the command running, making it unsuitable for long-term solutions. Additionally, while convenient, it's not meant for production environments due to security concerns. For such scenarios, Kubernetes offers more robust options like Services and Ingress. Understanding the strengths and weaknesses of kubectl port-forward allows for its effective and responsible use during development and testing phases of Kubernetes applications.

References

  • kubernetes - How does kubectl port-forward create a connection ... kubernetes - How does kubectl port-forward create a connection ... | Jul 22, 2018 ... The port-forward feature of kubectl simply tunnels the traffic from a specified port at your local host machine to the specified port on the specified pod.
  • Use Port Forwarding to Access Applications in a Cluster | Kubernetes Use Port Forwarding to Access Applications in a Cluster | Kubernetes | This page shows how to use kubectl port-forward to connect to a MongoDB server running in a Kubernetes cluster. This type of connection can be useful for database debugging. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts.
  • kubectl port-forward - Kubernetes Port Forwarding Explained | Refine kubectl port-forward - Kubernetes Port Forwarding Explained | Refine | We'll see how to use kubectl port-forward to access internal Kubernetes services from outside the cluster.
  • kubectl port-forward | Kubernetes kubectl port-forward | Kubernetes | Synopsis Forward one or more local ports to a pod. Use resource type/name such as deployment/mydeployment to select a pod. Resource type defaults to 'pod' if omitted. If there are multiple pods matching the criteria, a pod will be selected automatically. The forwarding session ends when the selected pod terminates, and a rerun of the command is needed to resume forwarding. kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N] Examples # Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod kubectl port-forward pod/mypod 5000 6000 # Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the deployment kubectl port-forward deployment/mydeployment 5000 6000 # Listen on port 8443 locally, forwarding to the targetPort of the service's port named "https" in a pod selected by the service kubectl port-forward service/myservice 8443:https # Listen on port 8888 locally, forwarding to 50
  • How to Use Kubectl Port-forward in Kubernetes Applications How to Use Kubectl Port-forward in Kubernetes Applications | Learn what is Kubernetes port forwarding and how to use kubectl port-forward command. See examples including running it in the background.
  • How to Use Kubectl Port-Forward to Create a Connection & More How to Use Kubectl Port-Forward to Create a Connection & More | kubectl port-forwarding is a method used in Kubernetes to access and interact with internal resources of the cluster from your local machine.
  • Kubectl port-forward on rancher 2 provision cluster - Rancher ... Kubectl port-forward on rancher 2 provision cluster - Rancher ... | Hello together, sorry for cross posting, but as I have not got any helpful replies yet, I was not sure if I might have been in the wrong forum. As a matter of principle I try to port-forward a mysql database port to 127.0.0.1 on a worker node. I would like to connect to mysql with mysql workbench via ssh through the local node, as I do not want to make the database accessible to the outside. Unfortunately just the ssh connection part works, but I cannot connect to the port forwarded database...
  • Port forwarding is not working on Mac - microk8s - Discuss Kubernetes Port forwarding is not working on Mac - microk8s - Discuss Kubernetes | I’m new to microk8s, and I’m trying out things by depolying a simple apache2 to see things working on my Mac M1: ◼ ~ $ microk8s kubectl run apache --image=ubuntu/apache2:2.4-22.04_beta --port=80 pod/apache created ◼ ~ $ microk8s kubectl get pods NAME READY STATUS RESTARTS AGE apache 1/1 Running 0 5m37s ◼ ~ $ microk8s kubectl port-forward pod/apache 3000:80 Forwarding from 127.0.0.1:...
  • kubernetes - Why does kubectl port-forward require the destination ... kubernetes - Why does kubectl port-forward require the destination ... | Mar 15, 2021 ... Cool, thank you. The link there mentions: "If you make an outbound connection to 127.0.0.1 from a container it will return to the same container ...

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait