July Release Confetti
150+ New Courses, Hands‑On Labs, And
Interactive Learning Activities
Learn More

Installation of HAProxy

INTRODUCTION

Welcome to part two in a series of guides on HAProxy. In the first installment in the series, you were introduced to HAProxy, its terminology and a few ways in which it can be used. If you are not familiar with HAProxy and it uses, please take this time to review the first guide before jumping into this one.

In this guide, you will be shown a few ways in which HAProxy can be installed on different distributions as well as some of the more specific methods for doing so.

By the end of this guide, you should be comfortable installing HAProxy regardless of the host OS or method of your choice.


Installation Methods

As with most software on Linux, you have a few methods for installing software and with HAProxy, this is no different. The two methods for installation are going to be through your distributions package manager or compiling from source yourself. Each of these options has its benefit’s and its challenge’s and your choice will ultimately depend on your needs or use case. Benefit’s and challenges aside, lets jump into the actual workflows for each process below.

While HAProxy can be installed on a number of systems, we will be focusing on CentOS.


Package Manager

Because your package manager handles all the dependencies for HAProxy, we simply need to specify which package we want to install. As you will see during the source build, you will be required to install a number of dependencies manually but your package manager will automatically search for and install the required dependencies that are required for HAProxy. However, we can install HAProxy with the following command. As you can see, the version of HAProxy that is currently in the base rep for CentOS is 1.5.14 which is a few releases behind the most current release on the HAProxy site. This is the major downside to installing HAProxy using your package manager in that the releases are likely to be a few behind.


[root@ip-10-0-2-113 ~]# yum install haproxy
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.cogentco.com
* extras: repo1.ash.innoscale.net
* updates: centos.mirror.nac.net
Resolving Dependencies
--> Running transaction check
---> Package haproxy.x86_64 0:1.5.14-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
haproxy x86_64 1.5.14-3.el7 base 833 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 833 k
Installed size: 2.6 M
Is this ok [y/d/N]:

Once it is finished, you can then proceed with building your configuration file in /etc/haproxy/haproxy.cfg.

Because we installed using the package manager, you can also rely on it to handle updates for HAProxy. To check for updates, you can simply run the below commands to determine if a newer version is available.


[root@ip-10-0-2-113 ~]# yum update haproxy
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.cogentco.com
* extras: repo1.ash.innoscale.net
* updates: centos.mirror.nac.net
No packages marked for update

Because you are relying on your package manager, you simply need to check for updates and apply them as they come available.


Building from Source

As mentioned previously, building HAProxy from source requires more oversight and time but the benefits greatly outweigh those challenges. Because we can tailor the build to our exact needs, we can increase performance substantially.

HAProxy comes with two files that you will want to consult before building the source. Those are the Makefile as well as the README. Both of these files provide a plethora of information regarding the numerous options available to use when compiling HAProxy. Because there are an enormous number of options, we will simply review a small number of the more common ones you should expect to use on every installation.

We will review the options involved with HAProxy followed by the actual steps to do so.


Dependencies

Before you attempt to build HAProxy from source, there are a number of dependencies that you should install. The only required dependency is GNU make while everything else is optional. Reading the README file states,

“To build haproxy, you will need:

- GNU make. Neither Solaris nor OpenBSD's make work with the GNU Makefile. If you get many syntax errors when running "make", you may want to retry with "gmake" which is the name commonly used for GNU make on BSD systems.”

As mentioned, any other dependencies you may need will depend on what options you wish to build HAProxy with. The developers state which options they recommend to build with which will be highlighted later. For now, know that you will need OpenSSL, ZLIB as well as PCRE installed.

With the dependencies out of the way, the next thing to determine is which options you need for your specific use. Let’s take a look at the most common ones you should expect to use.


Options
TARGET - Required

To build HAProxy, you have to choose your target OS from the following list.

•linux22 for Linux 2.2

•linux24 for Linux 2.4 and above (default)

•linux24e for Linux 2.4 with support for a working epoll (> 0.21)

•linux26 for Linux 2.6 and above

•linux2628 for Linux 2.6.28, 3.x, and above (enables splice and tproxy)

•solaris for Solaris 8 or 10 (others untested)

•freebsd for FreeBSD 5 to 10 (others untested)

•netbsd for NetBSD

