HAProxy con Docker

Balanceador: Implementación de servicio HAProxy con Docker

Para este ejemplo se utiliza un contenedor que genera los certificados ssl/tls en un proceso interno. Los dominios se definen en la reglas ACL del frontend en el archivo de configuracion (haproxy.cfg). En el docker-compose.yml se exponen 2 servidores nginx para fines de prueba.

haproxy.cfg

# CONFIGS APPLIED GLOBALLY
global
    maxconn 32768
    daemon

# CONFIGS APPLIED BY DEFAULT ON FRONTENDS AND BACKENDS
defaults
    mode    http
    retries 3
    timeout connect     5s
    timeout client     50s
    timeout server    450s

# FRONTENDS HTTP REDIRECT TO HTTPS
frontend http-in
    bind *:80
    acl http ssl_fc,not
    http-request redirect scheme https if http

# FRONTENDS HTTPS
frontend https-in
    bind *:443
    mode http
    acl host_subdomain_yourdomain hdr(host) -i subdomain.yourdomain.com
    use_backend cluster_subdomain_yourdomain if host_subdomain_yourdomain

# BACKENDS HTTP
backend cluster_subdomain_yourdomain
    mode http
    balance roundrobin
    option forwardfor
    server node1 nginx-01:80 check
    server node2 nginx-02:80 check

docker-compose.yml


version: "3.5"

# Docker Volumes
volumes:
    letsencrypt_logs:
        name: letsencrypt_logs
    letsencrypt_data:
        name: letsencrypt_data

# Docker services
services:
    lets-haproxy:
        image: achetronic/lets-haproxy:latest
        container_name: lets-haproxy
        restart: always
        tty: true
        stdin_open: true
        environment:
            ENVIRONMENT: "staging" # staging | production
            ADMIN_MAIL: "tucorreo@tudominio.com"
            SKIP_CREATION: "false"
        volumes:
            - ./haproxy.cfg:/root/templates/haproxy.user.cfg:ro
            - letsencrypt_logs:/var/log/letsencrypt
            - letsencrypt_data:/etc/letsencrypt
        ports:
            - "80:80"
            - "443:443"

    nginx-01:
        image: nginxdemos/hello
        container_name: nginx-01
        restart: always
        tty: true
        stdin_open: true
        expose:
            - "80"

    nginx-02:
        image: nginxdemos/hello:plain-text
        container_name: nginx-02
        restart: always
        tty: true
        stdin_open: true
        expose:
            - "80"

Iniciar servicios:

docker-compose run -d

Prueba rápida con curl en terminal cliente:

curl https://subdomain.yourdomain.com

¿Tienes alguna consulta?
Puedes contactarme enviándome un mensaje desde aquí.