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?

coffee-grinder
  • 26,940
  • 19
  • 56
  • 82
Alok Kumar Singh
  • 2,331
  • 3
  • 18
  • 37
  • This sounds like 2 separate questions. You probably should edit the post to have only 1 in it and ask the other question separately – fejese Feb 04 '15 at 07:33
  • I have made it 2 questions. – Alok Kumar Singh Feb 04 '15 at 07:43
  • you have qualifier for the `docker run` command to allocate more or less CPU and memory to your containers, see https://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: , where unit = b, k, m or g)` – user2915097 Feb 04 '15 at 08:06

1 Answers1

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;
    }
}
}
Michael
  • 10,124
  • 1
  • 34
  • 49
  • 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. – Alok Kumar Singh Feb 04 '15 at 10:41
  • Just put nginx on a server in front of the host. Aside from using service discovery, this is the simple way. – Michael Feb 04 '15 at 16:03
  • 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? – Alok Kumar Singh Feb 05 '15 at 07:29
  • Please edit your question, that is not what you asked originally and that is a very different question. – Michael Feb 05 '15 at 17:40