I have a ClusterIP Service which is used to distribute load to 2 PODs internally. The load is not distributed evenly across the PODs.
How to make the load distributed evenly ?
kube-proxy in User space proxy mode chooses backend pod via round robin fashion.
kube-proxy in iptables mode chooses the backend pod randomly.
Kubernetes uses iptables to distribute the load between pods (iptables proxy mode by default).
If you have 2 pods, it is distributed evenly with 0.5 (50%) probability. Because it is not using round-robin, the backend pod is chosen randomly. It will be even in a longer time-frame.
If there would be 3 pods, probability will change to 1/3 (33%), for 4 pods 1/4 and so on.
To check it you can run sudo iptables-save
.
Example output for 2 pods (for nginx service):
sudo iptables-save | grep nginx
-A KUBE-NODEPORTS -p tcp -m comment --comment "default/nginx:" -m tcp --dport 31554 -j KUBE-SVC-4N57TFCL4MD7ZTDA //KUBE-SVC-4N57TFCL4MD7ZTDA is a tag for nginx service
sudo iptables-save | grep KUBE-SVC-4N57TFCL4MD7ZTDA
-A KUBE-SVC-4N57TFCL4MD7ZTDA -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-SOWYYRHSSTWLCRDY
If you want to make sure load is distributed evenly using round-robin algorithm you can use IPVS which by default uses rr (round-robin). It works as load balancer in front of a cluster and directs requests for TCP- and UDP-based services to the real servers, and make services of the real servers appear as virtual services on a single IP address. It is supported on cluster created by local-up, kubeadm and GCE.