- 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.
- Connect to the server through the SSh terminal and install Apache Load balancer then configure it to point traffic coming to LB to the Web Servers by running the following:
sudo apt update
sudo apt install apache2 -y
sudo apt-get install libxml2-dev
- Enable the following and restart the service:
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
-
Ensure Apache2 is up and running:
sudo systemctl status apache2 -
Next, configure load balancing in the default config file:
sudo vi /etc/apache2/sites-available/000-default.conf -
In the config file add and save the following configuration into this section *<VirtualHost :80> making sure to enter the IP of the webservers
<Proxy "balancer://mycluster">
BalancerMember http://172.31.95.40:80 loadfactor=5 timeout=1
BalancerMember http://172.31.89.249: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 - Verify that our configuration works – try to access your LB’s public IP address or Public DNS name from your browser:
http://<Load-Balancer-Public-IP-Address-or-Public-DNS-Name>/index.php
- The load balancer accepts the traffic and distributes it between the servers according to the method that was specified.
- In Project-7 I had mounted /var/log/httpd/ from the Web Servers to the NFS server, here I shall unmount them and give each Web Server has its own log directory:
sudo umount -f /var/log/httpd - Open two ssh/Putty consoles for both Web Servers and run following command:
sudo tail -f /var/log/httpd/access_log - Refresh the browser page
http://172.31.95.118/index.phpwith the load balancer public IP several times and make sure that both servers receive HTTP GET requests from your LB – new records will appear in each server’s log file. The number of requests to each server will be approximately the same since we set loadfactor to the same value for both servers – it means that traffic will be disctributed evenly between them.
- Sometimes it may become tedious to remember and switch between IP addresses, especially when you have a lot of servers under your management. We can solve this by configuring local domain name resolution. The easiest way is to use /etc/hosts file, although this approach is not very scalable, but it is very easy to configure and shows the concept well.
- Open this file on your LB server:
sudo vi /etc/hosts - Add 2 records into this file with Local IP address and arbitrary name for both of your Web Servers
172.31.95.40 Web1
172.31.89.249 Web2
- Now you can update your LB config file with those names instead of IP addresses.
BalancerMember http://Web1:80 loadfactor=5 timeout=1
BalancerMember http://Web2:80 loadfactor=5 timeout=1