nginx Load Balancer Configuration
With a better understanding of multi-mode Vagrant, I can easily create multiple virtual machines and have them networked together easily.
One thing I want to learn to configure: an nginx load balancer. I have seen load balancers scale out applications, how can I configure one from a fresh install?
To configure an nginx load balancer to connect to a private network of computers based on an endpoint used.
- 1x load balancer that is publicly accessible
- 5x worker nodes connected to the load balancer with a private network
I will go through step by step, verifying configuration along the way.
To follow along, install the following pieces of software in your system.
Regular readers will know I use these often. If you have problems, please contact me
Making a Balancer
Let’s start with a single balancer virtual machine that is connectable from the outside through the forwarded port and has its own private network.
I will have this Vagrantfile to build a balancer:
Highlights from the file:
- Global variables:
BALANCER_PORTdefines which vagrant box image, internal IP address to use, and port to use in a single section of the file.
- The provisioning step installs nginx and replaces the default home page with the virtual machine’s hostname. (Keep it simple!)
Starting things up to test:
So far, so good.
A single load balancer is pretty boring. We need workers to balance the load!
First, extend the Vagrantfile by adding a section to create worker:
Highlights from the addition:
WORKER_COUNTvariable specifies the number of workers to create.
- Provisioning each worker is the same: install nginx and replace the default nginx page.
- The original specification required workers to be only accessible on the private network, but I keep an open port so we can verify connectivity before closing it.
Let’s bring up each worker and verify accessiblity:
Balancer - Worker Connections
Each virtual machine is up, let’s verify each worker is accessible from the balancer:
Up to this point, we have a balancer and five workers operational. Each are acessible via a forwarded port, returns a page with their hostname on it, and connected to the balancer.
To make nginx a load balancer, we have to update its current
configuration with a new directive:
Change the current default nginx config in
/etc/nginx/sites-available/default to the content:
Highlights from the file:
||specify a server group|
|backend, even, odd||labels for different upstream server groups|
||specifies the endpoint to listen to (i.e.
||have nginx rewrite the URL before passing onto the upstream server. In this case, remove:
||pass the incoming request to the specified upstream server|
Testing Load Balancer
Run the command:
sudo nginx -t to test out the current configuration
and fix any errors.
Restart the nginx server with new configuration with command:
service nginx restart inside the balancer virtual machine.
Or run commands:
On the command line, this is quick way to verify:
For any worker:
Just odd numbered workers:
And finally, even numbered workers:
You can also test this using a browser.
With the above configuration, this will happen:
localhost:8081will give the default web page for the balancer, that returns ‘balancer’.
localhost:8081/workerwill pass the request to workers one through five, when loading multiple times in a row.
localhost:8081/oddwill pass the request to odd numbered workers (worker1, worker3, worker5) when loading multiple times in a row.
localhost:8081/evenwill pass the request to even numbered workers (worker2, worker4) when loading multiple times in a row.
So, according to the original specifications, this configuration has:
- 1x load balancer that is publicly accessible through the port: 8081 on localhost.
- 5x worker nodes connected to the load balancer with a private network, IP Address range: 192.168.100.11-16.
- Any worker is available using the
- Odd/even workers are available using the
localhost:8081/[odd | even]balancer endpoint.
The most important part of this article is the nginx configuration! This is the configuration that changes nginx from a static webserver into a load balancer.
The following are resources I used in developing this article: