Linux LVS Load Balancer Setup

Posted on 21st February 2013 in Computer Tips, Linux OS

This tutorial will explain how to create a software load balancer using the Linux kernel, LVS load balancing.

For the purpose of this tutorial (and because I believe it to be the easiest distribution) I will be using Ubuntu Server 12.04 LTS. This will work on other Linux distributions but this guide is written specifically for Ubuntu. This tutorial will assume you are using the root user.

For the purposes of this tutorial I will use the below IP config:
VIP (Virtual IP, load balanced IP) = 192.168.0.110
RIP1 (Real IP 2, real server 1 IP) = 192.168.0.111
RIP2 (Real IP 2, real server 2 IP) = 192.168.0.112

1. Install keepalived package from respository using command ”apt-get install keepalived”.

3. Edit “sysctl.conf” file (use “vim  /etc/sysctl.conf” command), find “#net.ipv4.ip_forward = 1″ and remove the # (uncomment)

4. Run “sysctl -p” command to update sysctl

5. Edit “hosts” file (use “vim  /etc/hosts” command), add the hostnames & ip addresses of all load balancers and real servers

6. Set the loopback on all real servers to the VIP (see below for Windows loopback info)

7. Create a “keepalived.conf” file in “/etc/keepalived/” directory (use “vim /etc/keepalived/keepalived.conf” command) and populate with the below (feel free to edit as required). Please note anything between a set of *** are comments and must be removed before saving the file. E.G. ***blah blah***

vrrp_sync_group VG1 {
group {
VI_1
}
}

vrrp_instance VI_1 {
state MASTER ***Must be “BACKUP” on failover load balancer***
interface eth0
virtual_router_id 1
priority 100 ***Must be less than 100 on failover load balancer, 50 is best***
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.110
}
}
#Load Balance Port 80 – Standard Web Port
virtual_server 192.168.0.110 80 {
delay_loop 15
lb_algo wlc
lb_kind DR
persistence_timeout 50
protocol TCP

real_server 192.168.0.111 80 {
weight 100
TCP_CHECK {
connect_timeout 3
}
}

real_server 192.168.0.112 80 {
weight 100
TCP_CHECK {
connect_timeout 3
}
}
}

#Load Balance Port 443 – Standard SSL Web Port
virtual_server 192.168.0.110 443 {
delay_loop 15
lb_algo wlc
lb_kind DR
persistence_timeout 50
protocol TCP

real_server 192.168.0.111 443 {
weight 100
TCP_CHECK {
connect_timeout 3
}
}

real_server 192.168.0.112 443 {
weight 100
TCP_CHECK {
connect_timeout 3
}
}
}

8. Start keepalived on all load balancers using command “/etc/init.d/keepalived start”

 

You can then check the ipvsadm configuration and which load balancer has the VIP by using the below commands:
“ipvsadm”
“ipvsadm -lc”
“ip addr list” or “ip a s”

 

If you are load balancing a Windows server then you will need to do the following on the Windows real server:
1. Install the Windows Loopback and set IP to the VIP with a 255.255.255.255 subnet mask
2. Rename adapters to “lan” and “loopback” respectively
3. Run below commands in a command prompt:
netsh interface ipv4 set interface “lan” weakhostreceive=enabled
netsh interface ipv4 set interface “loopback” weakhostreceive=enabled
netsh interface ipv4 set interface “loopback” weakhostsend=enabled

 

Thanks goes out to the below websites that I used to collect information:
Ubuntu Manual for ipvsadm
gcharriere.com
www.linuxvirtualserver.org

comments: 0 »