kubectl port-forward Explained: Reach a Pod or Service from Your Laptop Safely
kubectl port-forward opens a private tunnel from a port on your computer to a port on a Kubernetes pod, through the API server. For example, kubectl port-forward svc/web 8080:80 -n shop lets you open http://localhost:8080 and see the app, without creating a public load balancer. It is meant for debugging and admin tasks: it forwards to one pod, stops when that pod restarts, and needs RBAC permission.
Quick answer
kubectl port-forward svc/web 8080:80 -n shop # service
kubectl port-forward deploy/api 9000:8080 -n shop # deployment (picks a pod)
kubectl port-forward pod/postgres-0 5432:5432 -n db # a pod
LOCAL:REMOTE. Press Ctrl+C to stop.
A live example first
The orders API in namespace shop returns errors, but it is only reachable inside the cluster (a ClusterIP Service). We want to call it from our laptop.
- Find it:
kubectl get svc,pods -n shop
NAME TYPE CLUSTER-IP PORT(S)
service/orders ClusterIP 10.100.42.17 80/TCP
NAME READY STATUS
pod/orders-7d9c8b5f6-x2kqp 1/1 Running
- Forward:
kubectl port-forward svc/orders 8080:80 -n shop
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080
(The Service port 80 maps to container port 8080, so kubectl shows the container port.)
- In another terminal:
curl -s localhost:8080/health
{"status":"degraded","db":"timeout"}
Now we know the database connection is the problem, and we never exposed the API to the internet. Friends, this is the safest way to peek inside a cluster.
How does port-forward work?
port-forward makes a private tunnel from localhost to one pod through the API server.
kubectl connects to the API server with your credentials. The API server checks RBAC, then asks the kubelet on the pod's node to open a stream to the pod's port. Your local port is joined to that stream. The traffic is carried over your authenticated connection, which is why nothing new is opened to the internet.
How do I use it step by step?
- Run
kubectl config current-contextto be sure you are on the right cluster. - Find the resource and its port with
kubectl get svc,pods -n NAMESPACE. - Run
kubectl port-forward TYPE/NAME LOCAL:REMOTE -n NAMESPACE. - Use
localhost:LOCALin your browser, curl or database client. - Press Ctrl+C when finished.
- If the pod restarts, run the command again.
Examples by tool and stack
| Tool / app | Command | Then connect with |
|---|---|---|
| Web app (Node.js, Spring Boot, Django) | kubectl port-forward deploy/web 3000:3000 |
http://localhost:3000 |
| PostgreSQL | kubectl port-forward svc/postgres 5432:5432 -n db |
psql -h localhost -U app |
| MySQL | kubectl port-forward svc/mysql 3306:3306 -n db |
mysql -h 127.0.0.1 -u app -p |
| Redis | kubectl port-forward svc/redis 6379:6379 |
redis-cli -h 127.0.0.1 |
| Grafana | kubectl port-forward svc/grafana 3000:80 -n monitoring |
http://localhost:3000 |
| Prometheus | kubectl port-forward svc/prometheus-server 9090:80 -n monitoring |
http://localhost:9090 |
| Argo CD UI | kubectl port-forward svc/argocd-server 8443:443 -n argocd |
https://localhost:8443 |
Several ports at once: kubectl port-forward pod/app 8080:80 9229:9229 (app plus Node.js debugger).
Common errors
| Error | Cause | Fix |
|---|---|---|
bind: address already in use |
Local port busy | Use another port, e.g. 8081:80 |
error: unable to forward port ... connection refused |
Nothing listens on that pod port | Check the container port with kubectl describe pod |
pods "x" is forbidden ... portforward |
No RBAC permission | Ask for create on pods/portforward |
lost connection to pod |
Pod restarted or was deleted | Run the command again |
| Works on laptop but teammate cannot connect | It binds to 127.0.0.1 only | That is by design; each person runs their own |
Binding address: keep it on localhost
By default kubectl listens only on 127.0.0.1. You can add --address 0.0.0.0, but then anyone who can reach your machine on that network can reach the pod. Use it only on a trusted network, and stop it as soon as you are done.
Real example: why private access matters
In 2018, the cloud-security firm RedLock reported that attackers found a Tesla Kubernetes dashboard on the internet with no password. They used it to read AWS credentials and run cryptocurrency mining. Tools like dashboards, Grafana and admin panels should stay private, and port-forward (or a VPN, or an authenticated ingress) lets your team reach them without exposing them.
Mistakes to avoid
- Using port-forward as a permanent way to serve users. Use a Service and Ingress or a load balancer.
- Forgetting the namespace. You get
NotFounderrors. - Giving everyone
pods/portforwardin production. It is real access to the app and database.
Summary table
| Task | Command |
|---|---|
| Service | kubectl port-forward svc/NAME L:R -n NS |
| Deployment | kubectl port-forward deploy/NAME L:R |
| Pod | kubectl port-forward pod/NAME L:R |
| Many ports | ... 8080:80 9229:9229 |
| Stop | Ctrl+C |
| Permission | create on pods/portforward |
Quick quiz: check what you learned
What does kubectl port-forward create?
- A tunnel from localhost to a pod
- A public load balancer for users
- A new Service of type NodePort
- A copy of the pod on your laptop
Show answer
A. It tunnels a local port to a pod port through the API server.
In 8080:80, which number is the local port?
- The first, 8080
- The second, 80
- Both are local
- Neither is local
Show answer
A. The format is LOCAL:REMOTE.
Forwarding to a Service sends traffic to what?
- One selected pod
- Every pod in turn
- Only the worker node
- The ingress pod
Show answer
A. kubectl chooses one pod behind the Service.
Which RBAC permission is needed?
- pods/portforward create
- secrets list everywhere
- nodes delete access
- cluster-admin always
Show answer
A. Port-forward uses the pods/portforward subresource.
Learn it properly
These pages go deeper, with labs:
Frequently asked questions
What does kubectl port-forward do?
It opens a tunnel from a port on your computer, through the Kubernetes API server, to a port on a pod. Your local tools can then talk to the pod as if it ran on localhost.
What is the syntax?
kubectl port-forward TYPE/NAME LOCAL:REMOTE -n NAMESPACE, for example kubectl port-forward svc/web 8080:80.
Does port-forward to a Service load balance?
No. kubectl picks one pod behind the Service and forwards only to that pod for the life of the tunnel.
Why does the forward stop by itself?
If the pod restarts or is deleted, or the connection is idle or broken, the tunnel ends. Start it again.
Is port-forward safe for production use?
It is for debugging and admin access by people with RBAC permission. Do not use it as a way to serve real users.
Which permission does it need?
RBAC permission to create the pods/portforward subresource in that namespace.