How to setup a simple load balancer in Metal Cloud
Due to its architecture that combines the power of dedicated servers with the scalability of cloud computing, Bigstep Metal Cloud instances offer a high-performance hosting platform.
However, a successful modern website can generate so much traffic that even the most powerful available server might run out of resources.
The solution, in this case, is load balancing, which typically uses three or more servers to distribute requests. If the budget is tight, scaling from a single server to a setup with three or more machines is not an option. In addition, reverting to a single server when the traffic is low can be quite difficult.
Bigstep Metal Cloud provides an easy load balancing method that is effective and scalable at the same time.
Create an infrastructure
Start by adding a new Instance Array from the Infrastructure Editor, then click on it. Select Configuration from the top menu bar, choose a server type and select at least two of them (for example, 2 x FMCI 8.32 v2).
Click on the drive arrays attached to the instances and configure them as well, in this case, the two 40 GB SSD drives will be provisioned with CentOS 7.5.
Click again on the instance array. You will notice in the upper part that a subdomain is already configured for instance array on the domain bigstep.io, based on the random name generated for the array and the name of your infrastructure. You can edit it in order to replace the random array name with a more convenient one.
Save your changes and deploy the infrastructure, the servers will be available at the end of the process.
How does load balancing work
Click again on the instance array and note the IP addresses of the servers, as well as the infrastructure array domain (for example lb.vanilla.test.5877.bigstep.io).
When the subdomain is created, it will automatically have records for the public IP addresses of all instances that are part of the array. If you ping the domain several times, you’ll notice the IPs of the instances will alternatively respond to it.
This feature can be used to set up a very simple type of DNS-based load balancing. You just have to configure the DNS CNAME record of your domain to point to the instance array domain, the raw record for a www subdomain will look similar to this one:
www 14400 IN CNAME lb.vanilla.test.5877.bigstep.io.
After the change propagates, your domain will be served from the two instances, in round-robin fashion.
Testing the setup
For the purpose of this article, we will setup the simplest web server, using a module already built in every version of Python.
Connect to your servers through SSH and create an index.html file in any folder, the contents of the files can be “server one” and “server two”.
Run the following command in order to start the HTTP server (it listens on port 8000 by default):
python -m SimpleHTTPServer
If you access your domain in a browser of port 8000, you’ll notice that both instances alternatively serve it. Since browsers cache domain IP addresses for some time, you might have to use incognito mode to force a refresh. Port 8000 has to be allowed in the operating system firewall or the one managed from the Infrastructure Editor diagram.
A big advantage of this setup is that you can scale it easily, by adding more servers when the traffic is high or by using a single one if the activity is low. This allows you to greatly reduce costs if you choose an on-demand plan with billing on an hourly basis.
Unless your websites are static, you’ll have to configure the servers to use a common root folder and database. This is easily achieved by exporting an NFS share from the main server and mounting it on the others, while databases can be replicated in a master-master setup.
Since you don’t have a real load balancer in front of your servers, the requests are distributed round robin and more advanced mechanisms are not available.
In order to prevent the requests from one client being served from different machines, you can setup a common location for sessions, memcache is a great tool for this purpose.
Keep in mind there are no health checks, so many requests will fail with errors if one or more servers are offline.
While this basic method of load balancing is very limited compared to a real setup, it can be surprisingly effective when resources are limited. Follow our blog for more articles on how to configure high-traffic solutions using the bare-metal performance of Bigstep Metal Cloud.