Issue
I have a simple Flask application that runs on port 5000 that I want to scale up to 3 instances. I want the Nginx load balancer to balance clients based on their associated key provided in the url (i.e. "localhost/get?key=1" will always route to server 1 and "...key=2" always routes to server 2). However, when I use docker-compose to build and scale my app, it is using the Round Robin load balancing strategy instead of the
hash $arg_key consistent
strategy I have specified in the nginx.conf file.
I am not sure why this is happening.
nginx.conf:
events {}
http {
# Define the group of servers available
upstream backend {
hash $arg_key consistent;
server app:5000;
}
server {
# Server group will respond to port 80
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
Docker-compose:
version: "3.7"
services:
app:
build:
context: components
dockerfile: Dockerfile
environment:
- DB_USERNAME=${DB_USERNAME}
- DB_PASSWORD=${DB_PASSWORD}
networks:
- kvnetwork
nginx:
build:
context: nginx
dockerfile: Dockerfile
volumes:
- './nginx/nginx.conf:/etc/nginx/nginx.conf'
ports:
- "80:80"
networks:
- kvnetwork
depends_on:
- app
networks:
kvnetwork:
I would appreciate any help.
Solution
To scale your application to 3 instances while using Docker Compose, you should use the scale command after bringing up your services with docker-compose up. Here's an example command to scale your app service to 3 instances:
docker-compose up -d --scale app=3
nginx then needs to be configured to use the different instances. i.e.
upstream backend {
hash $arg_key consistent;
server app1:5000;
server app2:5000;
server app3:5000;
}
Answered By - Gabriel Ramuglia
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.