How To Install and configure Haproxy Load Balancer On Linux

How To Install and Configure Haproxy Load Balancer On Linux

I will show you the basics of how to install and configure haproxy load balancer for the apache web server. we can also check the config using the command available in haproxy. we will have two apache web server and one load balancer server.

Load Balancer IP: 192.168.185.142 
Web Server 1: 192.168.185.143
Web Server 2: 192.168.185.128

Install the package using below command

yum install haproxy -y
Installed:
haproxy.x86_64 0:1.5.18-9.el7

Dependency Updated:
openssl.x86_64 1:1.0.2k-19.el7 openssl-libs.x86_64 1:1.0.2k-19.el7

Check the version

[root@haproxy-master ~]# haproxy -version
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>

Make a copy of the default haproxy file.

[root@haproxy-master ~]# cd /etc/haproxy/
[root@haproxy-master haproxy]# ls
haproxy.cfg
[root@haproxy-master haproxy]# cp haproxy.cfg haproxy.cfg-back

Start and enable the service using the following commands.

[root@haproxy-master ~]# systemctl start haproxy
[root@haproxy-master ~]# systemctl enable haproxy
ln -s '/usr/lib/systemd/system/haproxy.service' '/etc/systemd/system/multi-user.target.wants/haproxy.service'
[root@haproxy-master ~]# systemctl is-active haproxy
active

Haproxy doesn’t create a log file. We will have to create it. You can follow this article to configure the log file.

Check Out: Configure HaProxy Server Log In Rsyslog On CentOS 7

Go to the vim /etc/rsyslog.conf and uncomment the below parameters.

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

$UDPServerAddress 127.0.0.1

# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514

Restart the service and create a haproxy.log file in /var/log/

Once you check the status of service and found the below error.

[root@haproxy-master log]# systemctl status haproxy 
haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled)
   Active: active (running) since Wed 2020-04-01 12:02:17 PDT; 2min 44s ago
 Main PID: 17588 (haproxy-systemd)
   CGroup: /system.slice/haproxy.service
           ├─17588 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
           ├─17589 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
           └─17590 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds

Apr 01 12:04:42 haproxy-master python[17592]: SELinux is preventing /usr/sbin/haproxy from name_connect access on the tcp_socket port 5002.

you have to set an SELinux boolean and if you don’t want it, you can disable SELinux. 

[root@haproxy-master log]# setsebool -P haproxy_connect_any 1
[root@haproxy-master log]# systemctl restart haproxy

Check the status again, the error won’t be showing.

[root@haproxy-master log]# systemctl status haproxy 
haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled)
Active: active (running) since Wed 2020-04-01 12:08:14 PDT; 9s ago
Main PID: 18660 (haproxy-systemd)
CGroup: /system.slice/haproxy.service
├─18660 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
├─18661 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
└─18662 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds

Apr 01 12:08:14 haproxy-master systemd[1]: Started HAProxy Load Balancer.
Apr 01 12:08:14 haproxy-master haproxy-systemd-wrapper[18660]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds

Next step, we will configure two apache web server. I’ve set up a web server.

Check Out:  Copy Database To Another Database Using SQLYog On Ubuntu

webserver 1 output:

[root@memcached ~]# curl 192.168.185.143
testing first website

webserver 2 output:

[root@redhat html]# curl 192.168.185.128
testing website second

Now I will create a separate file for haproxy web server. 

[root@haproxy-master haproxy]# vim ha_web.cfg

Verify the configuration settings before restarting the haproxy service.

[root@haproxy-master haproxy]# haproxy -c -f /etc/haproxy/haproxy.cfg
Configuration file is valid

Now configure the haproxy for a webserver.

[root@haproxy-master haproxy]# vim haproxy.cfg
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 local2

chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
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 http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
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 
## acl url_static path_beg -i /static /images /javascript /stylesheets
## acl url_static path_end -i .jpg .gif .png .css .js
bind *:80

## use_backend static if url_static
default_backend web_server

#---------------------------------------------------------------------
# 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 web_server
balance roundrobin
mode http
server redhat.example.com 192.168.185.128:80 check
server memcached 192.168.185.143:80 check

Check Out:  How To Install and Configure MongoDB On Ubuntu

we will hit the load balancer IP and it should go to the webserver 1 and 2 to the server the website with load balancing using round-robin. 

[root@haproxy-master ~]# curl 192.168.185.142
testing first website
[root@haproxy-master ~]# curl 192.168.185.142
testing website second

We can change the timeout values in the configuration file. if we don’t define the bind *:80 and you try to access the website using the load balancer IP. It won’t work. 

You’re done haproxy configure for apache install check config linux

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment