This post is the fifth chapter of a LAMP Tutorial series in which we will show you how to set up a fully redundant, high-availability LAMP stack in the cloud with ElasticHosts. Find the other parts here:
- LAMP Tutorials (1/6): Set Up a LAMP Stack on a Cloud Server
- LAMP Tutorials (2/6): Move MySQL to a Separate Cloud Database Server
- LAMP Tutorials (3/6): Create a Second MySQL Cloud Database Server
- LAMP Tutorials (4/6): Add a Second Cloud Web Server with Round-Robin DNS Load Balancing
- LAMP Tutorials (6/6): Add a Second HA Cloud Load Balancer
Add a Front-End Apache Cloud Load Balancer
The next step in our tutorial is adding load balancing. This means setting up a new server in front of our two web servers. This load balancing server will monitor both web servers, and switch traffic between them intelligently.
Load balancing setup on ElasticHosts has special requirements because our system does not allow floating Mac addresses. This makes using VRRP (Virtual Router Redundancy Protocol), which is the basis for many load balancers, problematic. We use the Apache module
mod_proxy_balancer. This turns Apache into a load balancer, retrieving requested pages from two or more backend web servers and delivering them to the user’s computer. If required,
mod_proxy_balancer can keep track of sessions, so a single user always deals with the same backend web server.
The steps are as follows:
- Step 1: Set up a new server
- Step 2: Install Apache and
- Step 3: Configure
Step 1: Set up a new server
First we create a fifth Ubuntu 10.04 server, which will be our load balancer. We’ll call this new server herring. Then set herring up to share a private VLAN with our web and database servers, as described in our How To Set Up a VLAN tutorial.
In this tutorial, we’ve given herring the IP address 10.0.0.5. In addition, we will need an external static IP: add this as described in our Add a Static IP Address tutorial. In this tutorial, we’ve given herring the static IP address 188.8.131.52.
Note: In the next tutorial, you’ll be setting up a second load balancer with another static IP address. These should be within the same class C range, so you may find it helpful to set up two static IP addresses now, and check that they are within the same A.B.C. range.
Just to recap, we currently have five servers in total:
- puffin: Web server 1, private IP address 10.0.0.1.
- albatross: Web server 2, private IP address 10.0.0.4.
- beagle: Database server 1, private IP address 10.0.0.2.
- crocodile: Database server 2, private IP address 10.0.0.3.
- herring: Load balancer 1, private IP address 10.0.0.5 and static IP address 184.108.40.206.
In a real system, at this point, you might want to configure your DNS servers to give each of your servers with a subdomain (e.g. db1.yourdomain.com, web1.yourdomain.com, loadb1.yourdomain.com etc) in order to check their status easily.
Step 2: Install Apache and
First, on our new load balancing server, we need to install Apache and enable the proxy modules:
$ apt-get update $ apt-get install apache2 $ a2enmod proxy $ a2enmod proxy_balancer $ a2enmod proxy_http $ /etc/init.d/apache2 force-reload
Step 3: Configure
Now, we need to configure our cluster, in a new file in conf.d:
$ vi /etc/apache2/conf.d/proxy-balancer BalancerMember http://10.0.0.1 BalancerMember http://10.0.0.4 ProxyPass / balancer://mycluster
Then we need to configure our proxy to allow access from all hosts:
$ vi /etc/apache2/mods-enabled/proxy.conf
Change the line “Deny from all” in this file to “Allow from all”.
Then restart Apache:
$ /etc/init.d/apache2 restart
Now, when you visit http://220.127.116.11 in a web browser, you should see our site. You should also be able to stop either one of the web servers or one of the database servers without the site stopping.
You can find more information on configuring
mod_proxy_balancer – including balancing methods, sticky sessions and dynamic updates to load balancing at the Apache documentation.
In the final tutorial, we will [Add a Second HA Cloud Load Balancer](/blog/add-a-second-ha-cloud-load-balancer/) to our servers.