Digital Ocean Load Balancers: A Practical Tutorial

Share:

 

Digital Ocean announced the release of Load Balancers a few days ago, and like a kid in a candy store, I couldn’t wait to try it out.  Load Balancers have been one of the most demanded features from the VPS company, and after giving it a spin, it was everything I expected and more!

In this tutorial, I want to give  you a practical guide with Digital Ocean Load Balancers.  If you don’t know what a Load Balancer is, to put it simply, it’s a server that’s optimized to re-route traffic, it’s a server that sits between your application servers and the user, the job of load balancer is to take the incoming traffic, and redirect it to one of your application servers.

Now why would you need one you asked? The main need for load balancers is it allows you to horizontally scale your application infrastructure.  Let’s say you have only one application server, and one day one of your blog posts hit reddit and all of sudden you have 1000x your normal incoming traffic, your website starts to slow down, and before you know it, it’s completely down.  Imagine if you run an ecommerce store,an outage will cause a devastating lost in sales and potential new customers.

So what can you do? You can vertically scale your application server by upgrading hardware specs such as CPU, RAM, and storage so you handle the sudden increase in traffic with a much more powerful machine,  but the fact is, this is expensive and there’s a limit to how much hardware you can add to a single machine.  The better solution is to add more application servers with the same specs and scale horizontally as the traffic increases!  Server providers like Digital Ocean allows you to start a server with same specs in less than a minute! So let’s say you scaled up your application to 10 servers (cloned from your first one), and they’re ready to work,  this is where load balancers come in.  You can put a load balancer in front of your 10 servers, and when a user requests your website, the load balancer will perform an algorithm (usually round robin) to pick which server to serve to the user!  Instant scalability!  What if your site is now on TV, Radio, etc? Not a problem, you can simply clone more servers, and have them register to the load balancer and you’re ready to go!

Load balancer concept

Let’s demonstrate the above scenario and see how you can use the new load balancer feature from Digital Ocean to scale your website.

Create Three Droplets

If you don’t have a Digital Ocean account, you can sign up for one, use this link and you’ll get $10 off.  Click on Create Droplet button from the dashboard, we’re going to use a pre-existing LEMP on Ubuntu 16.04 image to speed up our process.

Create Droplet Digital Ocean

Use the lowest level $5/month droplet because this is a test, leave everything else default.  At the botttom of the page, Let’s name our droplets lb-test-01 press the + button 2 more times to create 3 droplets total.  Press Create and wait about 2 minutes for the droplets to be created.

You should receive 3 emails from Digital Ocean with IP and Password to your newly created droplets.  Go ahead and open three terminal tabs and log into each one.

ssh root@YOUR_DROPLET_IP_ADDRESS

You’ll be asked to set up a new password when you successfully logged in for the first time.

What we want to accomplish with these three droplets is to edit the index.html files on each and have it say the name of the server.  Let’s go to the /var/www/html folder in the server directory and use your favorite command line text editor to edit the file:

<h1>Hello from Server 1</h1>

repeat the same for server 2 and server 3. (make them display Hello from Server _ , where _ is the number of your server)

This way when we go to the IP address of each droplet, it will greet us with the name of the server.

Create a Load Balancer

Digital Ocean makes it extremely simple to create a load balancer from their dashboard. Click on Networking from the header menu, then click on Load Balancers and finally click Create Load Balancer button.

create load balancer digital ocean

  1. Enter a name for the Load Balancer, let’s call it pentacode-lb
  2. Add our droplets to the load balancer, lb-test-01, lb-test-02, lb-test-03
  3. Leave forwarding rules as is, this is useful if you want load balancer to forward traffic to none standard server ports
  4. Leave advanced settings as is, this is where you can set different routing behavior and algorithms for the load balancer router.
  5. Press Create Load Balancer

digital ocean load balancer settings

Wait about a minute or two and your load balancer should be ready to receive traffic.  Click on the load balancer to get detailed information about the servers it oversees, you can see critical stats like health and uptime/downtime from the dashboard.

Let’s do a quick test, go to the IP address of the load balancer from our browser, if you did everything correctly, you should see the following:

reload the page a few times, you should notice each time it routes you to the next server on the list, so you’ll see Hello from server 2 next, followed by Hello from server 3

The load balancer will automatically rebalance traffic should it detect unhealthy servers, this will make sure your will always have a good server to serve to your users.  Feel free to play around the settings tab to see what different behaviors you can make your load balancer do.

digital ocean load balancer settings

That’s it! You now have high availability and scalability!  I can’t wait to see more products like this to be added by Digital Ocean, it’s a company I’ve been using for many years and highly recommend their services if you’re in the market for a new server host.

Comments Or Questions? Discuss In Our Discord

If you enjoyed this tutorial, make sure to subscribe to our Youtube Channel and follow us on Twitter @pentacodevids for latest updates!

More from PentaCode