Kubernetes: How to configure the load balancing between Service and Pod

Viewed 1266

Inside a K8s cluster, I run a web application with 2 Pods (replica is 2), and expose a them using a Service with type LoadBalancer. Then I do an experiment by sending 2 consecutive requests, I found that both request are handled by the same Pod.

Anyone can help me to explain this behavior? And what should I do to change this behavior to round robin or something else?

2 Answers

By default, kubernetes uses iptables mode to route traffic between the pods. The pod that is serving request is chosen randomly.

For 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.

It can be checked using 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

As mentioned by @Zambozo IPVS proxy mode allows you to use round-robin algorithm (which is used by default) to spread the traffic equally between the pods.

Related