PJ8-Load-Balancer-Solution-With-Apache

LOAD BALANCER

In this project https://github.com/Umeh-Johnbosco-Ifeanyi/Devops_Tooling_website_Solution, i developed a tooling solution for a Devops team comprising of 3 Webservers, 1 Database(mysql) and an NFS server availble to the web servers. In this project, we will be working on unifying the requests to the webservers whereby clients will be able to access all webservers using a single URL. This will be possible by using a load baalancer(Apache in this case) to distribute traffic to the webservers.

pere

The requirement for this project are:

Two RHEL8 Web Servers One MySQL DB Server (based on Ubuntu 20.04) One RHEL8 NFS server

perequisite

CONFIGURE APACHE AS A LOAD BALANCER

Create an Ubuntu Server 20.04 EC2 instance and name it Project-8-apache-lb:

instance

Open inbound rule port 80 for the instance so it would be accessible to traffic on this port

Install Apache:

sudo apt update
sudo apt install apache2 -y
sudo apt-get install libxml2-dev

aptupdate

libxml2

Enable these modules:

sudo a2enmod rewrite
sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod headers
sudo a2enmod lbmethod_bytraffic

aemod

#Restart apache2 service sudo systemctl restart apache2

Confirm apache2 is up and running

sudo systemctl status apache2

confirmapache

Configure load balancing. Open the following file and populate it with the code below

sudo vi /etc/apache2/sites-available/000-default.conf

#Add this configuration into this section <VirtualHost *:80>  </VirtualHost>

<Proxy "balancer://mycluster">
               BalancerMember http://<WebServer1-Private-IP-Address>:80 loadfactor=5 timeout=1
               BalancerMember http://<WebServer2-Private-IP-Address>:80 loadfactor=5 timeout=1
               ProxySet lbmethod=bytraffic
               # ProxySet lbmethod=byrequests
        </Proxy>

        ProxyPreserveHost On
        ProxyPass / balancer://mycluster/
        ProxyPassReverse / balancer://mycluster/

#Restart apache server

sudo systemctl restart apache2

editsitesavail

This is bytraffic balancing method, it distribute incoming load between our Web Servers according to current traffic load. We can control in which proportion the traffic must be distributed by loadfactor parameter.

Verify that our configuration works. Open web browser and enter:

http://<Load-Balancer-Public-IP-Address-or-Public-DNS-Name>/index.php

indexphplb

Open two terminals for the two webservers and Run

sudo tail -f /var/log/httpd/access_log

s2accesslog

s1accesslog

These are the logs of our load balancer page (apache), showing the get requests it is senging to our two webserver

Refresh the load balancer page and check the access log on both webserver terminals

xtralog2

xtralog1

The loadfactor we configured were set to approximately equal numbers and this has made the requests coming in approximately the same

SET UP DNS

Open and edit load balancer server

sudo vi /etc/hosts

Add 2 records into this file with Local IP address and arbitrary name for both of our Web Servers

<WebServer1-Private-IP-Address> Web1
<WebServer2-Private-IP-Address> Web2

etchosts

Update the LB config file with these names instead of IP addresses.

sudo vi /etc/apache2/sites-available/000-default.conf

BalancerMember http://Web1:80 loadfactor=5 timeout=1
BalancerMember http://Web2:80 loadfactor=5 timeout=1

reeditsitesavail

Now, curl the the webservers from the Loadbalancer

curl http://Web2
curl http://Web1

curl

Note that this is local to the load balancer and cannot be accessible from either webservers

The diagram below shows exactly how the setup is

Screenshot from 2023-03-15 06-23-00

End