Where can I find actual haproxy and keepalived deb packages for Raspberry Pi and how I install high availability?

At https://packages.raspberry-hosting.com/ we have provided for download the latest version of the HAProxy for Raspberry Pi. There is also a init script. Packages will be updated regularly.

Raspberry HAProxy

What you need:

4x Raspberry Pi
5x IP address (1 IP is need for Virtual Interface - VIF)

VIF - 192.168.1.1
rpi-haproxy01 (master) 192.168.1.2
rpi-haproxy02 (slave) 192.168.1.3
rpi-web01 (webserver 01) 192.168.1.4
rpi-web02 (webserver 02) 192.168.1.5

Installation HAProxy:

1. Update/upgrade

root@haproxy01:~# apt-get update && apt-get -y upgrade

2. Download and install the HAProxy deb package from the following address: (Check on https://packages.raspberry-hosting.com/ for last version)

root@haproxy01:~# wget https://packages.raspberry-hosting.com/haproxy-raspberry-pi/haproxy_1.5.9-1_armhf.deb
install:
root@haproxy01:~# dpkg -i ./haproxy_1.5.9-1_armhf.deb

2. Add BEST-HOSTING repository with all packages: (Read more: FREE Raspbian repository )

root@haproxy01:~# apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0x908332071dd2e32e
root@haproxy01:~# cat > /etc/apt/sources.list.d/best-hosting.list
deb http://deb.best-hosting.cz/debian/ raspberry main
root@haproxy01:~# apt-get update
root@haproxy01:~# apt-cache search bh-
root@haproxy01:~# apt-get install bh-haproxy

3. Download HAProxy init script in /etc/init.d/ and add it permissions to execute:

root@haproxy01:~# wget https://packages.raspberry-hosting.com/haproxy-raspberry-pi/etc/init.d/haproxy -O /etc/init.d/haproxy
root@haproxy01:~# chmod +x /etc/init.d/haproxy

root@haproxy01:~# wget http://deb.best-hosting.cz/_scripts_/haproxy/init.d/haproxy -O /etc/init.d/haproxy
root@haproxy01:~# chmod +x /etc/init.d/haproxy

4. Allowing execution HAProxy even after reboot:

root@haproxy01:~# update-rc.d haproxy defaults

6. Create the following folder:

root@haproxy01:~# mkdir /etc/haproxy/

7. Download and create basic config file:

root@haproxy01:~# wget https://packages.raspberry-hosting.com/haproxy-raspberry-pi/etc/haproxy/haproxy.cfg -O /etc/haproxy/haproxy.cfg

root@haproxy01:~# wget http://deb.best-hosting.cz/_scripts_/haproxy/haproxy.cfg -O /etc/haproxy/haproxy.cfg

8. Create haproxy user and group:

root@haproxy01:~# adduser --group --system --no-create-home --disabled-password --disabled-login haproxy
Adding system user haproxy (UID 107) ...
Adding new group haproxy (GID 110) ...
Adding new user haproxy (UID 107) with group haproxy ...
Not creating home directory /home/haproxy

9. Check haproxy congfiguration in /etc/haproxy/haproxy.cfg and change IP address to your. This config file is will be same on both haproxy devices

Change x.x.x.x to your IP address ( in this example: 192.168.1.4 and 192.168.1.5 )

10. Run HAProxy and test is running:

root@haproxy01:~# /etc/init.d/haproxy restart
Checking HAProxy configuration first
Configuration file is valid
Everything looks fine
[ ok ] Restarting haproxy: haproxy.
root@haproxy01:~# ps ax | grep haproxy
 2093 ?        Ss     0:00 /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D -p /var/run/haproxy.pid
 2112 pts/0    S+     0:00 grep haproxy

11. Repeat step 1 to 10 on haproxy02

HAProxy statistics report:

For haproxy01: http://192.168.1.2:8880/
For haproxy02: http://192.168.1.3:8880/
login: pi
password: raspberry

Well done!


Installation Keepalived:

1. Install the keepalived deb package:

root@haproxy01:~# wget https://packages.raspberry-hosting.com/keepalived-raspberry-pi/keepalived_1.2.13-1_armhf.deb
root@haproxy01:~# dpkg -i ./keepalived_1.2.13-1_armhf.deb

root@haproxy01:~# apt-get install bh-keepalived

2. Check keepalived version:

root@haproxy01:~# keepalived -v
Keepalived v1.2.x (xx/xx,xxxx)

3. Download keepalived init script in /etc/init.d/ and add it permissions to execute:

root@haproxy01:~# wget https://packages.raspberry-hosting.com/keepalived-raspberry-pi/etc/init.d/keepalived -O /etc/init.d/keepalived
root@haproxy01:~# chmod +x /etc/init.d/keepalived

root@haproxy01:~# wget http://deb.best-hosting.cz/_scripts_/keepalived/init.d/keepalived -O /etc/init.d/keepalived
root@haproxy01:~# chmod +x /etc/init.d/keepalived

4. Allowing execution keepalived even after reboot:

root@haproxy01:~# update-rc.d keepalived defaults

5. Start keepalived a check is running:

root@haproxy01:~# /etc/init.d/keepalived start
[ ok ] Starting keepalived: keepalived.
root@haproxy01:~# ps ax | grep keep
 6883 ?        Ss     0:00 /usr/local/sbin/keepalived
 6885 ?        S      0:00 /usr/local/sbin/keepalived
 6886 ?        S      0:00 /usr/local/sbin/keepalived
 6908 pts/0    S+     0:00 grep keep

6. Repeat step 1 to 5 on haproxy02

7. Download config file for keepalived and save it to /etc/keepalived/keepalived.conf

root@haproxy01:~#  wget https://packages.raspberry-hosting.com/keepalived-raspberry-pi/etc/keepalived/keepalived.conf -O  /etc/keepalived/keepalived.conf

root@haproxy01:~#  wget http://deb.best-hosting.cz/_scripts_/keepalived/keepalived.conf -O  /etc/keepalived/keepalived.conf

8. Config for haproxy01:

global_defs {
    lvs_id haproxy01
}
vrrp_sync_group SyncGroup01 {
    group {
        VI_1
    }
}
vrrp_script chkhaproxy {
    script "/usr/bin/killall -0 haproxy"
    script "/usr/sbin/service haproxy start"
    interval 9
    timeout 3
    weight 20
    rise 2
    fall 4
}
vrrp_instance VI_1 {
    interface eth0                # interface to monitor
    state MASTER
    virtual_router_id 51          # Assign one ID for this route
    priority 101                  # 101 on MASTER, 100 on BACKUP
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass 6c7448fdece8f61841c60293f9a8bd3c
    }
    virtual_ipaddress {
        192.168.1.1         # the virtual IP
    }
    track_script {
        chkhaproxy
    }
}

