How to load balance containers?
Asked Answered
A

1

13

How to load balance docker containers running a simple web application?

I have 3 web containers running in a single host. How do I load balance my web containers?

Adjure answered 4/2, 2015 at 7:23 Comment(3)
This sounds like 2 separate questions. You probably should edit the post to have only 1 in it and ask the other question separatelyGilburt
I have made it 2 questions.Adjure
you have qualifier for the docker run command to allocate more or less CPU and memory to your containers, see docs.docker.com/reference/commandline/cli/#run for the CPU you have -c, --cpu-shares=0 CPU shares (relative weight) and for the memory -m, --memory="" Memory limit (format: <number><optional unit>, where unit = b, k, m or g)Prearrange
J
11

Put a load balancer, such as haproxy or nginx can even do the job.

Decent Haproxy Documentation

Nginx Howto

Either way, put the load balancer on the host or on a different server that can access the exposed ports on the containers. Nginx will probably be simpler for your needs.

To setup basic nginx load balancing:

http {
upstream myapp1 {
    server CONTAINER_APP0_IP:PORT;
    server CONTAINER_APP1_IP:PORT;
    server CONTAINER_APP2_IP:PORT;
}
server {
    listen 80;
    location / {
        proxy_pass http://myapp1;
    }
}
}
Jurassic answered 4/2, 2015 at 8:6 Comment(4)
thanks for the reply. yes this solution would work in a one docker host situation. But how will it work when my docker container is running in a container platform like coreos ? I want to achieve autoscaling and auto reconfiguration of the proxy depending on the load.Adjure
Just put nginx on a server in front of the host. Aside from using service discovery, this is the simple way.Jurassic
thanks, we can use nginx/haproxy to forward the requests to the containers. But I want to know how auto-reconfiguration of proxy server happens depending on the state of containers in etcd ? Do we need to use some other tool/(or write it) to solve it or do we already have a solution to this problem?Adjure
Please edit your question, that is not what you asked originally and that is a very different question.Jurassic

© 2022 - 2024 — McMap. All rights reserved.