haproxy check config

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

[[email protected] ~]# haproxy -version
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <[email protected]>

Make a copy of the default haproxy file.

[[email protected] ~]# cd /etc/haproxy/
[[email protected] haproxy]# ls
haproxy.cfg
[[email protected] haproxy]# cp haproxy.cfg haproxy.cfg-back

Start and enable the service using the following commands.

[[email protected] ~]# systemctl start haproxy
[[email protected] ~]# systemctl enable haproxy
ln -s '/usr/lib/systemd/system/haproxy.service' '/etc/systemd/system/multi-user.target.wants/haproxy.service'
[[email protected] ~]# 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.

[[email protected] 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. 

[[email protected] log]# setsebool -P haproxy_connect_any 1
[[email protected] log]# systemctl restart haproxy

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

[[email protected] 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:

[[email protected] ~]# curl 192.168.185.143
testing first website

webserver 2 output:

[[email protected] html]# curl 192.168.185.128
testing website second

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

[[email protected] haproxy]# vim ha_web.cfg

Verify the configuration settings before restarting the haproxy service.

[[email protected] haproxy]# haproxy -c -f /etc/haproxy/haproxy.cfg
Configuration file is valid

Now configure the haproxy for a webserver.

[[email protected] 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. 

[[email protected] ~]# curl 192.168.185.142
testing first website
[[email protected] ~]# 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

LEAVE A REPLY

Please enter your comment!
Please enter your name here