Fork me on GitHub
10 September 2015

Load balancing is important for many applications. A popular open source load balancer is HAProxy. Here’s how I used an HAProxy container to automatically discover and load balance a simple web application.

Simple Load Balancing

In an earlier post I created a Simple Load Balanced Application with Docker. This used an HAProxy container available in Docker Hub. In sumamry what was needed was:

docker run -t -d -p 8080:8080 --name=stage_rest rest
docker run -t -d --link stage_rest:rest --name=stage_web1 web
docker run -t -d --link stage_rest:rest --name=stage_web2 web
docker run -d -p 80:80 --name=stage_web --link stage_web1:stage_web1 --link stage_web2:stage_web2 tutum/haproxy

The problem with this approach is that you can’t add new web containers without restarting the proxy container. Not a huge problem but it would be nice if the proxy auto-discovered these containers and removed them when they went offline.

Dynamically Configured HA Proxy Container

In order to build a proxy that could automatically included or removed web continares as thaey came and went I used another container that can be found in Docker Hub, eeacms/haproxy. This container includes some scripts that automatically configure HAProxy to load balance across available http servers.

In order to test this I used the same demo application as in the earlier post - a simple PHP web front-end and a Java back-end. In order to more easily test the scaling up and down of the application I also took the opportunity to write a Docker Compose file for the application, which looks like this:

  image: eeacms/haproxy
    - "80:80"
    - "1936:1936"
    - web
  restart: always
  build: web
    - rest
  restart: always
  build: java
    - "8080:8080"
  restart: always

No we can start the application with:

docker-compose up -d

Visiting http://localhost will show you that the application is working. Refreshing the page will result in the same hostname every time since there is currently only one host. Create another two web hosts with the following command:

docker-compose scale web=3

Now refreshing your browser will result in responses from three different hosts.You can scale the application down to two hosts with:

docker-compose scale web=2

The eeacms/haproxy container has lots of configuration options, why not take a look.

My colleague Ahmet recommended I also look at Interlock - I thought I’d drop a note here to remind me (and you).

Related Posts

blog comments powered by Disqus