pythondockerhaproxy

Getting '503 Service Unavailable' while running HAProxy in Docker


I'm new to HAProxy. I'm trying to run my Python Service's Docker Image in multiple instances and use HAProxy to Balance the Load.

(python service is running at 8090 via fastapi)

Firstly, I've created docker image of my python service.

Then I've created docker-compose.yml:

version : '3'

services:
    elb:
        image: haproxy
        ports:
            - "8100:8100"
        volumes:
            - ./haproxy:/usr/local/etc/haproxy
    pythonapp1:
        image: my-python-service:0.0.1
        ports:
            - "8090:8090"

Then I've created HAProxy folder and inside that folder I've created haproxy.cfg:

# haproxy.cfg
frontend http
    bind *:8100
    mode http
    timeout client 10s
    use_backend all

backend all
    mode http
    
    server s1 pythonapp1:8090

Then I run docker compose up and sent request to http://localhost:8100 - it worked perfectly fine.

But when I added more containers it gave me error.

version : '3'

services:
    elb:
        image: haproxy
        ports:
            - "8100:8100"
        volumes:
            - ./haproxy:/usr/local/etc/haproxy
    pythonapp1:
        image: craft-text-detection-python-service:0.0.1
        ports:
            - "8090:8090"
    pythonapp2:
        image: craft-text-detection-python-service:0.0.1
        ports:
            - "8091:8090"
# haproxy.cfg
frontend http
    bind *:8100
    mode http
    timeout client 10s
    use_backend all

backend all
    mode http
    
    server s1 pythonapp1:8090
    server s2 pythonapp2:8091

Now when I run docker compose up and hit request for the first time at http://localhost:8100 it works fine. But when hit again at http://localhost:8100 it gives me 503 Service Unavailable.

Guide me is it problem of port setting or something else?


Solution

  • There is no need to specify ports for pythonapp1 and pythonapp2 in docker compose

    Your docker compose should look like:

    version : '3'
    
    services:
        elb:
            image: haproxy
            ports:
                - "8100:8100"
            volumes:
                - ./haproxy:/usr/local/etc/haproxy
        pythonapp1:
            image: craft-text-detection-python-service:0.0.1
            
        pythonapp2:
            image: craft-text-detection-python-service:0.0.1
        
    

    And update your haproxy.cfg as well, which should look like:

    # haproxy.cfg
    frontend http
        bind *:8100
        mode http
        timeout client 10s
        use_backend all
    
    backend all
        mode http
        
        server s1 pythonapp1:8090
        server s2 pythonapp2:8090