We've just configured HAProxy to listen on the virtual IP address 192.168.0.99, but someone has to tell lb1 and lb2 that they should listen on that IP address. This is done by Wackamole and Spread which we install like this:
lb1/lb2:
apt-get install wackamole
To allow HAProxy to bind to the shared IP address, we add the following line to /etc/sysctl.conf:
vi /etc/sysctl.conf
[...]
net.ipv4.ip_nonlocal_bind=1
... and run:
sysctl -p
Next we modify /etc/default/spread and set ENABLED to 1:
vi /etc/default/spread
# Change to enable spread
ENABLED=1
# Options, see spread.1 for list
OPTIONS=""
The Spread configuration is located in /etc/spread/spread.conf. We create a backup of the original file and edit spread.conf as follows:
cp /etc/spread/spread.conf /etc/spread/spread.conf_orig
cat /dev/null > /etc/spread/spread.conf
vi /etc/spread/spread.conf
Spread can send broadcast or multicast messages, therefore you have two choices for configuring Spread.
Spread will feel free to use broadcast messages within a sub-network if you use broadcast messages. If IP-multicast is supported by the operating system, then the messages will only be received by those machines who are in the group and not by all others in the same sub-network as happens with broadcast addresses
Now we can start Spread:
/etc/init.d/spread start
On to the Wackamole configuration. Open /etc/default/wackamole and set ENABLED to 1:
vi /etc/default/wackamole
# Change to enable wackamole
ENABLED=1
# Options
OPTIONS=""
Then configure Wackamole as follows:
cp /etc/wackamole.conf /etc/wackamole.conf_orig
cat /dev/null > /etc/wackamole.conf
vi /etc/wackamole.conf
The VirtualInterfaces stanza is the most important part - it conatins our network interface (eth0 in this example) together with our virtual IP address (192.168.0.99).
The Notify stanza contains hosts (e.g. your router) or subnets to notify when the virtual IP switches. It is not necessary, so you can leave out that stanza, if you like.
(You can learn more about the Wackamole configuration by taking a look at
man 5 wackamole.conf
)
Finally we start Wackamole on both load balancers:
/etc/init.d/wackamole start
Then run:
ifconfig
on both load balancers. The outputs should be different - one load balancer should now own the virtual IP address, e.g. like this:
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:11 errors:0 dropped:0 overruns:0 frame:0
TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:824 (824.0 b) TX bytes:824 (824.0 b)
lb2:~#
6 Starting HAProxy
Now we can start HAProxy:
lb1/lb2:
/etc/init.d/haproxy start
7 Testing
Our high-availability load balancer is now up and running.
You can now make HTTP requests to the virtual IP address 192.168.0.99 (or to any domain/hostname that is pointing to the virtual IP address), and you should get content from the backend web servers.
You can test its high-availability/failover capabilities by switching off one backend web server - the load balancer should then redirect all requests to the remaining backend web server. Afterwards, switch off the active load balancer (lb1) or stop Wackamole on the active load balancer - lb2 should take over immediately. You can check that by running:
lb2:
ifconfig
You should now see the virtual IP address in the output on lb2:
You might have noticed that we have used the options stats enable and stats auth someuser:somepassword in the HAProxy configuration in chapter 4. This allow us to access (password-protected) HAProxy statistics under the URL http://192.168.0.99/haproxy?stats. This is how it looks:
If you don't need the statistics, just comment out or remove the stats lines from the HAProxy configuration.
Please do not use the comment function to ask for help! If you need help, please use our forum. Comments will be published after administrator approval.
Recent comments
15 hours 42 min ago
20 hours 47 min ago
1 day 1 hour ago
1 day 3 hours ago
1 day 17 hours ago
1 day 17 hours ago
1 day 22 hours ago
2 days 4 hours ago
2 days 5 hours ago
2 days 6 hours ago