Flask Application Load Balancing using Docker Compose and Nginx
Tags: docker, webdev, python, devops
Published: 2023-07-30 15:13:36

Flask is a Python lightweight microweb application framework. It developed using [Jinja](https://www.palletsprojects.com/p/jinja/) and [Werkzeug](https://www.palletsprojects.com/p/jinja/). It is the easiest and fastest way of building web applications using Python. It can build simple and complex applications. 

Load balancing in computing refers to the process of distributing requests and incoming network traffic to the different application servers. 
A modern application or website can serve many users or clients concurrently. Load balancing ensures that the concurrent requests from application users are distributed/routed across all the application servers. It prevents one application server from being overworked/overloaded/overwhelmed with concurrent requests from users and crashing the server. It also ensures that if one application server crashes, it will redirect the incoming network traffic to the other servers that are online.

Different load balancers can be implemented/configured to sit between your servers and the client/user requests. In this tutorial, you will use Nginx as a load balancer for the Flask application. [Nginx](https://www.nginx.com/resources/glossary/nginx/) will maximize the application speed and resource utilization.  

[Docker Compose](https://docs.docker.com/compose/) allow Docker user to run multiple containers for a single application. Docker Compose will configure the application containers as services. You will use Docker Compose to configure the Flask application and Nginx load balancer containers. In this tutorial, you will first build a Flask web application. You will then implement Flask application load balancing using Docker Compose and Nginx.

### Prerequisites
Before you begin, make sure you have the following:
- [Docker Compose](https://docs.docker.com/compose/): You will use Docker Compose to configure the Flask application and Nginx load balancer containers.
- [Python](https://www.python.org/)
- [Flask Micro web Framework](https://flask.palletsprojects.com/): You will use Flask to build a Flask web application.

After setting these two up, let's start building a Flask web application.

### Building a Flask Web Application
To build a Flask web application:
Step 1: Open your Code editor and create a new project directory named `flask_load_balancing`. 
Step 2: In the `flask_load_balancing` project directory create a new file named `app.py`.
Step 3: Open the `app.py` and add the following Flask code:

```flask
# Importing Flask Python micro web framework
from flask import Flask
app = Flask(__name__)
# This is a Flask application route
@app.route("/")

# This is a FLask application function
def load_balancing():
    return "<p><h3>Flask Application Load Balancing using Docker Compose and Nginx<h3></p>"

if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)
```
To run the Flask web application, run this command:

```python 
python app.py
```
Console output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vim77isakk8nevwya12x.png)

The command will start the Flask Web Application on: `http://127.0.0.1:5000/`:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3tda298ai381qcqrrqi.png)

### Implementing Flask Application Load Balancing using Docker Compose and Nginx
To implement Flask application load balancing using Docker Compose and Nginx, follow these steps:

#### Step 1: Create a `requirements.txt` file
To create a `requirements.txt` file with all the Flask application dependencies, run this `pip` command:

```pip
pip freeze > requirements.txt
```

#### Step 2: Create a Dockerfile
You will create a new Dockerfile in the `flask_load_balancing` project directory. Docker will use the Dockerfile to build the Flask application Docker image:

```dockerfile
#Base image for the Flask application
FROM python:3.10-alpine
#Working Directory for the Flask application
WORKDIR /flask
#It will copy the Flask application files and the requirements.txt file
COPY . /flask
#It will install the dependencies for the Flask application
RUN pip install -r requirements.txt
#It starts the Flask application on Port 5000
CMD python app.py
```
#### Step 3: Configure Nginx Load Balancer
You will create a nginx.conf file to configure the Nginx Load balancer. In the "flask_load_balancing" project directory, create an "nginx.conf" file:

```nginx
events {
    worker_connections 1000;
}

http {
    server {
        listen 80;

        listen / {
            proxy_pass http:/app:5000
        }
    }
}
```
#### Step 4: Create a docker-compose.yml file
The docker-compose.yml will contain the configurations for the Flask application and Nginx load balancer containers. In the "flask_load_balancing" project directory, create a "docker-compose.yml" file. In this file, we will add two services. The first service will be for the Flask web application container and the second one will be for the Nginx load balancer container.

#### Flask application Service
In the "docker-compose.yml" file, add the following to the YML code to create the Flask application Service:

```yml
# Version of Docker-compose for this Flask Load balancing application
version: '4'
services:
  # Add the Flask application Service
  app:
  # Location of the Flask Application dockerfile
    build: 
      context: app
    ports:
       # Flask application Host port: Flask Container port
      - '5000'
```
It will create a Flask application Service called `app`. It will use the Dockerfile to create the Flask application container. This service will run on port 5000.

#### Nginx load balancer Service
In the "docker-compose.yml" file, add the following to the YML code to create the Nginx load balancer service:

```yml
 # nginx service
  nginx: 
  # The official nginx Docker from Docker Hub. For building the nginx container
    image: nginx:latest
    volumes:
    # Volume for the nginx Docker container
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    # The nginx service depends on the Flask application service (It will link the Flask web application and Nginx Load balancer)
    depends_on:
      - app
    # ports for the nginx application service
    ports:
    # Port for the Nginx load balancer
      - "80:80"
```
It will create an Nginx load balancer Service called `nginx`. It uses the official Nginx Docker from Docker Hub to build the Nginx container. This service will run on port 80. The Nginx service depends on the Flask application service. This will link the Flask web application and the Nginx Load balancer.

### Launching the Flask Web Application with Nginx load balancer
To launch the Flask Application with the Nginx load balancer, run this command:

```docker-compose
docker-compose up --build -d --scale app=3
```
Console Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9502070vvhozmphv2sjp.png)

The command will start the Flask Application with the Nginx load balancer. It will also run three instances of the Flask Web application. The Nginx load balancer will equally distribute/route requests and incoming network traffic to the three Flask web application instances. 

To see the Flask Web Application with Nginx load balancer, go to  http://127.0.0.1:5000/. You can then refresh the webpage for the Nginx load balancer to route requests to the three Flask web application instances.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l76cwre0v65d2hqq4tcx.png)

You have successfully implemented Flask Application Load Balancing using Docker Compose and Nginx. 

### Conclusion
In this tutorial, you have learned how to implement Flask Application Load Balancing using Docker Compose and Nginx. Load balancing is important when running applications in production. It ensures the application operates efficiently and is reliable. 

You used Nginx as the load balancer. It equally routed requests and incoming network traffic to the three Flask web application instances. You then refreshed the webpage for the Nginx load balancer to route requests to the three Flask web application instances. You have also learned how to use Docker Compose to run the Flask application container and the Nginx load balancer container. Thanks for reading this tutorial and I hope you find it helpful.