5

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?

Xiao Ma
  • 95
  • 2
  • 7
  • How are you sending the requests? If you're connecting with `kubectl port-forward`, or you're sending both requests over the same persistent HTTP connection, they'll always reach the same pod. – David Maze Nov 25 '20 at 11:37
  • Hi, @DavidMaze I check the `EXTERNAL-IP` of the service and request directly using that.I am not sure if I use the `kubectl port-forward`, I did not use it directly. – Xiao Ma Nov 26 '20 at 01:56

2 Answers2

3

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.

kool
  • 3,214
  • 1
  • 10
  • 26
2

I think you may have to look into IPVS proxy mode. IPVS provides more options for balancing traffic. https://kubernetes.io/docs/concepts/services-networking/service/#proxy-mode-ipvs

Zambozo
  • 456
  • 4
  • 12
  • Hi, I actually checked out the IPVS proxy mode of the `kube-proxy` but I did find out any resource on how to set up it in a existing K8s cluster, do you have any resource? Thanks – Xiao Ma Nov 26 '20 at 02:30
  • You can try this: https://stackoverflow.com/a/56497675/8003861 @XiaoMa – lamhoangtung May 08 '22 at 18:38