This project provides a dynamic load balancer using Docker Compose, which can automatically adjust to multiple backend services and distribute traffic based on configurable weights.
- Dynamic Backend Configuration: Add, update, and remove backend services at runtime using a simple command-line interface (CLI).
- Automated
docker-compose.ymlGeneration: The CLI tool generates or updates thedocker-compose.ymlandbackends.jsonconfig files automatically. - Automatic Docker Compose Build & Up: The
initandaddcommands automatically triggerdocker-compose up --buildto build and start the containers, ensuring everything is up-to-date. - Health Checks: The load balancer performs periodic health checks to ensure only healthy backends serve requests.
- Weighted Round-Robin Load Balancing: Requests are distributed to backends based on their weight, simulating a weighted round-robin algorithm.
Before using the project, make sure you have the following:
- Docker: Install Docker
- Docker Compose: Install Docker Compose
- Python 3.x: Download Python
cli.py: A Python script that handles backend management and automaticdocker-compose.ymlgeneration.Dockerfile.backend: Dockerfile for building the backend service containers.docker-compose.yml: Automatically generated or updated by thecli.pyscript to define services, including the load balancer and backends.config/backends.json: Config file containing backend details, including URLs and weights.
-
Clone the repository to your local machine:
git clone https://github.com/yourusername/load-balancer.git cd load-balancer
The init command initializes the backend services, generates the necessary configuration files (docker-compose.yml and backends.json), and starts the services using docker-compose up --build.
###backend1:9001:2: Defines a backend service backend1 running on port 9001 with weight 2.
```bash
python cli.py init backend1:9001:2 backend2:9002:3
The add command allows you to add new backends to the existing configuration and restart the services.
```bash
python cli.py add backend3:9003:1
Removes a backend from the configuration by either its name or port.
Usage:
python cli.py remove <backend_name_or_port>