Why A Load Balancer?
If you are familiar with PaaS(Platform as a Service) providers such as Digital Ocean or Amazon Web Services, then you may be familiar already with provisioning of web servers. From a software development perspective, this is great because we are able to create applications, deploy easily and also create backups of our server with just a few clicks. What happens though when your application or website starts to get a lot of traffic and that tiny little server you provisioned comes crawling to its knees, and your site goes down altogether? One option is to change your server configuration and add more CPU/RAM or disc storage. But this is not necessarily guaranteed to work, depending on what exactly your server is experiencing. The other option is to scale your application horizontally. Or in other words, place it behind a load balancer with other servers that are all hosting the same application. The load balancer is merely the receiver of the initial request to the site and then they are handed off to the application where they will all do the same work.
As you can see in the above image taken from the Digital Ocean Article ‘5 DigitalOcean Load Balancer Use Cases’, the load balancer will allow us to create a pool of similar resources, and scale our application in this way. This is a pretty common scenario, and there are many advanced architectural patterns that can be used for sharing resources, but for now, we are just going to to go over the basics of setting up a Load Balancer on DigitalOcean and creating a resource pool that sits behind it, which will then be accessed when an incoming request hits the load balancer and distributed across the pool. I work quite a bit with AWS, and while I have nothing against them perse, I also like to use other Platform Providers, and DigitalOcean is super cool, as I find their documentation outstanding and there is a large community of developer’s who are willing to comment and help in their resources sections.
Setup our Resource Pool
For this tutorial, you’ll need a DigitalOcean account. It’s pretty straightforward, and I’d recommend setting one up.
We’re going to be provisioning for our resource pool, three servers. We are just going to be using LEMP stack for these. Create our servers by adding a new droplet, look for ‘Market Place’ and search for LEMP on Ubuntu 18.04. This used to be called ‘One Click Apps’ but recently changed. Such is the life of a developer.
Click and select the ‘LEMP on 18.04’ icon.
Next we need to set the size of our servers. Scroll down and select the cheapest option as this is only tutorial. Normally I would also setup SSH keys and backups, but as this is a tutorial and merely to gain an understanding of the concept we will forgo this.
Keep scrolling to the bottom of the page and look for the ‘Create Droplet’ button. Change the number of droplets to 3. Rename them and click the button. In this case, I name all of mine
Test-LEMP-2, etc ….
Wait a few minutes grab a cup of coffee, and then check your email. You’ll have received one for each server with the host information and a ‘root’ user password.
Copy that information to a text file or somewhere you can grab it easily. Should look something like the below.
Droplet Name: Test-LEMP-01 IP Address: 206.XXX.XXX.142 Username: root Password: 7.........b Droplet Name: Test-LEMP-02 IP Address: 167.XXX.XXX.97 Username: root Password: 2...........9 Droplet Name: Test-LEMP-03 IP Address: 167.XXX.XXX.164 Username: root Password: 8...........4
Setting up Your Servers
Next, you’ll need to SSH into each of these servers using the provided password sent to you in each email.
Should look like this when you SSH into each. You’ll need to reset your password as well.
$ ssh firstname.lastname@example.org The authenticity of host '184.108.40.206 (220.127.116.11)' can't be established. ECDSA key fingerprint is SHA256:sltNBQ5wS60L/9MadUp2W2XO8hITjZPHZgRGUIsIrqA. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '18.104.22.168' (ECDSA) to the list of known hosts. email@example.com's password: You are required to change your password immediately (root enforced) Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-46-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Thu May 9 13:18:24 UTC 2019 System load: 0.0 Processes: 89 Usage of /: 6.7% of 24.06GB Users logged in: 0 Memory usage: 31% IP address for eth0: 22.214.171.124 Swap usage: 0% Get cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud 0 packages can be updated. 0 updates are security updates. ******************************************************************************** Welcome to DigitalOcean's One-Click LEMP Droplet. To keep this Droplet secure, the UFW firewall is enabled. All ports are BLOCKED except 22 (SSH), 80 (HTTP), and 443 (HTTPS). In a web browser, you can view: * The LEMP One-Click Quickstart guide: http://do.co/lemp1804#start * Your LEMP website: http://126.96.36.199 On the server: * The default web root is located at /var/www/html * The MySQL root password is saved in /root/.digitalocean_password * Certbot is preinstalled. Run it to configure HTTPS. See http://do.co/lemp1804#enable-https for more detail. For help and more information, visit http://do.co/lemp1804 ******************************************************************************** To delete this message of the day: rm -rf /etc/update-motd.d/99-one-click The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Changing password for root. (current) UNIX password: Enter new UNIX password: Retype new UNIX password: root@Test-LEMP-01:~#
Then you are going to need to navigate to the following directory where NGINX serves files from. Look for
root@Test-LEMP-01:~# cd .. root@Test-LEMP-01:/# cd var/www/html root@Test-LEMP-01:/var/www/html# vim index.html
I used vim. Type
gg to go to the top of the file. Then type
dG to delete the content then hit
i to insert
<h1> Hello from Server 1...</h1>
index.html by hitting
esc followed by
:wq and ‘enter’.
Testing Your Edits to
Do this for your other two servers as well. Once you’ve done that, in your browser navigate to one of your IP’s. You should see something like the following displayed at the top of your page: Hello From Server 1…
We are doing this so that when we navigate to our three servers IP addresses, we can see which server number we have designated them as.
Setting Up Our Load Balancer
Back in your DigitalOcean console, go to the
Networking tab on the left side of your browser. Navigate from there to
Load Balancer. Your page should look something like this:
Create Load Balancer
Assign the region. Note that the Load Balancer must be in the same region as your droplets we created. Give it a name. And click
Create Load Balancer.
Following that you can add your droplets.
Testing Our Setup
Now you should have the IP address of your newly created load balancer. Navigate your browser to the load balancer IP. You should see one of your header tags showing which server you have been forwarded to. If you hit refresh, you’ll see the next server, so on and so forth.
By default, the load balancer is set to use Round Robin for selecting which server the load balancer will forward clients to. Basically, it’s just a rotational selection, there are also options for least connections, and then also if you want a user to maintain session state, enabling sticky sessions is also an option. There are also ways to enable port forwarding and SSL Termination. We are not going to do anything with these right now, as this is merely a demonstration, but be aware that these setting exist and depending upon requirements are often used.
Hope you found this helpful, at least in understanding the general idea behind a load balancer and how to set up an implementation. You can check out this article on DigitalOcean’s blog for some additional ideas, as well as look into layer 4 and layer 7 architectures, both of which are pretty common uses, for shared resources such as a database.
Cheers and happy coding!