Main Page

Wednesday, 20 January 2021

Install , configure and load balance HAProxy Server

 

HA Proxy Load Balance Setup

Step 1) Down and Install CentOS Machines . One Mache for HAProxy setup and other two machines for Apache installation. 

Use the Link for reference on how to setup CentOS Machines :

http://middlewareadmin-pavan.blogspot.com/2021/01/install-centos-machines-and-establish.html

Step 2) Install and configure Apache on Machine 1 

a) Login as a root and install apache using yum install httpd -y

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

b) Update the Listen port from 80 to 10001 in /etc/httpd/conf/httpd.conf

[root@machine1 ~]# grep "Listen 10001" /etc/httpd/conf/httpd.conf

Listen 10001

c) create a index.html file under document root directory

[root@machine1 ~]# echo "Apache is running on machine1" > /var/www/html/index.html

d) start apache instance using systemctl command

[root@machine1 ~]# systemctl start httpd

e) access the apache url by using curl command

[root@machine1 ~]# curl http://localhost:10001
Apache is running on machine1


Step 3) Install and configure Apache on Machine 2

a) Login as a root and install apache using yum install httpd -y

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

b) Update the Listen port from 80 to 10001 in /etc/httpd/conf/httpd.conf

[root@machine2 ~]# grep "Listen 10001" /etc/httpd/conf/httpd.conf

Listen 10001

c) create a index.html file under document root directory

[root@machine2 ~]# echo "Apache is running on machine2" > /var/www/html/index.html

d) start apache instance using systemctl command

[root@machine2 ~]# systemctl start httpd

e) access the apache url by using curl command

[root@machine2 ~]# curl http://localhost:10001
Apache is running on machine2


Step 4) Install and configure HAProxy on master machine

a) Login as root and run yum install haproxy -y command

[root@master ~]# yum install haproxy

b) Open /etc/haproxy/haproxy.cfg file and configure load balance changes

Change port number  from 5000 to 80 at frontend configurations

frontend main
    bind *:80

Change the backend app configuration as below

backend app
    balance     roundrobin
    server  machine1 192.168.1.196:10001 check
    server  machine2 192.168.1.197:10001 check

c) start haproxy instance

[root@master haproxy]# systemctl start haproxy

d) Test the loadbalance 

[root@master haproxy]# curl http://localhost
curl: (7) Failed to connect to 192.168.1.196 port 80: No route to host

if you get this error disable firewalld on all 3 machines

[root@master haproxy]# systemctl stop firewalld
[root@master haproxy]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@machine1 ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@machine1 ~]# systemctl stop firewalld

[root@machine2 ~]# systemctl stop firewalld
[root@machine2 ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

e) when you test the load balance url on the master then you should see the traffice is being routed to machine1 or machine2 using round robin algorithm 

[root@master haproxy]# curl http://master
Apache is running on machine2
[root@master haproxy]# curl http://master
Apache is running on machine1
[root@master haproxy]# curl http://master
Apache is running on machine2


Incase if you want to test this url on your browser then update your local DNS entry as follows and hit the url on the browser.





Access the url http://master on the browser and you should see the traffic is being routed to machine1 and machine 2. 






No comments:

Post a Comment