Skip to content

Latest commit

 

History

History
74 lines (66 loc) · 4.64 KB

File metadata and controls

74 lines (66 loc) · 4.64 KB

LOAD BALANCER SOLUTION WITH APACHE

Capture

Step 1 Configure Apache As A Load Balancer

  1. Create an Ubuntu Server 20.04 EC2 instance and name it Project-8-apache-lb.
  2. Open TCP port 80 on Project-8-apache-lb by creating an Inbound Rule in Security Group.
  3. 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
  1. 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
  1. Ensure Apache2 is up and running: sudo systemctl status apache2 pix1

  2. Next, configure load balancing in the default config file: sudo vi /etc/apache2/sites-available/000-default.conf

  3. 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/

pix2

  1. Restart Apache server: sudo systemctl restart apache2
  2. 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 pix3
  • The load balancer accepts the traffic and distributes it between the servers according to the method that was specified.
  1. 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
  2. Open two ssh/Putty consoles for both Web Servers and run following command: sudo tail -f /var/log/httpd/access_log
  3. Refresh the browser page http://172.31.95.118/index.php with 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. pix4 pix5

Step 2 – Configure Local DNS Names Resolution

  1. 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.
  2. Open this file on your LB server: sudo vi /etc/hosts
  3. 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

pix6

  1. 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

pix7

  1. You can try to curl your Web Servers from LB locally curl http://Web1 or curl http://Web2 to see the HTML formated version of your website. pix8 pix9