HAProxy is a reliable and high performance load balancer for TCP and HTTP(S) based applications.
There are several detailed tutorials around the web on how to install HAProxy. As is usual of my “Tech Recipe” posts, I’ve boiled those instructions down to the quickest way to have HAProxy installed and running on your Ubuntu machine. You will have a running and configured version of HAProxy 1.5 running in under 2 minutes, and ready to go into production.
The latest version of HAProxy is 1.5.9 (as of Jan 10th, 2015). On Ubuntu 12.04, HAProxy is on v1.2, and on 14.04 it’s on version 1.4. To install version 1.5 you need to add the HAProxy PPA. Installation involves the following steps :
- Adding the PPA
- Installing HAProxy via
- Enabling the service in
- Starting it up!
Run the following in your shell :
sudo add-apt-repository ppa:vbernat/haproxy-1.5; sudo apt-get update; sudo apt-get -y install haproxy; sed -i s/ENABLED=0/ENABLED=1/ /etc/default/haproxy; sudo service haproxy start; ps -auxxw | grep haproxy
If you can see haproxy as the last line of output, it’s on.
Adding a MySQL cluster
Bonus! Let’s add MySQL to HAProxy. There’s loads of tutorials for these around. Again, I’ve boiled down the instructions to the following :
- Setup some MySQL servers in a Master-Slave setup or a Galera Cluster. We will not be discussing these here.
- Add MySQL to
- Add a user to each MySQL server that HAProxy proxies to
Assuming that you have a couple of MySQL servers running, edit the
/etc/haproxy/haproxy.cfg file and add the following details :
listen mysql # Bind HAProxy to your mysql's host bind 127.0.0.1:3306 mode tcp option tcplog # HAProxy will connect to MySQL using the 'haproxy_check' user. It doesn't need a pass. option mysql-check user haproxy_check balance source # Add your MySQL server cluster here. server db01 192.168.0.1:3306 check server db02 192.168.0.2:3306 check
This binds HAProxy to listen on
127.0.0.1:3306 in TCP mode. Any connections to this port are routed to one of the
server db0* backends (remember to update the IPs!). The ‘
mysql-check‘ option is described as follows :
If you specify a username, the check consists of sending two MySQL packet, one Client Authentication packet, and one QUIT packet, to correctly close MySQL session. We then parse the MySQL Handshake Initialisation packet and/or Error packet. It is a basic but useful test which does not produce error nor aborted connect on the server. However, it requires adding an authorization in the MySQL table.
Let’s combine this into copy-pasteable form.
MYSQL_HOST1="192.168.0.1" # Update the IPs! MYSQL_HOST2="192.168.0.2" MYSQL=" listen mysql bind 127.0.0.1:3306 mode tcp option tcplog option mysql-check user haproxy_check balance source server db01 $MYSQL_HOST1:3306 check server db02 $MYSQL_HOST2:3306 check"; sudo sh -c "echo '$MYSQL' >> /etc/haproxy/haproxy.cfg"; mysql $MYSQL_HOST1 -u root -p mysql < 'INSERT INTO user (Host,User) values ('%','haproxy_check'); FLUSH PRIVILEGES;' mysql $MYSQL_HOST2 -u root -p mysql < 'INSERT INTO user (Host,User) values ('%','haproxy_check'); FLUSH PRIVILEGES;' sudo service haproxy restart;
You should now have HAProxy routing requests to MySQL servers on
Adding the Stats module
Now that HAProxy is kicking, you want to be able to see some statistics on how it is performing, in realtime! HAProxy comes with a built-in module for this purpose. It shows you live sessions across all your backends. You’ll need to configure it to be able to see the stats. Open the
/etc/haproxy/haproxy.cfg file and add the following :
listen stats 0.0.0.0:8081 stats enable stats uri /haproxy?stats stats realm Strictly\ Private stats auth admin:password
Make sure you :
- Adjust the host and port (0.0.0.0:8081) to something more secure.
- Update the authentication details!
Here it is in copy paste form :
STATS=" listen stats 0.0.0.0:8081 stats enable stats uri /haproxy?stats stats realm Strictly\ Private stats auth admin:password"; sudo sh -c "echo '$STATS' >> /etc/haproxy/haproxy.cfg"; sudo service haproxy restart;
Now check this URL in your browser :
In this post we quickly setup HAProxy 1.5 on Ubuntu and setup MySQL load-balancing. We also loaded up the
stats module so we can see routing stats in realtime. Read up on the HAProxy documentation here : http://cbonte.github.io/haproxy-dconv/configuration-1.5.html