Adding 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 pelican

We'll set up a new server called pelican, as in step 1. Connect via VLC and change the root password.

Next, we need to give it a static IP address, as we did for seagull 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 elastic1@10.0.0.4 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).
pelican$ apt-get update
pelican$ apt-get dist-upgrade
pelican$ apt-get install apache2 php5 libapache2-mod-php5 php5-mysql rsync
Back to seagull, and rsync across our PHP file:
seagull$ rsync /var/www/index.php root@10.0.0.4:/var/www/
Back to pelican, make the file executable and edit it to connect to 10.0.0.3, so we're using the second MySQL server.
pelican$ rm /var/www/index.html
pelican$ chmod a+x /var/www/index.php
Then, restart Apache:
pelican$ /etc/init.d/apache2 restart
Visit our new server's IP in a web browser to check it works:
Step 4: Set up Round-robin DNS

We won't show this in full here, because what you will do exactly will depend 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.crosspeer.com 20 IN A static.ip.1
lamp.crosspeer.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 use a 'real' load balancer.
Copyright © 2012 - 2014 Crosspeer, Inc.
               All Rights Reserved