Cloud Computing‎ > ‎Load Balancing‎ > ‎HAProxy‎ > ‎

Setup


Overview

HAProxy is a free and open-source Linux application used for load balancing network traffic. This tutorial will guide you through deploying it for both simple web applications and large, complex web sites.

Load Balancing Concepts

Depending on the complexity of your needs, balancing can be accomplished many ways using different techniques. One of the first things you’ll need to identify is do use layer 4 or layer 7 balancing. Each is a solution for different needs, so it’s very important to understand your actually requirements before making a decision on a load balancer configuration.

Layer 4

The higher you are on the network stack layer, the faster you will be able to process requests. The caveat is that the extra performance gain comes at the cost of feature and ability loss. Ignoring features you may find in advanced enterprise-ready load balancers, you can’t beat the performance of a layer 4 balancing. It works by modifying the destination IP and MAC address part of every packet’s header to forward it to one of the available nodes. This approach is accomplished by using very little processing.



Layer 4 web server load balancing

The biggest downside to this method of balancing is your nodes must host every component of your application (PHP, Java, CSS, Javscript, IMGs, etc), and the application files on each node must be the exact same versions. Otherwise, you’re users’ experience will differ each time they access your website.

Layer 4 balancing is ideal for small websites or those with few complexities.

Layer 7

Layer 7 balancing may require more horsepower to process, but what it offers in return for large and complex websites is worth the extra CPU time. This type of balancing allows you to separate every component onto it’s own server, and it does so transparently to the user. Another benefit is being able to move heavily accessed areas of your website onto separate servers. For example, maybe you’re hosting a news website that also has forums to allow users to discuss things. Given enough popularity, the forum may cause other areas of your website to be sluggish or become inaccessible. Using layer 7, you could separate your forum onto its own server or server group to allow you to scale it out.



Layer 7 load balancing

Layer 7 works by analyzing the application request part of every packet, and then matching it against a set of policies or rules. For a web server, it looks at the HTTP request to identify the action and destination URI.  Using our news website with a forum example above, if an HTTP GET or POST request is submitted starting with /forum, the traffic will be routed to the servers hosting the forum application, allevating stress from the news posting part of the site.

 

Installing HAProxy

HAProxy isn’t available in the default repositories for CentOS or Red Hat. In order for us to be able to install it, we need to either compile it from source (preferred) or add the EPEL repository to our server and install it using Yum.


Install epel repository

sudo yum -y install epel-release

Refresh repolist

sudo yum repolist


To list all available packages under a repo called epel, enter:

$ sudo yum --disablerepo="*" --enablerepo="epel" list available


References

http://www.tecmint.com/how-to-enable-epel-repository-for-rhel-centos-6-5/

http://www.cyberciti.biz/faq/installing-rhel-epel-repo-on-centos-redhat-7-x/


Install HAProxy

yum install -y haproxy

sudo service haproxy status | start | stop

sudo haproxy -v
HA-Proxy version 1.5.4
Copyright 2000-2014 Willy Tarreau <w@1wt.eu>


Add HAProxy config with above 2 servers as backend

Config - https://www.upcloud.com/support/haproxy-load-balancer-centos/

sudo vi /etc/haproxy/haproxy.cfg

backend app
balance roundrobin
server serv-1 1.2.3.4:8080 check
server serv-2 1.2.3.5:8080 check
# server app1 127.0.0.1:5001 check
# server app2 127.0.0.1:5002 check
# server app3 127.0.0.1:5003 check
# server app4 127.0.0.1:5004 check


Validate HAProxy

http://1.2.3.6:5000/whoami.html


Add Stats

sudo vi /etc/haproxy/haproxy.cfg

listen stats
bind *:8181
stats enable
stats uri /
stats realm Haproxy\ Statistics
stats auth cloud:cloud

$ service haproxy restart
Access the HAProxy Stats URL
http://1.2.3.6:8181/

This shows the stats page



References

http://louwrentius.com/how-to-compile-haproxy-from-source-and-setup-a-basic-configuration.html

http://www.serverlab.ca/tutorials/linux/network-services/compiling-and-running-haproxy-from-source-on-ubuntu-14/

https://github.com/joyent/haproxy-1.4/blob/master/Makefile

http://www.linux-admins.net/2014/09/deploying-haproxy-15-from-source.html

http://stackoverflow.com/questions/25520526/centos-6-5-haproxy-fatal-error

http://www.serverlab.ca/tutorials/linux/network-services/compiling-and-running-haproxy-from-source-on-ubuntu-14/

http://serverfault.com/questions/464166/should-i-use-haproxys-linux2628-or-linux26-make-target

Install openssl https://sandilands.info/sgordon/upgrade-latest-version-openssl-on-ubuntu 


Comments