Installing HAProxy 2.5.5 from source in RedHat
In this blog post, we will show step by step on how to install HAPrxoy version 2.5.5 from sources downloaded from the HAProxy official website in the Redhat server. As you might already know, HAProxy is free and open-source software that provides high availability load balancers for both TCP and HTTP-based applications by spreading incoming requests across multiple servers. HAProxy is commonly used to bootstrap the performance of a server environment by distributing the workload across servers not only for web or applications but also databases.
In our scenario of conducting this experiment, we are actually using HAProxy as load balancers standing in front of our database cluster for routing transactions to read-only replicas. Follow the steps below to get the work done.
1. Download Source Code of HAProxy 2.5.5
You can click here to download or visit the HAProxy official website to download the version you want. In our case, we experiment with the 2.5.5 version. Next, using tar
command to extract source code.
tar vxf haproxy-2.5.5.tar.gz
2. Prerequisite
In order to install HAProxy 2.5.5 and start the service properly, we have to have these packages (gcc, pcre-devel, systemd-devel, zlib-devel)
pre-installed in RedHat. You can use the following command to check.
rpm -qa | egrep 'gcc|pcre|systemd|zlib'
If there is a missing package, you have to manually download it with the compatible version ex. systemd-libs-219-67.el7_7.1.x86_64
, we need to download systemd-devel-219-67.el7_7.1.x86_64.rpm
and then install using the below command
rpm -ivh systemd-devel-219-67.el7_7.1.x86_64.rpm
3. Compile
Navigate to the source code then extracted folder of HAProxy and then use the below command to compile
make -j $(nproc) TARGET=linux-glibc USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1
4. Installation
Then, using the below command to install HAProxy
make install
Prepare directory for HAProxy configuration and statistic collector.
cp /usr/local/sbin/haproxy /usr/sbin/haproxy
mkdir -p /var/lib/haproxy/
mkdir -p /etc/haproxy
5. HAProxy Configuration File
Next step, we will provide a sample HAProxy configuration file just for the sake of testing. However, you might have your own existing configuration file which can work as well.
- Create a new HAProxy config file using the below command
touch /etc/haproxy/haproxy.cfg
- Sample HAProxy config file
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1:514 local2
chroot /var/lib/haproxy
pidfile /run/haproxy.pid
maxconn 4000
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode tcp
log global
option tcplog
option dontlognull
option http-server-close
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend main *:5000
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
# use_backend static if url_static
# default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#backend app
# balance roundrobin
# 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
listen stats
bind *:9201
mode http
stats enable
stats hide-version
stats realm Haproxy\ Statistics
stats uri /
stats refresh 5s
stats show-node
- Then, we can create an empty file for the stats collector of HAProxy. This stats collector will record traffic of connection accessing through HAProxy and provide us an insight into what's going on in HAProxy.
touch /var/lib/haproxy/stats
5. Create HAProxy Service
Since we install HAProxy from the source, we have to create a system service in Redhat with creatable privilege users manually. Using the following command to create the service called haproxy
vi /etc/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=network.target
[Service]
#Environment=LD_LIBRARY_PATH=/usr/local/openssl-1.1.1c/lib/
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/haproxy.pid" "EXTRAOPTS=-S /run/haproxy-master.sock"
ExecStartPre=/usr/local/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecStart=/usr/local/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE $EXTRAOPTS
ExecReload=/usr/local/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always
SuccessExitStatus=143
Type=notify
[Install]
WantedBy=multi-user.target
Next, we can now enable, start, and check the service of installed HAProxy v2.5.5 using the below command
systemctl enable haproxy
systemctl start haproxy
systemctl status haproxy
Finally, now let's open the web browser and check the statistic page with port 9201 as we've config in haproxy.cfg.
We should see the screen below