Difficulty: Beginner
How do you access a Pod or Service that is only reachable inside the cluster from your local machine, for example to debug it?
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.
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.
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).Good uses:
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.
create on pods/portforward. In many companies developers can read Pods in production but cannot port-forward, because it bypasses NetworkPolicies and Ingress authentication.kubectl proxy is different: it exposes the Kubernetes API itself on localhost, not an application port.# 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.
kubectl port-forward, API Server Tunnel, Local Debugging, pods/portforward RBAC, Service Exposure