Load balancing is a common solution for distributing web applications horizontally across multiple hosts while providing the users a single point of access to the service. It aims to optimize resource usage, maximize throughput, minimize response time, and avoid overloading any single resource.
HAProxy is one of the most popular opensource load balancing software, which also offers high availability and proxy functionality. It’s available for install on many Linux distributions as well as Solaris and FreeBSD.
HAProxy is particularly suited for very high traffic websites, and is therefore often used to improve web service reliability and performance for multi-server configurations. This guide lays out the steps for setting up HAProxy as a load balancer on CentOS 7 to its own cloud host which then directs the traffic to your web servers.
As a pre-requirement for following this guide, you’ll need to have a minimum of two servers with at least the basic web service such as Apache2 or httpd installed and running, and a third load balancer server you’ll be installing HAProxy on.
Installing HAProxy 1.6
As a fast developing opensource application HAProxy available for install in the CentOS default repositories might not be of the latest release. To find out what version number is being offered through the official channels enter the following command
sudo yum info haproxy
HAProxy has always three active stable versions of the releases, two of the latest versions in development plus a third older version that is still receiving critical updates. You can always check the currently newest stable version listed on HAProxy website and then decide which version you wish to go with.
In this guide we’ll be installing the currently latest stable version of 1.6, which was not yet available in the standard repositories. Instead you’ll need to install it from the source, but before this check that you have the prerequisites to download and compile the program.
sudo yum install wget gcc pcre-static pcre-devel -y
Download the source code with the command below. You can check if there’s a newer version available at the
HAProxy download page and then replace the download link in the
wgetcommand with the latest.
wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.3.tar.gz -O ~/haproxy.tar.gz
Once the download is complete, extract the files using the following
tar xzvf ~/haproxy.tar.gz -C ~/
Change into the directory.
cd ~/haproxy-1.6.3
Then compile the program for your system.
make TARGET=linux2628
And finally install HAProxy itself.
sudo make install
To complete the install, use the following commands to copy the settings over.
sudo cp /usr/local/sbin/haproxy /usr/sbin/
sudo cp ~/haproxy-1.6.3/examples/haproxy.init /etc/init.d/haproxy
sudo chmod 755 /etc/init.d/haproxy
Create these directories and the statistics file for HAProxy to record in.
sudo mkdir -p /etc/haproxy
sudo mkdir -p /run/haproxy
sudo mkdir -p /var/lib/haproxy
sudo touch /var/lib/haproxy/stats
Then add a new user for HAProxy.
sudo useradd -r haproxy
After the installation you can double check the installed version number with the following
sudo haproxy -v
HA-Proxy version 1.6.3 2015/12/25
Copyright 2000-2015 Willy Tarreau
In this case the version is 1.6.3 like shown in the example output above.
Nhận xét