Flask is a Python lightweight microweb application framework. It developed using Jinja and Werkzeug, and it is the easiest and fastest way of building web applications using python. It can build both 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 also prevents one application server from being overworked/overloaded/overwhelmed with concurrent requests from users and crashing the server. Additionally, it ensures that if one application server crashes, it will redirect the incoming network traffic to the other online servers.
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 will maximize the application speed and resource utilization. You can read this post that covers Nginx here on Sweet Code blog.
Docker Compose allows Docker users 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: You will use Docker Compose to configure the Flask application and Nginx load balancer containers. You can read this post that covers Docker Compose here on Sweet Code blog.
- Python
- Flask Micro web Framework: You will use Flask to build a Flask web application. You can read this post that covers Flask here on Sweet Code blog.
After setting these two up, let’s start building a Flask web application.
Building a Flask Web Application
To build a Flask web application:
- Open your Code editor and create a new project directory named ‘flask_load_balancing’.
- In the ‘flask_load_balancing’ project directory create a new file named ‘app.py’.
- Open the ‘app.py’ and add the following Flask code:
# 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 app.py
Console output:
The command will start the Flask Web Application on: http://127.0.0.1:5000/`:
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 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:
#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 an Nginx.conf file to configure the Nginx Load balancer. In the “flask_load_balancing” project directory, create an “nginx.conf” file:
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 YML code to create the Flask application Service:
# Version of Docker-compose for this Flask Load balancing application version: '4.0' 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 YML code to create the Nginx load balancer service:
# 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 Container with Nginx Load Balancer
To launch the Flask Application container with the Nginx load balancer, run this command:
docker-compose up --build -d --scale app=3
Console Output:
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.
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. Then you 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.