kubectl 1.37 reference · Debugging

kubectl port-forward Command (kubectl)

Forward a local port to a pod or Service for testing and debugging without exposing it publicly.

kubectl 1.37

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

kubectl port-forward POD|svc/NAME|deploy/NAME [LOCAL_PORT:]REMOTE_PORT

Parameters

ParameterDescription
LOCAL:REMOTE Local port and the pod's port.
--address ADDR Bind address (default 127.0.0.1).

Examples

Reach a pod from your machine
kubectl create namespace rc-ref-pf > /dev/null
trap 'kubectl delete namespace rc-ref-pf --wait=false > /dev/null' EXIT
kubectl -n rc-ref-pf create deployment web --image=nginx:1.29-alpine > /dev/null
kubectl -n rc-ref-pf rollout status deployment/web --timeout=120s > /dev/null
kubectl -n rc-ref-pf port-forward deploy/web 18091:80 > /dev/null 2>&1 &
pf=$!; sleep 2
curl -s localhost:18091 | grep -o '<title>.*</title>'
kill $pf

Output

<title>Welcome to nginx!</title>

Run this on your computer — see “Get Started” for setup.

Forward to a Service
kubectl create namespace rc-ref-pf2 > /dev/null
trap 'kubectl delete namespace rc-ref-pf2 --wait=false > /dev/null' EXIT
kubectl -n rc-ref-pf2 create deployment web --image=nginx:1.29-alpine > /dev/null
kubectl -n rc-ref-pf2 expose deployment web --port=8080 --target-port=80 > /dev/null
kubectl -n rc-ref-pf2 rollout status deployment/web --timeout=120s > /dev/null
kubectl -n rc-ref-pf2 port-forward svc/web 18092:8080 > /dev/null 2>&1 &
pf=$!; sleep 2
curl -s -o /dev/null -w 'HTTP %{http_code} through the Service\n' localhost:18092
kill $pf

Output

HTTP 200 through the Service

Run this on your computer — see “Get Started” for setup.

Port-forwarding connects to one pod (even via a Service), so it's for debugging, not load-balanced access.

Was this page helpful?