This post is the fourth 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 (5/6): Add a Front-End Apache Cloud Load Balancer
- LAMP Tutorials (6/6): Add a Second HA Cloud Load Balancer
Add a Second Cloud Web Server with Round-Robin DNS Load Balancing
So we’ve duplicated our MySQL server. The next logical step is to duplicate our web server. We can then send half our traffic to one web server, and one to the other. In this tutorial, we’re not going to do anything smarter than that – literally half to one, half to the other. We’ll use a technique called round-robin DNS, which simply alternates requests between the two servers.
Step 1: Set up a new server called albatross
We’ll set up a new server called albatross, as in the first chapter of this series. Connect via VNC and change the root password. Next, we need to give it a static IP address, as we did for puffin previously, and connect it to the VLAN with the IP address 10.0.0.4.
Step 2: Update our databases to allow connections from this machine
We need to tell both our MySQL databases, on beagle and crocodile, to allow connections from this server at 10.0.0.4.
On both database servers, do the following:
$ mysql -u root -p > GRANT ALL PRIVILEGES ON prices.* to email@example.com IDENTIFIED BY 'oag4Chai'; > FLUSH PRIVILEGES; > EXIT;
Step 3: Duplicate our PHP application
Next, we need to install all the software we need to run PHP (note: we won’t install MySQL on this machine, just php5-mysql).
albatross$ apt-get update albatross$ apt-get dist-upgrade albatross$ apt-get install apache2 php5 libapache2-mod-php5 php5-mysql rsync
Back to puffin, and rsync across our PHP file:
puffin$ rsync /var/www/index.php firstname.lastname@example.org:/var/www/
Back to albatross, make the file executable and edit it to connect to 10.0.0.3, so we’re using the second MySQL server.
albatross$ rm /var/www/index.html albatross$ chmod a+x /var/www/index.php
Then, restart Apache:
albatross$ /etc/init.d/apache2 restart
Step 4: Set up round-robin DNS
We won’t show this in full here, because what you will do completely depends on how you choose to handle your DNS, but in theory, this is as easy as adding multiple A records in our DNS zone file.
Suppose we use this in the DNS zone file for our domain:
lamp.elastichosts.com 20 IN A static.ip.1 lamp.elastichosts.com 20 IN A static.ip.2
Now about half the time, a host will see the first address, and half the time the second. And each server will have half the load that our previous single server did.
Note: You should set the TTL low (to 20 seconds) to prevent caching DNS servers keeping one address too long. But this doesn’t handle traffic in a very sophisticated way.
In the next tutorial, we will Add a Front-End Apache Cloud Load Balancer.