9. Config for haproxy02:

global_defs {
    lvs_id haproxy02
}
vrrp_sync_group SyncGroup01 {
    group {
        VI_1
    }
}
vrrp_script chkhaproxy {
    script "/usr/bin/killall -0 haproxy"
    script "/usr/sbin/service haproxy start"
    interval 9
    timeout 3
    weight 20
    rise 2
    fall 4
}
vrrp_instance VI_1 {
    interface eth0                # interface to monitor
    state BACKUP
    virtual_router_id 51          # Assign one ID for this route
    priority 100                  # 101 on MASTER, 100 on BACKUP
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass 6c7448fdece8f61841c60293f9a8bd3c
    }
    virtual_ipaddress {
        192.168.1.1         # the virtual IP
    }
    track_script {
        chkhaproxy
    }
}

10. Restart both keepalived on haproxy devices:

root@haproxy01:~# /etc/init.d/keepalived restart
and
root@haproxy02:~# /etc/init.d/keepalived restart

11. Check VIF IP address:

root@haproxy01:~# ip a sh eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether b8:27:eb:fb:e2:7e brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 brd 193.150.14.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.1.1/32 scope global eth0
       valid_lft forever preferred_lft forever
root@haproxy02:~# ip a sh eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether b8:27:eb:6e:03:ad brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.3/24 brd 193.150.14.255 scope global eth0
       valid_lft forever preferred_lft forever

12. Test stop keepalived on haproxy01:

root@haproxy01:~# /etc/init.d/keepalived stop
[ ok ] Stopping keepalived: keepalived.
root@haproxy01:~# ip a sh eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether b8:27:eb:fb:e2:7e brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 brd 193.150.14.255 scope global eth0
       valid_lft forever preferred_lft forever
root@haproxy02:~# ip a sh eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether b8:27:eb:6e:03:ad brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.3/24 brd 193.150.14.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.1.1/32 scope global eth0
       valid_lft forever preferred_lft forever

Nice. You have done. Enjoy!

Don't remember! When you update package to new version, be very careful and keep a back-up of all configuration files from /etc/haproxy/ and /etc/keepalived !

Sources:
FREE Raspbian repository
HAPROXY Manual 1.5
KeepAlived
Raspberry Pi Packages

Comments

Hello,

First, Thank you for this tutorial which helped me a lot.

I have implemented the same architecture using 2 redhat7 servers.
When testing keepalived service crash; the failover to slave server works fine, the VIP is assigned the slave server which is then declared as newly primary server.
That is not the case when crashing Haproxy service. Failover procedure is not triggered and users requests are still routed to the primary server, which is obviously not responding.
I think vrrp_script is not working correctly on redhat 7 as killall command does not exist in this version. Is there any other alternatives i can use ?

Hello,
you are welcome. We've tested it on Debian and Raspbian only. The problem should be in service identification, which may differs at RedHat. 

vrrp_script chkhaproxy {
    script "/usr/bin/killall -0 haproxy"
    script "/usr/sbin/service haproxy start"

    interval 9
    timeout 3
    weight 20
    rise 2
    fall 4
}