Linux Load Balancing – Implement Load Balancing on Any Linux Distro

bandwidthiplinuxload balancing

I am thinking of implementing a load balancing solution for personal use.

What I want to do, is to maximize data throughput over mobile phone Internet connections. Let me be clear:

I have data plan in my mobile phone and my family have their respective data plans in their phones, too. If I can connect up to 4 phones in one (desktop) PC (through USB preferably) then I will achieve (theoretically) a faster Internet connection than any one of the 4 phones can achieve (if I connect them to a PC).

That desktop computer will then act as a router for an intranet.

If the above has a sound basis (I could be wrong – don't know the technologies involved in great detail), I need a how to to implement that.

I have seen that the tool for the job is ipvs (right?) but no how to.

Distro-wise the job can be done in any distro, but I know that connecting an Android phone with Ubuntu works plug and play.
So If I can do it in Ubuntu, it will probably be faster than compiling everything from strach.

Is there a relative how to? Is there a distro perhaps that does load balancing, and identifies USB internet connections on the fly?

Best Answer

To balance outgoing connections all you need is standard iptables and some policy routing. This does get a bit complex with 4 connections as you will need to reconfigure and rebalance the links as connections come and go.

The raw iptables setup is

  • Create a routing table for each connection

    ip rule add fwmark 10 table PHONE0 prio 33000
    ip rule add fwmark 11 table PHONE1 prio 33000
    ip rule add fwmark 12 table PHONE2 prio 33000
    ip rule add fwmark 13 table PHONE3 prio 33000
    
  • Add the default gateway for each connection to each table (the gateway IP will vary depending on each phones provider/setup)

    ip route add default via 192.168.1.2 table PHONE0 
    ip route add default via 192.168.9.1 table PHONE1 
    ip route add default via 192.168.13.2 table PHONE2 
    ip route add default via 192.168.7.9 table PHONE3 
    
  • Randomly mark any unmarked flows, which will route the flow via a specific connection. OUTPUT is used for local processes. Use PREROUTING if you are forwarding traffic for other clients)

    iptables -t mangle -A OUTPUT -j CONNMARK --restore-mark
    iptables -t mangle -A OUTPUT -m mark ! --mark 0 -j ACCEPT
    iptables -t mangle -A OUTPUT -j MARK --set-mark 10
    iptables -t mangle -A OUTPUT -m statistic --mode random --probability 0.25 -j MARK --set-mark 11
    iptables -t mangle -A OUTPUT -m statistic --mode random --probability 0.25 -j MARK --set-mark 12
    iptables -t mangle -A OUTPUT -m statistic --mode random --probability 0.25 -j MARK --set-mark 13
    iptables -t mangle -A OUTPUT -j CONNMARK --save-mark
    
  • NAT for each of the connections (the interface will need to be whatever you phone connection appears to the system as)

    iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
    iptables -t nat -A POSTROUTING -o ppp1 -j MASQUERADE
    iptables -t nat -A POSTROUTING -o ppp2 -j MASQUERADE
    iptables -t nat -A POSTROUTING -o ppp3 -j MASQUERADE
    

Note that a single TCP or UDP connection will see no speed up as it will still be going over a single link. You have to use multiple concurrent connections (at least 4) to make use of the extra bandwidth. Most browsers do this under the hood when requesting multiple objects. Some download managers allow you to use multiple connections for a single file.

As garethTheRed suggests, ispunity adds some of the "glue" on top of this iptables setup to loop through a list of connections, check that the gateway is responding, re balance if something is wrong etc. It's "sticky session" management looks to be additional setup per port on top of it's base "round robin" load balancing of connections. Another solution is Net-ISP-Balance, a Perl script and library that automates the iptables and routing table configuration, monitors the ISP status, alerts you to problems, and reconfigures the routing in case one or more ISPs become inacessible.

Also note that having requests come from multiple IP's can break some services that are based on consistent IP lookups and you may need to add additional rules for those services to tie them to a connection. You won't see any speedup on single connections, only when you are doing 4 things at once, which most browsers will try to do anyway.

ipvs is more for creating a virtual service addresses for things you host so the service can be failed over between multiple hosts.

Related Question