How do kubernetes services balance load internally?

3/3/2019

I got the following architecture:

     [Service]
    /    |    \
 [Pod1][Pod2][Pod3]

We assert the following Pod IPs:

  • Pod 1: 192.168.0.1
  • Pod 2: 192.168.0.2
  • Pod 3: 192.168.0.3

I am executing a loop like this:

for ((i=0;i<10000;i++)); do curl http://someUrlWhichRespondsWithPodIP >> curl.txt; done;

This writes the pods IP 10000 times. I expected it to be round robin schemed but it was not. File looks similar to this:

192.168.0.1
192.168.0.1
192.168.0.3
192.168.0.2
192.168.0.3
192.168.0.1

The service config looks like this:

kind: Service
metadata:
  name: service
spec:
  type: NodePort
  selector:
    app: service
  ports:
  - name: web
    protocol: TCP
    port: 31001
    nodePort: 31001
    targetPort: 8080

Anyone has an idea what kind of internal load balancing kubernets is using?

-- elp
kubernetes
kubernetes-service
load-balancing

1 Answer

3/3/2019

You are probably using the default iptables mode of kube-proxy, which uses iptables NAT in random mode to implement load balancing. Check out the ipvs support (https://kubernetes.io/docs/concepts/services-networking/service/#proxy-mode-ipvs) for a big pile of other modes including round robin.

-- coderanger
Source: StackOverflow