Thursday, December 26, 2024
Google search engine
HomeGuest BlogsLoad Balancing Flask Application using Nginx and Docker

Load Balancing Flask Application using Nginx and Docker

Load balancing means efficiently distributing the incoming traffic to different server instances. Nginx is open-source software that can be used to apply load balancing to backend systems. Nginx also can be serve services such as reverse proxy, caching, web server, etc.

Docker is a tool that gives a virtualization solution for a complete software package delivery by packaging it into containers. A container is a component that bundles the application along with its dependencies, libraries, and configuration files together and ships the software ready to be worked on any machine with an easy installation process.

Docker Compose is used to run systems having two or more containers. It configures the interconnection between different containers and executes them all in a single command.

We will first run a simple flask instance through docker. Then we will reconfigure the created instances by adding an Nginx load balancer and make the app scalable.

Part 1: Running Flask instance through Docker

Step 1: First, make sure to install Docker into your system.

Step 2: Create a project directory named ‘load_balance_tutorial‘ and navigate it into

$ mkdir load_balance_tutorial
$ cd load_balance_tutorial

Step 3: Create an ‘app‘ directory and navigate in it

$ mkdir app
$ cd app

Step 4: Create a virtual environment and activate it

$ python3 -m venv venv
$ source venv/bin/activate

Step 5: Install flask

$ pip3 install flask

Step 6: Create a boilerplate application for a flask with the file name ‘app.py

from flask import Flask

app = Flask(__name__)

# Route for the default page
@app.route("/")
def home():
    # Display message
    return "<center><h3>Welcome to GFG</h3></center>"

if __name__ == '__main__':
    app.run('0.0.0.0')

Step 7: Create a ‘requirements.txt‘ file

$ pip3 freeze > requirements.txt

Step 8: Creating a ‘Dockerfile

# Pick a low configuration python base image
FROM python:alpine

#  Expose the port 5000 of the docker container
EXPOSE 5000

# Create a folder app in container and work inside it
WORKDIR /app

COPY requirements.txt .

# Install all the requirements
RUN pip3 install -r requirements.txt

# Copy all the flask project files into the WORKDIR
COPY . .

# Execute flask application inside the container
CMD python3 app.py

Step 9: Create a ‘.dockerignore‘ file to ignore the venv folder

venv

Now we have successfully configured our flask application with the docker services. Now the next part is to build the Nginx load balancer.

Part 2: Configure Nginx with Docker

Step 1: Go to the ‘load_balance_tutorial‘ directory

$ cd ..

Step 2: Create a new directory named ‘Nginx‘ and navigate to it

$ mkdir nginx
$ cd nginx

Step 3: Create a configuration file for Nginx named ‘Nginx. conf‘. This configuration file will serve as the main file for the load balancing application. By default, Nginx will use the round-robin method for server allocation to requests.

# events are used to set general configurations on how
# nginx will handle the connection requests
events {}

http {
    # Define the group of servers available
    upstream app {
        server app;
        server load_balance_tutorial_app_1:5000;
        server load_balance_tutorial_app_2:5000;
    }
    server {
        # Server group will respond to port 80
        listen 80;
        server_name app.com;
        location / {
            proxy_pass http://app;
        }
    }
}

Step 4: Create a new ‘Dockerfile

FROM nginx

# Override the default nginx configuration file
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf

With this, we have successfully configured the load balancer and have attached it to the docker container. Now in the next part, we will combine the flask application and the Nginx load balancer to run simultaneously using docker-compose.

Part 3: Setting up the docker-compose

Step 1: Go to the load_balance_tutorial directory’

$ cd ..

Step 2: Create a file name ‘docker-compose.yml

Step 3: Edit the docker-compose.yml file as follows

version: '3.7'

services:
  # Build the app services
  app:
    build: app
  nginx:
    container_name: nginx
    build: nginx
    # Bind the port 80 of container to machine port 80
    ports:
      - 80:80
    # Make app as nginx dependency service
    depends_on:
      - app

With this, the services ‘app‘ and ‘Nginx‘ have been linked together.

Part 4: Running the system

Step 1: Run the following command to initiate the services. The system will run two instances of the flask application

$ docker-compose up --build -d --scale app=2

Step 2: Check the containers by using the following command:

$ docker ps --format '{{.Image}} {{.Names}}'

The above command must give a similar output as shown below

docker ps –format ‘{{.Image}} {{.Names}}’

Step 3: Go to http://localhost in your browser and see the output.

 

Step 4: To check whether the two instances are working properly or not, that is whether they are load-balanced or not would check the log files for the flask containers and see for 200 status codes for each refresh in the server for both instances.

$ docker logs load_balance_tutorial_app_1 -f
$ docker logs load_balance_tutorial_app_2 -f

You should be getting a similar type of output.

docker logs load_balance_tutorial_app_1 -f

docker logs load_balance_tutorial_app_2 -f

Finally, the flask application now has two instances and both of them are load balanced using Nginx and Docker.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Previous article
Next article
RELATED ARTICLES

Most Popular

Recent Comments