How to Setup HAProxy as Load Balancer for Http on CentOS 8

How to Setup HAProxy as Load Balancer

SHORT INTRODUCTION

An SSL certificate for Secure Socket Layer is used to secure and secure communications between the browser and the server. An SSL certificate is also known as a digital certificate. An SSL connection protects sensitive data such as us credit card information and passwords that are validated during each visit. When you buy an SSL certificate from a trusted third party they will say that your SSL certificate is valid and legally used by its owners.

We need one Haproxy-server(192.168.33.25) & two client-server: client1(192.168.33.30) & client2(192.168.33.31), Make sure all are accessible

Login into both client servers – 192.168.33.30 & 192.168.33.31

Step 1: Install the Apache Server on both Client Servers :

[root@client ~]# yum install httpd -y

  Start & Enable their Services:

[root@client ~]# systemctl start httpd
[root@client ~]# systemctl enable httpd
[root@client ~]# systemctl status httpd

Now, need to create test pages that we will use later on to test out the HAProxy setup :

--- Client Server 1 ---
[root@client1 ~]# vim /var/www/html/index.html
< h1> This is Fosnix's Client Server 1 < /h1>
--- Client Server 2 ---
[root@client2 ~]# vim /var/www/html/index.html
< h1> This is Fosnix's Client Server 2 < /h1>

For Testing:  Open your browser and Type your Client’s Server-IP as shown below-

Login into a Haproxy server – 192.168.33.25 

Step 2: Installing and Configuring HAProxy Server on CentOS 8

[root@fosnix ~]# yum install haproxy -y

  Start & Enable their Services:

[root@fosnix ~]# systemctl start haproxy
[root@fosnix ~]# systemctl enable haproxy

Now install & configure rsyslog –

[root@fosnix ~]# yum install rsyslog -y
[root@fosnix ~]# vim /etc/rsyslog.conf

Search & uncomment the below lines:

$ModLoad imudp
$UDPServerRun 514

Add the below lines in the same file

# HAProxy Logging
local2.* /var/log/haproxy.log

Save & Exit

Restart the rsyslog service 

[root@fosnix ~]# systemctl restart rsyslog

Now, Configure Haproxy file

[root@fosnix ~]# vim /etc/haproxy/haproxy.cfg

Add the below lines in the file
In Stats auth fosnix:admin@123 replace fosnix with your username and admin@123 with your password

# HAProxy Load Balancer for Apache Web Server
frontend http-balancer
bind 192.168.33.25:80
default_backend web-servers
backend web-servers
mode http
balance roundrobin
stats enable
stats auth fosnix:admin@123
server client1 192.168.33.30:80 check
server client2 192.168.33.31:80 check

Now, Restart the Haproxy

[root@fosnix ~]# systemctl restart haproxy

If your firewall is on then allow the HTTP Services by the below commands:

[root@fosnix ~]# firewall-cmd --permanent --add-service=http
[root@fosnix ~]# firewall-cmd --permanent --add-service=https
[root@fosnix ~]# firewall-cmd --reload

Now,  Open your browser and Type your Haproxy Server-IP with port “:80”, eg – http://192.168.33.25:80 as shown below-

Browse http://YOUR-SERVER-IP/haproxy?stats
Enter your login-ID

Tags:

No responses yet

Leave a Reply

Your email address will not be published.

Latest Comments

No comments to show.