K8s loadbalancer externalTrafficPolicy "Local" or "Cluster"

Pablo Perez
Pablo Perez
Published in
2 min readNov 13, 2018

--

{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "example-service"
},
"spec": {
"ports": [{
"port": 8765,
"targetPort": 9376
}],
"selector": {
"app": "example"
},
"type": "LoadBalancer",
"externalTrafficPolicy": "Local"
}
}

With regard to setting the value “Cluster” instead of “Local”, the difference basically resides that when using “Cluster” value, Kubernetes will perform further balancing having the ability to forward the request to another node, adding one more hop in order to balance the load more efficciently.

As you can verify in the documentation, the drawback of setting a “Local” value in the policy is that “risks potentially imbalanced traffic spreading.”

https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/

I recommend you to take a quick look at this video below from the Google Cloud Conference which provides a good illustration about what happens under the hood regarding both values of this specific property, externalTrafficPolicy. It’s the better available explanation. From the minute 30' to minute 38', “Cluster” and “Local” behaviours for external traffic policy are explained deeply for load balanced services.

Basically, when you set “Local” value, in the case you had more pods in the worker node A than in the worker node B, the Load balancer would route the traffic equally between worker A and Worker B. This happens becuase the load balancer only knows about nodes and not about pods. And because you have more pods in the worker A you would have an imbalance problem as the pod in worker B would get more requests than the pods in the other worker node being the pod in the worker B overloaded and resources not consumed efficiently.

“Local” means that when the packet arrives to a pod, kube proxy will only distribute the load within the same node pods even other node in the cluster has more pods available and less loaded.

On the other hand, when setting “Cluster” value, the balancing takes into account not only the nodes but also the number of pods to distribute the requests, and to avoid imbalance, kubernetes performs the balancing within the cluster.

https://www.youtube.com/watch?v=y2bhV81MfKQ&feature=youtu.be&t=1823

--

--