Scale docker application with HAProxy

To be able to scale a docker service a instance can not have a static port definition for export.
HAProxy is a simple way to load balance traffic to diffrenet docker containers.

 
The concpet is that every container expose a port (can be the same for all), but not publish any ports.
HAProxy exports one port which gets distributed to all containers.

 
The application has to set the environment varialbe SERVICE_PORTS (for swarm). That’s all.

 
See this docker-comose (for stack):

  
version: '3'
services:
  app:
    build: .
    image: rit_app
    environment:
      - SERVICE_PORTS=80
    expose:
      - 80
    networks:
      - nw-rit
  ha:
    image: dockercloud/haproxy
    depends_on:
      - app
    environment:
      - BALANCE=roundrobin
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - "8102:80"
    networks:
      - nw-rit
    deploy:
      placement:
        constraints: [node.role == manager]
...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.