debug

kubectl port-forward

Forwards one or more local ports to a pod, service, or deployment inside the cluster via the API server. Ideal for reaching internal services (databases, admin UIs) from your laptop without exposing them publicly.

kubectl port-forward TYPE/NAME [LOCAL_PORT:]REMOTE_PORT [...] [flags]

Common flags

Flag Purpose
--address Local addresses to bind (default 127.0.0.1; use 0.0.0.0 to expose on LAN).
-n, --namespace Namespace of the target resource.
--pod-running-timeout How long to wait for at least one pod to be running (default 1m).

Examples

kubectl port-forward svc/postgres 5432:5432

Reach an in-cluster Postgres from localhost:5432.

kubectl port-forward deploy/api 8080:80

Map local 8080 to the api pod's container port 80.

kubectl port-forward -n monitoring svc/grafana 3000:3000 --address 0.0.0.0

Expose Grafana to your local network.

Gotcha

The tunnel dies if the target pod restarts; wrap it in a loop for long sessions. Forwarding to a Service picks a single backing pod, not the load-balanced VIP.

Related commands

← All kubectl commands · Docker vs Kubernetes