Skip to main content

Command Palette

Search for a command to run...

Port Forwarding: Expose Services Like a Secret Hacker

Article 5 in 6-part series: The Secret Handbook for Kubernetes Operators

Updated
4 min read
Port Forwarding: Expose Services Like a Secret Hacker

Sometimes, Kubernetes services are locked away inside the cluster, accessible only to other pods. This is great for security but a nightmare when you need to debug something.

Ever needed to test a database connection, but it’s only available inside Kubernetes? Or access an internal API without deploying a whole new service?

Port forwarding is your secret tunnel into the cluster. With a single command, you can punch a hole in Kubernetes' walls and make a pod or service accessible from your local machine—without modifying any network policies or exposing anything publicly.


1. The Basics: Forwarding a Pod’s Port to Your Machine

The simplest use case is accessing a pod directly. If a pod is running a web app on port 8080, you can forward it to your local machine like this:

kubectl port-forward pod/my-pod 8080:80 -n my-namespace
  • 8080 → The port on your local machine.

  • 80 → The port inside the pod.

Now you can visit http://localhost:8080, and it will magically route traffic to the pod inside the cluster.

Need to forward multiple ports? Just separate them with spaces:

kubectl port-forward pod/my-pod 8080:80 8443:443 -n my-namespace

Now both localhost:8080 and localhost:8443 will forward traffic inside the cluster.


2. Forwarding a Kubernetes Service (Not Just a Pod)

Forwarding a pod works, but what if the pod restarts? The forwarded connection dies, and you have to manually re-establish it. Instead, forward traffic to a Kubernetes Service.

kubectl port-forward svc/my-service 9090:80 -n my-namespace

Now http://localhost:9090 connects to the service no matter which pod is behind it.

This is perfect for debugging load-balanced services—you don’t care which pod handles the request, as long as you reach the service.


3. Debugging a Database Inside Kubernetes

Let’s say your PostgreSQL database is running inside the cluster on port 5432, and you need to connect to it from your local machine. Instead of deploying an external client inside Kubernetes, just forward the port:

kubectl port-forward svc/my-postgres 5432:5432 -n database

Now, connect to it from your favorite database tool:

psql -h localhost -p 5432 -U myuser -d mydatabase

Your database client now thinks PostgreSQL is running on your local machine, but in reality, it’s securely forwarding traffic to the Kubernetes service.

This also works for Redis, MySQL, MongoDB, or any other database you need access to.


4. Exposing a Private API for Local Development

Let’s say you have a backend service running in Kubernetes, but it’s only accessible inside the cluster. Instead of deploying a temporary Ingress or modifying network policies, just forward its port:

kubectl port-forward svc/internal-api 4000:4000 -n backend

Now, your local frontend can hit http://localhost:4000, and it will behave exactly as if the API were running locally.

This is especially useful when working on a frontend that depends on a Kubernetes backend, but you don’t want to expose the API externally.


5. Forwarding Traffic to a Specific Node (Advanced Use Case)

If you need to connect directly to a node’s internal service, you can forward traffic through the Kubernetes API server to a node:

kubectl port-forward node/my-node 5000:5000

Now, localhost:5000 connects to the actual node’s IP, which is useful for debugging Kubelet, metrics servers, or other node-level services.


6. Running Port Forwarding in the Background

By default, kubectl port-forward blocks your terminal while it runs. If you need to keep it running in the background, append & to the command:

kubectl port-forward svc/my-service 8080:80 -n my-namespace &

Now you can keep working in the same terminal.

To stop it later, find its process and kill it:

ps aux | grep port-forward
kill <process-id>

Or, if you're on macOS or Linux, use this handy one-liner:

pkill -f "kubectl port-forward"

This stops all active port-forward processes at once.


7. Troubleshooting Port Forwarding Issues

🔴 "Error from server: No such pod"

  • You’re trying to forward a pod that doesn’t exist.

  • Run kubectl get pods -n my-namespace to find the correct name.

🔴 "Address already in use"

  • Another process is already using the local port.

  • Either stop the existing process (pkill -f "kubectl port-forward") or use a different local port:

      kubectl port-forward svc/my-service 9999:80
    

🔴 "Pod disappeared during forwarding"

  • If you’re forwarding to a pod and it restarts or gets evicted, the connection breaks.

  • Solution: Always forward traffic to a Service, not a Pod:

      kubectl port-forward svc/my-service 8080:80
    

Final Thoughts

Port forwarding is one of Kubernetes' most underrated debugging tools. Whether you need to:

  • Access a database without exposing it publicly

  • Test an internal API without deploying an external Ingress

  • Inspect a load-balanced service from your local machine

  • Debug a node-level process inside the cluster

A single kubectl port-forward command can save hours of frustration.

Next time someone asks, "Hey, can I just access that service inside Kubernetes?", don’t waste time deploying workarounds. Forward the port, and get on with your life. 🚀

The Secret Handbook for Kubernetes Operators

Part 2 of 7

Mastering kubectl: The Secret Handbook unveils powerful, lesser-known kubectl tricks to level up your Kubernetes game. From hidden gems to practical insights, this series will transform you into a command-line sorcerer—minus the ritual sacrifices.

Up next

Debugging and Forensics: CSI Mode for Your Cluster

Article 4 in 6-part series: The Secret Handbook for Kubernetes Operators