Skip to content

Linux LVS Load Balancer Setup

This tutorial will explain how to create a software load balancer using the Linux kernel, LVS or IPVS 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

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

3) Run command: sysctl -p command to update sysctl

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

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

6) Create a keepalived.conf file in “/etc/keepalived/” directory (use command: vim /etc/keepalived/keepalived.conf) and populate with the below (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 ***Set to 50 on failover load balancer***
  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
    }
  }
}

7) Start keepalived on all load balancers using command:
/etc/init.d/keepalived start or service 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

Published inUbuntu

Be First to Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.