HAProxy basic configuration on Ubuntu 14.04
HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for very high traffic web sites. Over the years it has become the de-facto standard opensource software load balancer, is now shipped with most mainstream Linux distributions, and is often deployed by default in cloud platforms.
This post describes the steps used to install and deploy a basic configuration of HAProxy on Ubuntu 14.04.
The architecture is composed by three ubuntu virtual machine:
- Hostname: node1 IP Address: 172.17.0.101
Description: Server used to install and to run haproxy - Hostname: node2 IP Address: 172.17.0.102
Description: Server running already apache on port 80 - Hostname: node3 IP Address: 172.17.0.103
Description: Server running already apache on port 80
The haproxy software on server node1 will be used to balance the apache service installed on node1 and node2.
- Install haproxy software
- Enable haproxy init.d script
For default haproxy has been disabled. Enable HAProxy editing /etc/default/haproxy and setting ENABLED variable to 1
- Add the first frontend and backend in haproxy configuration
Edit the haproxy configuration file ( /etc/haproxy/haproxy.cfg ) and add the first frontend and backend sections.
These two sections showed below allow to balance each request coming to 172.17.0.101 port 80 to the ports 80 of the two web servers node2 and node3 (ip addresses 172.17.0.101 172.17.0.102).
The option “check” in the last two lines is used to verify using a tcp connection that each web server is available. If one of backend server is offline or doesn’t respond to tcp connection on port 80, haproxy redirects all the requests on the other webserver.
The balancing method used by default in haproxy is round robin.
- Start haproxy and enable starting on boot
Start haproxy service
Enable haproxy start on boot
- Log haproxy location
The default location of haproxy.log is /var/log/haproxy.log
- Test haproxy configuration
You can use curl or access to the url http://172.17.0.101/ using a browser
- Enable HAproxy stats HAproxy provides a simple web interface useful to show the status and statistics about the frontend/backend configured in haproxy and their connections.
To enable it add the following content in /etc/haproxy/haproxy.cfg and reload the service :
View haproxy statistics using the url http://172.17.0.101:1936/haproxy?stats
- Configure other frontend/backend with different balancing algorithms
An instance of haproxy can be configured to balance multiple services configuring multiple frontend and backend sections in its configuration file.
HAproxy allows also to define different balancing algorithms, it use round robin as default but it support other algorithms that can be selected using the option balance in backend section:
- source: This method selects which server to use based on a hash of the source IP i.e. your user’s IP address. This is one method to ensure that a user will connect to the same server
- leastconn: This method selects the server with the least number of connections. It’s recommended for longer sessions. Servers in the same backend are also rotated in a round-robin fashion.
The following configuration can be added to haproxy configuration file in order to add two other balanced service listening respectively on port 81 and 82 using leastconn and source balancing method: