A Beginner's Guide to Setting Up HAProxy with Open Web UI in Docker

Managing network traffic for containerized applications can be complex. HAProxy offers a robust solution for load balancing and proxying requests to your Docker containers. This guide will walk you through installing HAProxy and setting up a practical example with Open Web UI.

Getting Started with HAProxy and Docker

HAProxy is a fast and reliable open-source reverse proxy and load balancer. For Docker projects, it directs incoming traffic to your containerized applications, distributing requests across multiple instances to improve performance and reliability. This setup enhances scalability and resilience.

Assumptions:

  • Basic Linux command-line familiarity.
  • A running Linux system (Ubuntu/Debian or CentOS/RHEL).
  • Docker installed and a simple web server container (e.g., Nginx) ready.

Installation:

For Ubuntu/Debian:

sudo apt update
sudo apt install haproxy -y

For CentOS/RHEL:

sudo yum install haproxy -y

Basic Configuration:

1. Start a simple Nginx Docker container:

docker run -d --name mywebserver -p 8080:80 nginx

2. Edit HAProxy configuration:

sudo nano /etc/haproxy/haproxy.cfg

3. Replace the file’s content with this minimal config:

global
    log /dev/log    local0
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

defaults
    log global
    mode http
    option httplog
    option dontlognull
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http_front
    bind *:80
    default_backend http_back

backend http_back
    server webserver1 127.0.0.1:8080 check

4. Restart HAProxy:

sudo systemctl restart haproxy
sudo systemctl enable haproxy

Verification:

Open your web browser and navigate to your Linux system’s IP address or hostname. You should see the Nginx welcome page served by your Docker container through HAProxy.

Common Failure Modes & Fixes:

  • HAProxy not starting: Check sudo journalctl -u haproxy.service for syntax errors in haproxy.cfg.
  • “503 Service Unavailable”: Verify your Docker container is running (docker ps) and listening on the correct port (8080) and that HAProxy backend points to the correct IP/port.
  • Firewall blocking: Ensure port 80 is open on your Linux firewall. For Ubuntu: sudo ufw allow 80/tcp. For CentOS: sudo firewall-cmd --add-port=80/tcp --permanent && sudo firewall-cmd --reload.

HAProxy with Open Web UI Example and Conclusion

Assumptions:

  • Docker and Docker Compose are installed.
  • You have basic knowledge of Docker and HAProxy concepts.

Step-by-step:

1. Create a Docker Compose file (docker-compose.yml) for Open Web UI and a sample application:

version: '3.8'
services:
  open-webui:
    image: ghcr.io/open-webui/open-webui:main
    ports:
      - "8080:8080"
    volumes:
      - ./open-webui-data:/app/backend/data
  sample-app:
    image: nginxdemos/hello:plain-text
    ports:
      - "8000:80"
  haproxy:
    image: haproxy:latest
    volumes:
      - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
    ports:
      - "80:80"
    depends_on:
      - open-webui
      - sample-app

2. Create HAProxy configuration (haproxy.cfg):

global
  log stdout format raw local0 info

defaults
  mode http
  timeout client 10s
  timeout server 10s
  timeout connect 5s

frontend http_front
  bind *:80
  acl is_open_webui path_beg /webui/

  use_backend open_webui_backend if is_open_webui
  default_backend sample_app_backend

backend open_webui_backend
  server open_webui open-webui:8080 check

backend sample_app_backend
  server sample_app sample-app:80 check

3. Start the services:

docker-compose up -d

Verification:

  • Access the sample app: curl http://localhost
  • Access Open Web UI: curl http://localhost/webui/

Common Failure Modes and Fixes:

  • HAProxy not forwarding correctly: Check HAProxy logs (docker logs haproxy). Verify service names and ports in haproxy.cfg match the docker-compose.yml.
  • “503 Service Unavailable”: Ensure backend containers are running and healthy. Check Docker logs for open-webui and sample-app. HAProxy health checks might be failing due to incorrect port or unhealthy container.

Conclusion: HAProxy simplifies network management for Docker projects, acting as a robust reverse proxy. This setup demonstrates how to route traffic to different containers based on URL paths, improving reliability and control. Further applications include advanced load balancing for scalability and SSL termination for enhanced security.


Leave a Reply

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