1

I am trying to load balance 2 Python apps using Docker and Nginx.

App 1 is run using UWSGI and App 2 is run using default Python Server (I used default server for App 2 just for testing purposes. At the end of the day both should use UWSGI)

The hierarchy is as follows:

-app1
---app.py
---Dockerfile
---app.ini

-app2
---app.py
---Dockerfile

-nginx
---Dockerfile
---nginx.conf

-docker-compose.yml

app.py (for both app1 and app2 is the same)

from flask import request, Flask
import json

app1 = Flask(__name__)

@app1.route('/')
def hello_world():
    return 'Salam alikom, this is App1 (App2 for app2) :)'

if __name__ == '__main__':
    app1.run(host='0.0.0.0')

app1/Dockerfile

FROM python:3
COPY ./requirements.txt /requirements.txt
WORKDIR /
RUN pip install -r requirements.txt
COPY . /
ENTRYPOINT ["uwsgi", "app.ini"]

app1/app.ini

[uwsgi]
wsgi-file = app.py
callable = app
socket = :5000
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true

app2/Dockerfile

FROM python:3
COPY ./requirements.txt /requirements.txt
WORKDIR /
RUN pip install -r requirements.txt
COPY . /
ENTRYPOINT [ "python3" ]
CMD [ "app.py" ]

nginx/nginx.conf

upstream loadbalancer {
    least_conn;
    server 192.168.100.2:9001;
    server 192.168.100.2:9002;
}
server {
    location / {
        proxy_pass http://loadbalancer;
    }
}

docker-compose.yml

version: '3'
services:
  app1:
    build: ./app1
    ports:
      - "9001:5000"
  app2:
    build: ./app2
    ports:
      - "9002:5000"
  nginx:
    build: ./nginx
    ports:
      - "9003:80"
    depends_on:
      - app1
      - app2

When i run my docker compose file, containers for app 1, app 2 and the nginx server are created. When i access the load balancer on port 9003, only app 2 shows. When i manually go to port 9001, i get the error message invalid request block size: 21573 (max 4096)...skip for app 1.

If i use default Python server for app 1 too then the load balancing works fine but i don't want to use that since the default python server is only for development environment.

Can someone please help ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.