Configure Simple Load Balancing With Nginx

Let's apply what we learned in the previous blog post into practice. If you haven't read my previous blog post, please read it before going through this.

https://www.rajith.me/2018/07/what-is-load-balancing.html

We are going to load balance between two servers so you will need three servers. You can use any cloud provider to spin up these servers, here I will use AWS as the cloud service provider.

https://aws.amazon.com/free/
https://cloud.google.com/free/

Now I need three servers for this example,  let's name them as

1) External lb
2) Server-1   
3) Server-2   

Diagram

Install nginx in the lb server

Let's install nginx in the lb node, first ssh into the server.  I have chosen Ubuntu as the image for the load balancer server and for other servers as well.
sudo apt-get update
sudo apt-get install nginx
For other distributions follow the below link to install nginx
https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/#prebuilt
Now make sure when you visit the External lb IP, should redirect to default nginx page 

Install apache web server in both servers 

Run the below commands to install apache web server in both server 1 and server 2
sudo apt-get update
sudo apt-get install apache2
sudo systemctl status apache2

After installation now you can view the default Ubuntu 16.04 Apache web page if you type public Ip of the server in the browser. Since both servers now load the default page let's change the landing page of the apache server in both servers.
Change the index.html as web server - 1 and web server -2
 cd /var/www/html/  
 sudo rm index.html  
 sudo vim index.html   
 web server - 1  
 sudo systemctl restart apache2  
After this change, you should get two different pages when visiting the IP in the browser as

Configuring Nginx

 - ssh into the external lb server
 - go to nginx conf 
 cd /etc/nginx/conf.d
- now let's add our load balancing configurations, I'm naming this file as simple-load.conf
 sudo vim simple-load.conf 
- add the below configurations 
 upstream backend {
  server Server-1_IP;
  server Server-2_IP;
}
server {
  listen 80;
  location / {
      proxy_pass http://backend;
   }
}
- This server listens to all traffic to port 80 and passes to upstream (our web servers)
- The upstream name and the proxy_pass should be matched
- You should remove the default nginx configuration
 sudo rm /etc/nginx/sites-enabled/default
- Now reload the nginx service
 sudo systemctl reload nginx
- Now if you enter load balancer's public IP address through your browser, you should be passed to one of your web servers. This is the simplest load balancing mechanism.  (Round Robin)

Let's try different configurations

Weighted load balancing

 upstream backend {
  server Server-1_IP weight=5;
  server Server-2_IP;
}
Now the new requests will be distributed to the ratio 5:1 to both servers. 5 requests forward to
Server-1 then 1 request to Server-2.

Backup

Backup can be used when you want to configure active-passive load balancing. Traffic will always route to the Server-1, if the server goes down for some reason, the traffic will then serve to the backup server.
 upstream backend {
  server Server_1;
  server Server_2 backup;
}

Let's try this scenario

If you visit the lb public IP you will always route to the Server-1
Now let's stop the apache server of the server-1, to do that ssh into server-1 and run the below command
 sudo systemctl stop apache2
Now visit the External lb Ip again now you will be redirected to the backup server which is the server-2
Again if you start the apache server in server-1
 sudo systemctl start apache2
Traffic will start to route to server-1 again.
This is an end of very basic load balancer configuration tutorial, just play around with this and try to understand what load balancing mechanism you required when you deploy your applications.

Thank you !!!!

Comments