I have noticed that kubectl port-forward
only works when the process being forwarded to is listening on 127.0.0.1
or localhost
. If the process is bound to another address, like the pod IP, then port forwarding doesn't work.
For example, suppose I have a program that opens a server socket on the pod IP and port 8080, then this port forward does not work:
kubectl port-forward hasura-console-5d48cdb974-sv4l8 8080:8080
When I try to use it, its logs the following:
E0314 21:11:33.613318 93858 portforward.go:400] an error occurred forwarding 8080 -> 8080: error forwarding port 8080 to pod 9902636a8681f668702476c9e4148d6e4cd63ba20f4b3270fdc3c57d9ef9cb97, uid : exit status 1: 2021/03/15 01:11:34 socat[1486564] E connect(5, AF=2 127.0.0.1:8080, 16): Connection refused
When I restart the pod with the service listening on 127.0.0.1
, the port forward works and the above error is not logged.
Do services running in pods need to listen on localhost?