Load balance between source IPs in Linux

Today I received a question about how to distribute the outgoing connections between several IP addresses attached to an interface. Suppose that you have 3 IPs in the eth0 interface and you want to do round robin between that IPs for outgoing connections. With regular iproute commands you can’t. Doing some tricks with fwmarks, ip rule and ip route neither.

The only way that I’ve found to it is using SNAT and statistics to get a real Round Robin balance:

 

iptables -t nat -A POSTROUTING  -m statistic --mode nth --every 3 -j SNAT --to 192.168.1.201

iptables -t nat -A POSTROUTING  -m statistic --mode nth --every 2 -j SNAT --to 192.168.1.202

iptables -t nat -A POSTROUTING  -m statistic --mode nth --every 1 -j SNAT --to 192.168.1.203

The IPs described in the example should be local IPs.

Post to Twitter

One thought on “Load balance between source IPs in Linux”

Leave a Reply

Your email address will not be published. Required fields are marked *