Port Forwarding

Difficulty: Beginner

Question

How do you access a Pod or Service that is only reachable inside the cluster from your local machine, for example to debug it?

Answer

Use kubectl port-forward, which opens a tunnel from a port on your machine through the Kubernetes API server and the node's kubelet into the Pod. For example, kubectl port-forward pod/api-7d9f 8080:80 makes the Pod's port 80 available at localhost:8080 without creating a Service, Ingress or public IP.

How the tunnel works

  1. kubectl opens a streaming connection to the API server, authenticated with your kubeconfig.
  2. The API server proxies the stream to the kubelet on the node running the Pod.
  3. The kubelet (via the container runtime) connects to the target port inside the Pod's network namespace.
  4. Bytes from localhost:8080 flow through this chain in both directions.

Because traffic goes through the API server, it works even for private clusters with no public node IPs, as long as you can reach the API endpoint. Older versions used SPDY for the stream; newer kubectl and API servers use WebSockets.

Targets and useful options

  • pod/<name>: a specific Pod.
  • svc/<name> or deploy/<name>: kubectl resolves the resource to one Pod and forwards to that Pod only. It does not load balance and does not go through kube-proxy, so if that Pod dies the forward breaks.
  • 8080:80 maps local 8080 to remote 80; :80 picks a random free local port; you can list several mappings.
  • --address 0.0.0.0 lets others on your network use the forward (the default is localhost only).

When to use it, and when not

Good uses:

  • Opening an internal admin UI like Grafana, Argo CD or RabbitMQ management
  • Connecting a local SQL client to a PostgreSQL Pod for a quick query
  • Testing a new version of a service before exposing it through Ingress

It is not a production access path. It is single-user, tied to your terminal session, only supports TCP (no UDP), and every byte goes through the API server, which is not designed to carry heavy traffic. For permanent access use a Service of type LoadBalancer, an Ingress or Gateway, or a VPN or bastion into the VPC.

Security and gotchas

  • Port forwarding needs the RBAC permission create on pods/portforward. In many companies developers can read Pods in production but cannot port-forward, because it bypasses NetworkPolicies and Ingress authentication.
  • If the Pod restarts or is rescheduled, the session ends with an error and you must rerun the command.
  • Idle connections may be closed by load balancers in front of the API server; long database sessions can drop.
  • kubectl proxy is different: it exposes the Kubernetes API itself on localhost, not an application port.
  • Tools like Telepresence or mirrord go further and route cluster traffic to a process on your laptop for development.
Path of a port-forward connection. Traffic is tunnelled through the API server and kubelet, not through Services or kube-proxy.
  1. localhost:8080 on your laptop: Your browser or client connects here.
  2. kubectl: Holds an authenticated stream to the cluster.
  3. API server: Checks RBAC for pods/portforward and proxies the stream.
  4. Kubelet on the node: Connects into the Pod's network namespace.
  5. Pod port 80: The application receives the traffic.

Code examples

Common port-forward commands

# Forward to a Pod
kubectl port-forward pod/api-7d9f 8080:80 -n shop

# Forward to a Service (kubectl picks one backing Pod)
kubectl port-forward svc/grafana 3000:80 -n monitoring

# Local SQL client to an in-cluster PostgreSQL
kubectl port-forward svc/orders-db 5433:5432 -n data &
psql -h localhost -p 5433 -U app orders

# Random local port, listen on all interfaces
kubectl port-forward deploy/api :8080 --address 0.0.0.0

# Can I do this at all?
kubectl auth can-i create pods/portforward -n shop

Forwarding to a Service or Deployment still connects to a single Pod; the last command checks the required RBAC permission.

Key points

  • kubectl port-forward tunnels local ports into a Pod via the API server and kubelet
  • Works for private clusters as long as you can reach the API server
  • svc/ and deploy/ targets resolve to one Pod; there is no load balancing
  • TCP only, single session, breaks when the Pod restarts
  • Requires RBAC create on pods/portforward

Common follow-up questions

  • How is kubectl port-forward different from kubectl proxy?
  • How would you expose this service to other teams permanently?
  • Why might port-forward to a Service stop working after a rollout?
  • Does port-forward traffic respect NetworkPolicies?

Interview tips

  • Describe the tunnel path through the API server; it shows you understand why it works on private clusters
  • Say clearly that it is for debugging, not production traffic
  • Mention the RBAC permission and the security implications

Concepts covered

kubectl port-forward, API Server Tunnel, Local Debugging, pods/portforward RBAC, Service Exposure

More Kubernetes & Orchestration questions