Kubernetes β€’ Networking & Access

How to Port-Forward a Kubernetes Pod or Service to Localhost

The `kubectl port-forward` command establishes an encrypted tunnel between your local workstation and a target Pod or Service inside the cluster. It allows developers to test internal microservices, databases (Postgres, Redis), or private APIs locally.

Port-Forward Pod or Service Command
Safe β€’ Read-Only / Non-Destructive
kubectl port-forward svc/my-app 8080:80 -n default
Customize:
Namespace:
Resource Name:

Flags & Parameter Breakdown

svc/<service-name>Target service to forward traffic to (or pod/<pod-name>)
8080:80<local-port>:<remote-cluster-port>
-n <namespace>Kubernetes namespace where service resides
--address 0.0.0.0Optional: Listen on all network interfaces instead of localhost only

Step-by-Step Execution Workflow

1Identify target service and cluster port

Find the service name and its internal targetPort:

kubectl get svc -n default
2Initiate the port-forward tunnel

Run the command to bind local port 8080 to cluster port 80:

kubectl port-forward svc/my-service 8080:80 -n default
3Test connection in another terminal or browser

Send an HTTP request or open in your browser at http://localhost:8080:

curl http://localhost:8080/healthz

Common Pitfalls & Troubleshooting Advice

  • If you get "bind: address already in use", check if another process is using port 8080 (e.g. `lsof -i :8080` or `netstat -ano`).
  • Port-forward connections drop automatically if the target pod restarts or during rolling deployments.

Prerequisites & Cluster Access

  • Active kubeconfig context connected to the cluster.
  • Read/get permissions for pods and services in the target namespace.

Frequently Asked Questions About Port-Forward Pod or Service

Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

Yes, running `kubectl port-forward deployment/my-app 8080:80` will automatically route to one of the active replica pods managed by that deployment.