•osx for Mac OS/X

•openbsd for OpenBSD 3.1 and above

•aix51 for AIX 5.1

•aix52 for AIX 5.2

•cygwin for Cygwin

•generic for any other OS or version.

•custom to manually adjust every setting


CPU – Recommended

You have the option of choosing your CPU type to benefit from some optimizations. For this, you can assign one of the following choices to the CPU variable.

•i686 for intel PentiumPro, Pentium 2 and above, AMD Athlon

•i586 for intel Pentium, AMD K6, VIA C3.

•ultrasparc : Sun UltraSparc I/II/III/IV processor

•native : use the build machine's specific processor optimizations.

•generic : any other processor or no CPU-specific optimization. (default)


USE_PCRE - Recommended

If you wish to enable HAProxy to process HTTP headers, then you will want to use the option USE_PCRE which enables you to use ACL’s to intelligently monitor traffic and perform different operations based upon rules you specify. Because this option relies on PCRE (Perl Compatible Regular Expressions), you will need to ensure it is installed as a dependency.

The developers state in the README “If your system supports PCRE (Perl Compatible Regular Expressions), then it is recommended that you build with libpcre which is between 2 and 10 times faster than other libc implementations.”


USE_OPENSSL - Recommended

To help secure your users traffic, the ability to add native SSL support to HAProxy can be done by using the USE_OPENSSL option which will automatically link libssl and libcrypto with HAProxy. Because you installed OPENSSL as a dependency, you should have no issues using this option during make with USE_OPENSSL=1.


USE_ZLIB - Recommended

To help improve bandwidth utilization and speeds, you can build HAProxy to natively support HTTP compression with the USE_ZLIB option. Because you installed ZLIB as a dependency, you should have no issues using this option during make with USE_ZLIB=1.


USE_LIBCRYPT

This option will enable HAProxy to use encrypted passwords and hashes using the crypt(3) function.

USE_LIBCRYPT
Building

Now that you are familiar with a few of the more common options associated with HAProxy, it is time to walk through the actual execution of the steps involved in compiling from source.

1.Install dependencies

yum install openssl-devel pcre-devel zlib-devel

2.Grab the source code

wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.9.tar.gz

3.Extract the source

tar zxvf haproxy-1.6.9.tar.gz

4.Navigate into extracted folder

cd haproxy-1.6.9

5.Compile source

make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_LIBCRYPT=1

6.Install HAProxy

make install

7.Copy the binaries to their respective folders

cp -R /usr/local/sbin/haproxy /usr/sbin

8.Copy init file to /etc/init.d/haproxy

cp -R examples/haproxy.init /etc/init.d/haproxy

9.Make the init file executable

sudo chmod +x /etc/init.d/haproxy

10.Create directory for configuration file

mkdir -p /etc/haproxy

11.Create the configuration file and populate it with the below information making sure to replace the variables with your machine information.

nano /etc/haproxy/haproxy.cfg

global
log 127.0.0.1 local0
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option forwardfor
option http-server-close
frontend http-in
bind *:80
default_backend appZ-backend
backend appZ-backend
balance roundrobin
server appZ_01 IP_OF_MACHINE_01:8080 check
server appZ_02 IP_OF_MACHINE_02:8080 check

12.Restart the HAProxy service to pick up the config file changes.

service haproxy restart

Conclusion

As you should now see, the process for installing HAProxy is fairly straight forward regardless of the method you choose. With that said, you should keep in mind the below points when making the decision of which method you wish to pursue.


Package Manager:

•Easier installation

•Easier upgrades & patches

•Newest versions take longer to make their way into repo


Compiling from Source

•Newest versions can be installed as soon as they come out

•You can install multiple versions at the same time

•Tailored to your needs

•More knowledge of environment is required


Additional Resources

The following resources may help with additional understanding of the ways in which HAProxy can be configured and used.

HAProxy Documentation - http://www.haproxy.org/#docs



  • post-author-pic
    Michelle G
    12-21-2016

    Thanks for doing this, Michael!

  • post-author-pic
    Johnny J
    12-21-2016

     @mhatcher: Great guide!

  • post-author-pic
    Dele A
    06-16-2017

    thank you!

  • post-author-pic
    mahaque
    01-09-2018

    Well concise document !!

Looking For Team Training?

Learn More