kubectl port-forward - Forward one or more local ports to a pod
kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]
Options you will use
--address=[localhost]- Addresses to listen on (comma separated). Only accepts IP addresses or localhost as a value. When localhost is supplied, kubectl will try to bind on both 127.0.0.1 and ::1.
--pod-running-timeout=1m0s- The length of time to wait until at least one pod is running
Examples
$ kubectl port-forward pod/web-6b8d9c7f5d-8xk2p 8080:80listen on localhost:8080, forward to port 80 in the pod
$ kubectl port-forward svc/web 8080:80 &svc/ picks ONE pod behind the Service and forwards to its targetPort - no load balancing, no kube-proxy
$ kubectl port-forward deploy/api :8080a random local port
$ kill %1stop a backgrounded port-forward
Gotchas
- The tunnel goes kubectl -> apiserver -> kubelet -> the pod's network namespace, so NetworkPolicies do not apply to it and it works even for ClusterIP-only services.
- If the pod dies the forward ends ("lost connection to pod"). It never follows the Service to a new pod.
Try kubectl port-forward in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.