Create an Ubuntu Server 20.04 EC2 instance and name it Project-8-apache-lb.
Open TCP port 80 on Project-8-apache-lb by creating an Inbound Rule in Security Group.
On the LB instance - Update the repo, install Apache server and the xml package as well as enable some required apache modules.
sudo apt update -y
sudo apt install Apache2 -y
sudo apt-get install libxml2-dev -y
# Required Modules
sudo a2enmod rewrite
sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod headers
sudo a2enmod lbmethod_bytraffic
sudo systemctl restart apache2
Configure load balancing - configuring the LB-Server enables it to efficiently manage the distribution of incoming traffic to the best available Web Server ( for this setup, traffic is distributed evenly - loadfactor=5
)
Let's edit the Apache configuration file to register the two web servers
sudo vi /etc/apache2/sites-available/000-default.conf
Add the following line of codes within <vitualhist tags>
tags the file. This includes the private ip for both web servers
<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 the apache service to register the update
sudo systemctl restart apache2
bytraffic
balancing method will distribute incoming load between the Web Servers according to current traffic load. I have controlled which proportion of the traffic must be distributed by using theloadfactor
parameter.
Verify the LB Server (via public ip) is serving the tooling website in the browser.
Verify the log directory of both web servers are not mounted to the NFS server.
sudo umount -f /var/log/httpd/
To see traffic distrubtion (Load balancing) in action - open log file for both web servers and refresh the LB-server serveral times.
sudo tail -f /var/log/httpd/access_log
web server 1
web server 2
From the Logs, you can see both Web Servers are receiving the same load of web traffic. This is because the
loadfactor
was set to the value5
Optional Step – Configure Local DNS Names Resolution in the LB - Server
sudo vi /etc/hosts
Add the private ip address for the two web servers
172.31.88.47 Web1 172.31.95.118 Web2
Update this changes in the configuration file of the apache server on the LB instance.
sudo vi /etc/apache2/sites-available/000-default.conf
Use Curl verfiy that LB-server can acces web servers the local dns Web1 Web2
curl http://Web1