Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx load balancer - Docker compose

I have a simple flask app running on port 5000 inside the container , and i'm trying to add nginx load balance to scale the app(3 instances)

Here is my docker-compose file :

version: "3.7"

services: 
    chat-server:
        image: chat-server
        build: 
            context: .
            dockerfile: Dockerfile
        volumes: 
            - './chat_history:/src/app/chat_history'
        networks: 
            - "chat_net"

    ngnix-server:
        image: nginx:1.13
        ports: 
            - "8080:80"
        volumes: 
            - './ngnix.conf:/etc/ngnix/nginx.conf'
        networks: 
            - "chat_net"
        depends_on: 
            - chat-server

networks: 
    chat_net:

And here is my nginx.conf file :

events { worker_connections 1024;}

http {
    upstream app {
        server chat-server_1:5000;
        server chat-server_2:5000;
        server chat-server_3:5000;

    }
}

server {
    listen 80;
    location / {
        proxy_pass http://app;
    }
}

both services are on the same chat_net network , but when i hit localhost:8080 on my browser im getting the nginx default page , why is that? what am i missing ?

like image 443
NyaSol Avatar asked Jan 25 '23 08:01

NyaSol


1 Answers

You have a typo and are not mounting in your nginx.conf file correctly.

You spell it ngnix in a couple of places in your volumes section and the container runs with the default config (hence default home page).

Once you fix that, you will probably hit the error mentioned by @Federkun (nginx won't be able to resolve the 3 domain names you're proxying).

You also have your server directive in the wrong place (it needs to be within the http section).

This should be the modified version of your file:

events { worker_connections 1024;}

http {
    upstream app {
        server chat-server:5000;
    }
    server {
        listen 80;
        location / {
            proxy_pass http://app;
        }
    }
}

Notice this is better than needing nginx to be aware of the replica count. You can run docker-compose up with --scale chat-server=N and resize at anytime by running the same command with a different N without downtime.

like image 179
Jedi Avatar answered Jan 31 '23 05:01

Jedi