Dockerized Load Balancer Implementation
Load balancing architecture is to increase the availability and reliability of applications, so if a certain number of clients request some number of resources to backends, Load balancer stays between them and route the traffic to the backend
To setup Nginx as a load balancer (Containerized As well As Bare host LB) for backend servers, follow these steps:
- Open the Nginx configuration file with elevated rights
- Define an upstream element and list each node in your backend cluster
- Map a URI to the upstream cluster with a proxy_pass location setting
- Restart the Nginx server to incorporate the config changes
- Verify successful configuration of the Nginx load balancer setup
As your Nginx configuration grows, this will require changes to the file in which the upstream element and the proxy_pass location setting must be configured.
For this Containerized load balancer example, we will edit the file named default which is mounted in the container /etc/nginx/conf.d/default.conf directory.
./web1/Dockerfile
FROM nginx
WORKDIR /usr/share/nginx/html
COPY index.html /usr/share/nginx/html
./web1/index.html
THIS IS WEB1 APPLICATION
./web2/Dockerfile
FROM nginx
WORKDIR /usr/share/nginx/html
COPY index.html /usr/share/nginx/html
./web2/index.html
THIS IS WEB1 APPLICATION
./nginx/Dockerfile
FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
./nginx/nginx.conf
upstream loadbalancer {server web1:80;
server web2:80;
}
server {
location / {
proxy_pass http://loadbalancer;
}}
docker-compose.yml
version: "3"
services:
web1:
build: ./web1
ports:
- "8080:80"web2:
build: ./web2
ports:
- "8088:80"lb:
depends_on:
- web1
- web2
build: ./nginx
ports:
- "80:80"
Now run the following command and type the url on the browser to check connectivity (http://loadbalancer)
docker-compose up -d
You may also find the complete code on the below Github repo:
***************************************************
For this Nginx load balancer (S/w LB) example, we will edit the file named default which is in the /etc/nginx/sites-available/default folder.
Enjoy!!!