Does kubectl port-forward ignore loadBalance services?

kubectl port-forward looks up the first Pod from the Service information provided on the command line and forwards directly to a Pod rather than forwarding to the ClusterIP/Service port. The cluster doesn’t get a chance to load balance the service like regular service traffic.

The kubernetes API only provides Pod port forward operations (CREATE and GET). Similar API operations don’t exist for Service endpoints.

kubectl code

Here’s a little bit of the flow from the kubectl code that seems to back that up (I’ll just add that Go isn’t my primary language)

The portforward.go Complete function is where kubectl portforward does the first look up for a pod from options via AttachablePodForObjectFn:

The AttachablePodForObjectFn is defined as attachablePodForObject in this interface, then here is the attachablePodForObject function.

To my (inexperienced) Go eyes, it appears the attachablePodForObject is the thing kubectl uses to look up a Pod to from a Service defined on the command line.

Then from there on everything deals with filling in the Pod specific PortForwardOptions (which doesn’t include a service) and is passed to the kubernetes API.

Leave a Comment