LVS and keepalived – An example

The purpose of the post is just show an example of a Linux box operating as a loadbalancer with LVS and keepalived together.

Briefing:

LVS, or Linux Virtual Server, is a feature of the Linux Kernel for loadbalance services in a Linux box. Check the official site for more.

keepalived is a routing software that implement VRRP in order to manage dynamic gateway and failure. Here you can read more.

The components:

  • Two Linux box with LVS and keepalived, one master and another slave, in case of failure of the master.
  • Two Webservers. I will assume you already have the webservers configured and working.
LVS
LVS overview

Configuration

LVS is a kernel feature, but in order to handle it, ipvsadm package is needed. In CentOS, you can resolve with:

yum install -y ipvsadm keepalived

keepalived is also shipped with most of the distros.

Once you installed both, ipvsadm and keepalive, lets configure them. Below follows the /etc/keepalived/keepalived.conf commented:

global_defs {
  router_id LVS_DEVEL
}

router_id is just a string to identify.

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        100.10.20.50
    }
}

Above is defined the instance of the virtual router. In the first machine it will have the state as MASTER. In the second machine, use SLAVE instead. Another parameter should be different in both, the virtual_router_id. The number does not matter, just keep it different. The priority will make sense when you have 2 slaves. It defines what SLAVE should become MASTER first.
The authentication section is needed to the keepalived servers trust each other.
The virtual_ipaddress is the IP that the clients will connect to.

virtual_server 100.10.20.50 80 {
    delay_loop 15
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 100.10.20.55 80 {
        HTTP_GET {
            path /
            status_code 200
        }
    }
    real_server 100.10.20.56 80 {
        HTTP_GET {
            path /
            status_code 200
        }
    }
}

The delay_loop defines the timer for polling service in seconds. lb_algo is round-robind. lb_kind is direct routing, no need tunneling or nat. persistence_timeout is the persistence time in seconds for LVS. The real_server sections define the webservers. Both have the HTTP_GET checker. keepalived will do a http get at the ‘/’ path and expect a 200 code. If it’s not ok, keepalived will disable forwarding till receive 200 status code.

As soon as keepalived is started, it will configure the VIP in the interface defined at vrrp_instance.

Now in the webservers, a dummy interface must be configured. So they can receive packages destinated to the VIP 100.10.20.50. First load the dummy module:

modprobe dummy

Create the dummy interface and configure it:

ip link add dummy0 type dummy
ip link set dummy0 up
ip addr add 100.10.20.50/32

Source based routing

Most of network routing is based on the destination. But sometimes you may need to forward packets to different gateways depending on the source.

In Linux you can do this using the iproute2 package. It uses netlink socket interface in order to handle addressment, routing, queuing and scheduling of Linux network subsystem. Follow an example:

Define a lable for a table to be used:

echo "10 foo" >> /etc/iproute2/rt_tables

Insert a route into foo table:

ip route add default via 10.10.10.1 dev eth1 table foo

Insert a rule with low priority in order to a host consult the new table foo:

ip rule add prio 10 from 192.168.16.7 lookup foo

You can check the rules with:

ip rule show list

Use the man for more information.