Linux Network Load Balancer For Multiple ISP Links

This article explains how to configure a Redhat/CentOS (and many other Linux distributions) Linux server as a load balancer. For this example, there are two routers each with a different ISP although they could be the same ISP.
This was tested on a CentOS 6.3 64bit server.
In the script below, the load balancer has three interfaces: Eth0 is on the inside while Eth1 and Eth2 are connected to the routers.
Server addresses:
Eth0 = 192.168.0.10 (connected to inside network)
Eth1 = 192.168.1.10 (connected to router1/ISP1)
Eth2 = 192.168.2.10 (connected to router2/ISP2)
Router addresses:
Router1 = 192.168.1.1
Router2 = 192.168.2.1
Create a script file called “/usr/bin/lb.sh” with the following contents:
#!/bin/bash

# By Andrew Galdes, AGIX, 4 April 2013
# Remember to turn off (or customise) SELinux and add the ROUTER1/2 in
# the "/etc/iproute2/rt_tables" file.
# Do NAT on this server.

service network restart

ip route flush cache

ip route add 192.168.1.0/24 dev eth2 src 192.168.1.10 table ROUTER1
ip route add default via 192.168.1.1 table ROUTER1

ip route add 192.168.2.0/24 dev eth1 src 192.168.2.10 table ROUTER2
ip route add default via 192.168.2.1 table ROUTER2

ip rule add from 192.168.1.10 table ROUTER1
ip rule add from 192.168.2.10 table ROUTER2

# the following command is one line:
ip route append default scope global nexthop via 192.168.1.1 dev eth1 weight 1 nexthop via 192.168.2.1 dev eth2 weight 1

route del default gw 192.168.1.1
route del default gw 192.168.2.1

ip route
Make the script executable:
chmod 755 /usr/bin/lb.sh
Add the script to the boot process:
vi /etc/rc.local
Use the following command to make sure the firewall is set correct.
system-configure-firewall
The firewall should trust the Eth+ devices. It should masquerade the Eth+ devices.
Add the following to the end of the “/etc/iproute2/rt_tables” file:
1 ROUTER1
2 ROUTER2
Consider turning off SELinux as follows or make considered changes:
setenforce 0
Set it permanently in the “/etc/sysconfig/selinux” file if you like.
Make sure to configure static routes on the routers so they know their way back to the inside network.
 From >>>
http://agix.com.au/blog/?p=3173

No comments:

Post a Comment