Using haproxy to load balance tomcat instances with Docker


Today I wanted to set up my local environment with a load balancer for my tomcats. To get started, I took a look at the dockercloud-haproxy image and the provided example configuration for docker-compose:

version: '2'
services:
  web:
    image: dockercloud/hello-world
  lb:
    image: dockercloud/haproxy
    links:
      - web
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - 80:80

You can run this example right away and open http://localhost to see the load balance hello-world page. To scale the web service, use docker-compose scale web=2 and reload the page a couple of times. You should see the hostname changing with each request, as haproxy is using a roundrobin load-balancing strategy by default in this image.

To make this a bit more specific to my use case, I decided to replace the hello-world image with the official tomcat image. Because I like small images, I also decided to use tomcat:alpine.

version: '2'
services:
  web:
    image: tomcat:alpine
    volumes:
      - ./index.jsp:/usr/local/tomcat/webapps/ROOT/index.jsp:ro
  lb:
    image: dockercloud/haproxy
    links:
      - web
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - 80:80

I added a volume and a custom JSP to make this example a bit more my own:

<html>
<body>
This is host <strong><%= java.net.InetAddress.getLocalHost().getHostName() %></strong>.
</body>
</html>

If you run this example with docker-compose up -d && docker-compose scale web=2 you can again reload http://localhost a couple of times and you should see a message like This is host 76ce0ef9f152.. Congratulations, you now have (very simple) load balanced tomcat setup. 😀

Next steps would be to replace the simple tomcat setup with a real cluster to get session replication.

Weitere Artikel

Der letzte PR

Fahrrad geklaut

Erstes Review des Sony XZ Premium

Unterschiedliche Sichtweisen

Vorbestellt: Sony Xperia XZ Premium

Giants Run 2017

Gelesen: SciFi Serie 'The Expanse'

Neue Tastatur: Pok3r Vortex RGB

New minecraft survival mod for 1.11.2

Änderungen beim Flug mit Quadkoptern