Simple Flask Load Balancing using Nginx

Simple Flask Load Balancing using Nginx

Let's see a very simple case scenario (Where we have only 1 instance of application)

In which Developer write the Code and Deploy into a Production (i.e. put the code into a Public Server), so that all users can access.

Screenshot from 2021-01-18 22-47-58.png

if number of users who are accessing the application is less, then this architecture could be fine.

But if number of users increases (Generally in Live Scenarios) - Then the load will increases on one server and users may getting late response or even timeout or extra load may put the server down.

To sort the above issue, we have below option -

Deploy the application into 2-3 servers. But Both Servers have different IP. How user know which IP he/she has to access.

So, Now comes a Load Balancer as a middle-man, where user only send the request to middle-man (which is Load Balancer) and then Load-balancer will forward the request to different Servers based on the algorithm used (example Round-Robin, Weighted Round Robin and so many others)

Screenshot from 2021-01-18 22-39-30.png

Round-robin load balancing is one of the simplest and most used load balancing algorithms. Client requests are distributed to application servers in rotation. For example, if you have three application servers: the first client request to the first application server in the list, the second client request to the second application server, the third client request to the third application server, the fourth to the first application server and so on.

Let's see in practical

  • We will write a very simple Python Flask application.
  • We will run this application on two different ports.
  • We will use Nginx Web Server (which will act as a Load-Balancer).
  • We will update configuration of Nginx (So that Load-balancer knows - when user access Load-Balancer, where Load Balancer forwards the request).
  • Final Step is Access the application (or we can say - access Load Balancer)

Create 2 Files (app.py and nginx.conf) in some folder. Below is the file structure. Ignore doc.txt

Screenshot from 2021-01-18 23-04-36.png

Open app.py and write below code

carbon(1).png

Start the Both Applications

python3 app.py 5001

Screenshot from 2021-01-18 23-08-52.png

python3 app.py 5002

Screenshot from 2021-01-18 23-09-26.png

So we started both the applications on different ports i.e. 5001 & 5002

Now open nginx.conf and update with below code

carbon(2)(1).png

Let's understand few points of nginx.conf

  • location / => Here we give location as '/' i.e. whenever user will access localhost:80/ ; where 80 is the default port on which nginx runs (i.e. our load balancer runs)

  • proxy_pass loadbalancer; => So, we are saying, whenever someone tries to access localhost:80/ - it will forward it' request to loadbalancer

  • But what is loadbalancer ; so we need to define this as well

  • upstream loadbalancer { server 0.0.0.0:5001; server 0.0.0.0:5002; } => Here we define alias (loadbalancer) and provide our 2 instances on which our application is running as shown in above images.

Now, we need to update our nginx configuration

Note down the path of nginx.conf (where we created above file). In Linux, default nginx.conf placed at /etc/nginx

So, first open default nginx.conf (with any editor - I choose nano)

nano /etc/nginx/nginx.conf

Screenshot from 2021-01-18 23-20-42.png

We will see 'include' word - here we will one extra line to add our configuration ; like my configuration which I created is placed at /home/sumanshu/Desktop/nginx/nginx.conf;

So, we updated nginx.conf in /etc/nginx

Now, time to test the configuration - whether it's correct or some syntax issue (because of our changes)

Run below command -

nginx -t ==> to test configuration.

Screenshot from 2021-01-18 23-26-25.png

So, configuration is okay-

Now, let's reload the configuration (rather then stopping nginx and then restarting)

Run below command - nginx -s reload

Now, everything is Ready , but make Sure, the two python instances which we started individually on 5001 and 5002 should be UP.

Now, go to browser and browse localhost:80

We will see we will get request from 0.0.0.0:5001 and 0.0.0.05002 (one by one) i.e. in a Round-Robin fashion which is a default algorithm. If we have to choose any another algorithm, then we need to update the same in nginx.conf file

See below, we just access localhost ; and we get response from our application which is running on different ports

Screenshot from 2021-01-18 23-31-14.png

Screenshot from 2021-01-18 23-31-24.png

Run below command -

while true; do curl localhost:80; done

We just access localhost in loop, to see the behaviour of load-balancer

Screenshot from 2021-01-18 23-39-56.png

We noticed, we are getting response from different instances of our application.