Udemy
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
HAProxy for Beginners
Rating: 4.4 out of 5(1,158 ratings)
5,060 students

HAProxy for Beginners

Load Balancing and High Availability
Created byHadi Alnabriss
Last updated 10/2026
English
English [Auto],Spanish [Auto],

What you'll learn

  • Students will be able to build a high available and load balanced services

Course content

1 section • 24 lectures • 2h 4m total length
  • Load Balancing Preface4:21

    Hello, and welcome to this course on HAProxy, one of the most widely used open-source load balancers and reverse proxies.

    Concept lecture: why one server is not enough, scaling up versus scaling out, and the limits of round-robin DNS.

    Chapters:
    0:00 Introduction
    0:24 Every Server Has a Limit
    0:50 Tune It or Scale Up?
    1:11 Scale Out: Identical Servers
    1:47 Round-Robin DNS
    2:15 DNS Answers Are Cached
    2:45 DNS Doesn't Know a Server Failed
    3:06 Put a Load Balancer in Front
    3:33 What DNS Couldn't Do
    4:05 Recap

  • What Is HAProxy?5:37

    In the previous lecture, we saw why we need a load balancer. Now let's look at what HAProxy actually is.

    Concept lecture: what HAProxy is, where it sits, and the HAProxy 3.4 LTS release used in this course.

    Chapters:
    0:00 Introduction
    0:14 One Tool, Many Roles
    0:28 A TCP Proxy
    0:54 An HTTP Reverse Proxy
    1:32 An SSL/TLS Terminator and Initiator
    2:07 A Load Balancer: TCP and HTTP Mode
    2:34 A Content-Based Switch
    2:58 Normalizers: Only Clean Traffic Passes
    3:36 Traffic Regulator and Abuse Protection
    4:11 Taking Work off Your Servers
    4:42 What HAProxy Is Not
    5:22 Recap

  • Why HAProxy?2:58

    Why do so many teams choose HAProxy? In this lecture, we'll look at five reasons.

    Concept lecture: the performance, reliability and features that make HAProxy a common choice.

    Chapters:
    0:00 Introduction
    0:47 Reliable
    1:15 Observable
    1:48 Controllable While It Runs
    2:09 Open Source, Long-Term Support
    2:43 Recap

  • HAProxy Tasks4:04

    Once HAProxy is running, what does it actually do? According to its documentation, exactly three things.

    Concept lecture: load balancing, health checks, SSL/TLS, observability and the Runtime API. Peers replicate stick tables; they are not a cluster.

    Chapters:
    0:00 Introduction
    0:24 Processing a Connection
    1:10 Health Checks
    1:57 Many Kinds of Checks
    2:27 Why a Second Load Balancer?
    2:58 Stick Tables and Peers
    3:49 Recap

  • Frontend and Backend2:36

    HAProxy's configuration is built from two main kinds of sections: frontends and backends. Let's see what each one does.

    Concept lecture: frontends, backends, ACLs and how a request moves through HAProxy.

    Chapters:
    0:00 Introduction
    0:10 Two Halves of a Proxy
    0:40 A Frontend in the Configuration
    1:07 A Backend in the Configuration
    1:28 One Frontend, Several Backends
    1:52 listen and defaults
    2:17 Recap

  • Scheduling Algorithms5:20

    A backend's load balancing algorithm decides which server gets each new connection or request. HAProxy offers many of them. Let's see how each one works, and when to use it.

    Concept lecture: roundrobin, static-rr, leastconn, first, source, uri, hdr, random and consistent hashing.

    Chapters:
    0:00 Introduction
    0:13 balance and Server Weights
    0:36 roundrobin
    1:09 When Requests Take Different Times
    1:38 leastconn
    2:07 random
    2:34 first
    3:03 source
    3:29 uri, url_param and hdr
    4:05 hash, rdp-cookie and Log Backends
    4:32 hash-type: map-based or consistent
    4:58 Which Algorithm When?

  • HAProxy Configuration7:31

    In this lecture, we'll walk through a complete HAProxy configuration for cloudfiles-lab.com, section by section.

    Concept lecture: a complete haproxy.cfg for cloudfiles-lab.com, section by section, including the five timeouts and the stats socket.

    Chapters:
    0:00 Introduction
    0:31 global
    1:18 stats socket: the Runtime API
    1:58 defaults
    2:47 The Five Timeouts
    3:10 timeout http-request
    3:47 timeout queue
    4:11 timeout connect
    4:45 timeout client and timeout server
    5:42 frontend
    6:12 backend
    6:44 Check, Then Reload
    7:09 Recap

    Commands and configuration from this lecture:

    # /etc/haproxy/haproxy.cfg (the example from this lecture)
    global
    log /dev/log local0
    maxconn 4000
    user haproxy
    group haproxy
    daemon
    stats socket /run/haproxy/admin.sock mode 660 level admin

    defaults
    mode http
    log global
    option httplog
    option dontlognull
    retries 3
    timeout http-request 10s
    timeout queue 1m
    timeout connect 5s
    timeout client 1m
    timeout server 1m

    frontend cloudfiles
    bind :80
    bind :443 ssl crt /etc/haproxy/certs/cloudfiles.pem
    http-request redirect scheme https unless { ssl_fc }
    default_backend web

    backend web
    balance roundrobin
    default-server inter 2s rise 4 fall 3
    server web1 10.0.0.11:80 check
    server web2 10.0.0.12:80 check
    server web3 10.0.0.13:80 check
    server web4 10.0.0.14:80 check backup

    # check, then apply
    sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
    sudo systemctl reload haproxy

  • Practical Setup5:14

    Welcome to the first practical lecture. From here on, we build everything for real: three web servers for cloudfiles-lab.com, behind one HAProxy load balancer.

    Hands-on, on real Azure VMs: one proxy (Ubuntu 26.04, HAProxy 3.4.7 from ppa:vbernat/haproxy-3.4) and three Apache/PHP web servers (Ubuntu 24.04). Round robin and health checks seen live.

    Chapters:
    0:00 Introduction
    0:13 Four Virtual Machines in Azure
    0:38 Prepare the Web Servers
    1:05 Add the CloudFiles Page
    1:44 Same Steps on web2 and web3
    1:54 Install HAProxy 3.4
    2:37 Keep a Copy of the Original
    2:53 Write Our Configuration
    3:22 Check, Then Reload
    3:42 Round Robin in the Browser
    3:57 Health Checks in Action
    4:24 Only Healthy Servers Get Traffic
    4:54 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    # web servers (web1, web2, web3)
    sudo apt update
    sudo apt install -y apache2 php libapache2-mod-php
    sudo rm -f /var/www/html/index.html

    # proxy: HAProxy 3.4 LTS
    sudo add-apt-repository -y ppa:vbernat/haproxy-3.4
    sudo apt install -y haproxy=3.4.*
    haproxy -v
    sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.orig

    # /etc/haproxy/haproxy.cfg (the global section keeps the package defaults)
    defaults
    mode http
    log global
    option httplog
    retries 3
    timeout http-request 10s
    timeout queue 1m
    timeout connect 5s
    timeout client 1m
    timeout server 1m

    frontend cloudfiles
    bind :80
    default_backend web

    backend web
    balance roundrobin
    default-server inter 2s rise 4 fall 3
    server web1 10.0.1.4:80 check
    server web2 10.0.1.5:80 check
    server web3 10.0.1.6:80 check

    sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
    sudo systemctl reload haproxy

    # health checks: stop and start Apache on web2
    sudo systemctl stop apache2
    sudo systemctl start apache2

  • Statistics and Monitoring3:53

    In this lecture, we'll turn on HAProxy's statistics page, and use it to watch our load balancer live.

    Hands-on: a statistics frontend on port 8404 protected by an IP allow-list, live server states, and the built-in Prometheus exporter at /metrics.

    Chapters:
    0:00 Introduction
    0:47 Check, Reload, and Test
    1:16 Testing the Guard
    1:33 The Statistics Page
    1:56 Reading the Backend
    2:14 Watching a Failure Live
    2:42 Metrics for Prometheus
    3:30 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    # added to /etc/haproxy/haproxy.cfg
    frontend stats
    bind :8404
    http-request deny unless { src -f /etc/haproxy/admin-ips.lst }
    http-request use-service prometheus-exporter if { path /metrics }
    stats enable
    stats uri /stats
    stats refresh 5s

    # /etc/haproxy/admin-ips.lst: one allowed IP (or network) per line, plus 127.0.0.1
    sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
    sudo systemctl reload haproxy

    # open http://YOUR-PROXY:8404/stats
    # from the proxy: fetch http://localhost:8404/metrics with curl -s and grep for haproxy_backend_active_servers

  • Scheduling Algorithms in Practice3:31

    In lecture 6, we saw HAProxy's scheduling algorithms in theory. Now let's try them for real, and watch the statistics page to see how each one shares the traffic.

    Hands-on, measured with ApacheBench (ab): roundrobin 10/10/10 in 9.6 s, weight 2 gave 20/10/10, leastconn 3/13/14 in 3.8 s with one slow server, and source kept one visitor on one server.

    Chapters:
    0:00 Introduction
    0:12 Two Things for Our Tests
    0:50 roundrobin
    1:22 Round Robin with Weights
    1:52 leastconn
    2:29 source
    2:59 What We Measured

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    sudo apt install -y apache2-utils
    ab -n 30 -c 6 http://localhost/report.php
    ab -n 40 http://localhost/

    # backend web, one algorithm at a time
    balance roundrobin
    server web1 10.0.1.4:80 check weight 2
    balance leastconn
    balance source

    sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
    sudo systemctl reload haproxy

  • TCP Mode and HTTP Mode4:09

    HAProxy can work in two modes: TCP mode and HTTP mode. In this lecture, we'll see what each one can see, and try both on our real servers.

    Hands-on: the same browser test in HTTP mode (a server chosen per request) and in TCP mode (one server per connection), with the log lines of each.

    Chapters:
    0:00 Introduction
    0:12 What Each Mode Can See
    0:55 Our Current Setup: HTTP Mode
    1:41 Switching to TCP Mode
    2:14 TCP Mode, Live
    2:52 Which Mode When?
    3:31 Back to HTTP Mode
    3:45 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    # defaults: switch to TCP mode
    mode tcp
    option tcplog
    # and keep the stats frontend in HTTP mode
    frontend stats
    mode http

    sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
    sudo systemctl reload haproxy
    sudo grep 'web/web' /var/log/haproxy.log | tail -n 3

  • The Real Client IP6:19

    Our web servers never see who is really visiting them. In this lecture, we'll see why, and fix it with HAProxy's forwardfor option, and a small change on the web servers.

    Hands-on: option forwardfor adds X-Forwarded-For, and Apache's mod_remoteip (RemoteIPInternalProxy) makes the web servers see and log the real visitor IP.

    Chapters:
    0:00 Introduction
    0:12 Two Connections, Two Senders
    0:46 What Our Web Servers See
    1:26 A Note Attached to Every Request
    2:01 Adding option forwardfor
    2:24 The Header Arrives
    2:53 Teaching Apache to Read the Header
    3:41 Turning It On
    4:07 Trust Only Your Proxy
    4:52 Every Server Sees the Real Visitor
    5:24 The Standard Header: option forwarded
    5:51 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    # HAProxy, in defaults
    option forwardfor

    # Apache on every web server
    sudo a2enmod remoteip
    # /etc/apache2/conf-available/remoteip.conf
    RemoteIPHeader X-Forwarded-For
    RemoteIPInternalProxy 10.0.1.7
    sudo a2enconf remoteip
    sudo apache2ctl configtest
    sudo systemctl restart apache2

  • ACLs and Routing to Backends7:12

    In this lecture, HAProxy starts making decisions. With access control lists, we'll send our new PDF site to its own group of servers, and then look at the whole configuration file, frontends and backends together.

    Hands-on: host-based routing with ACLs (pdf.cloudfiles-lab.com gets its own backend), a path redirect, and the complete configuration explained.

    Chapters:
    0:00 Introduction
    0:16 Two Sites, One Proxy
    0:47 HAProxy Doesn't Read the Name Yet
    1:10 What Is an ACL?
    2:02 Editing the Configuration
    2:55 Two Names, Two Backends
    3:26 The Stats Page Shows Both Backends
    3:39 A Second ACL: the Path
    4:22 The Redirect, Live
    4:53 Our Complete Configuration
    5:54 How a Request Finds Its Server
    6:44 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    # /etc/haproxy/haproxy.cfg (frontend and backends)
    frontend cloudfiles
    bind :80
    acl is_pdf hdr(host) -i pdf.cloudfiles-lab.com
    acl wants_pdf path_beg /pdf
    http-request redirect location http://pdf.cloudfiles-lab.com/ if wants_pdf
    use_backend pdf if is_pdf
    default_backend web

    backend web
    balance roundrobin
    default-server inter 2s rise 4 fall 3
    server web1 10.0.1.4:80 check
    server web2 10.0.1.5:80 check

    backend pdf
    server web3 10.0.1.6:80 check
    sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
    sudo systemctl reload haproxy
    # check the redirect: request http://localhost/pdf with curl -I

  • Different Paths, Different Backends5:25

    In the last lecture, we routed by host name. In this one, we route by path: different parts of the same site, like /api and /files, go to different backends.

    Hands-on: /api/ and /files/ routed to different backends with path_beg, combining ACLs with AND/OR/NOT, and map files for many paths.

    Chapters:
    0:00 Introduction
    0:13 One Site, Three Parts
    0:44 Round Robin Doesn't Know the Paths
    1:12 Which Part Is the Path?
    1:50 Editing the Configuration
    2:34 Every Path Finds Its Server
    3:09 Four Backends on the Stats Page
    3:30 How Each Request Finds Its Server
    4:04 Combining ACLs in One Rule
    4:30 Many Paths? Use a Map File
    5:03 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    # frontend cloudfiles: new ACLs and rules
    acl is_api path_beg /api/
    acl is_files path_beg /files/
    use_backend api if is_api
    use_backend files if is_files

    backend api
    server web2 10.0.1.5:80 check

    backend files
    server web1 10.0.1.4:80 check

    # map file alternative (one line per path in /etc/haproxy/paths.map)
    use_backend %[path,map_beg(/etc/haproxy/paths.map,web)]

    sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
    sudo systemctl reload haproxy

  • Sticky Sessions5:32

    Some applications remember things about a visitor, like a shopping cart, on the server that served them. In this lecture, we'll see why load balancing breaks that, and fix it with sticky sessions.

    Hands-on: cookie-based persistence (cookie SERVERID insert indirect nocache), what happens when the sticky server fails, and why shared sessions matter.

    Chapters:
    0:00 Introduction
    0:12 Each Server Has Its Own Memory
    0:44 The Cart Keeps Jumping
    1:10 A Cookie That Names the Server
    1:46 Two Small Changes in backend web
    2:35 Now the Cart Stays Put
    3:25 When the Sticky Server Goes Down
    4:08 Sticky Is Not the Same as Shared
    4:34 Other Ways to Stick
    5:06 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    # /etc/haproxy/haproxy.cfg
    backend web
    balance roundrobin
    cookie SERVERID insert indirect nocache
    default-server inter 2s rise 4 fall 3
    server web1 10.0.1.4:80 check cookie web1
    server web2 10.0.1.5:80 check cookie web2

    sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
    sudo systemctl reload haproxy
    # check the cookie: request http://localhost/cart.php with curl -I

  • HTTPS and SSL Termination7:53

    Our site still uses plain HTTP, so everything travels unencrypted. In this lecture, we'll switch it to HTTPS, with HAProxy doing the encryption: that's called SSL termination.

    Hands-on: certificate, CA bundle and private key combined into one PEM file, bind :443 ssl crt, the HTTP to HTTPS redirect, HTTP/2, and proof that HAProxy terminates TLS.

    Chapters:
    0:00 Introduction
    0:15 Where the Encryption Ends
    1:09 Nobody Answers on Port 443
    1:28 Three Files
    2:15 A Look at the Files
    2:58 Three Files, One PEM File
    3:21 Building the PEM File
    4:09 Telling HAProxy to Speak HTTPS
    5:18 The Padlock Is There
    6:06 Encrypted Outside, Plain Inside
    6:37 Three Ways to Handle HTTPS
    7:16 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    cd ~/cloudfiles-ssl
    openssl x509 -in cloudfiles-lab.com.crt -noout -subject -issuer -enddate
    grep BEGIN *
    # certificate + CA bundle + private key, in this order, into one PEM file
    cat cloudfiles-lab.com.crt ca-bundle.crt cloudfiles-lab.com.key > cloudfiles-lab.com.pem
    grep BEGIN cloudfiles-lab.com.pem
    sudo mkdir -p /etc/haproxy/certs
    sudo mv cloudfiles-lab.com.pem /etc/haproxy/certs/
    sudo chown root:root /etc/haproxy/certs/cloudfiles-lab.com.pem
    sudo chmod 600 /etc/haproxy/certs/cloudfiles-lab.com.pem

    # frontend cloudfiles
    bind :80
    bind :443 ssl crt /etc/haproxy/certs/cloudfiles-lab.com.pem alpn h2,http/1.1
    http-request redirect scheme https unless { ssl_fc }

    sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
    sudo systemctl reload haproxy
    sudo ss -ltnp | grep haproxy

  • Let's Encrypt: Getting and Renewing Certificates6:46

    In the last lecture, we used a ready certificate. In this one, we'll see where it came from: Let's Encrypt, a free certificate authority, and how to renew it automatically, so HTTPS never breaks.

    Hands-on: Let's Encrypt with certbot, the HTTP-01 challenge routed through HAProxy, a renewal test that fails and then passes, and a deploy hook that rebuilds the PEM file. HAProxy 3.4's own ACME client is also introduced (experimental).

    Chapters:
    0:00 Introduction
    0:14 Where Certificates Come From
    0:49 The HTTP-01 Challenge
    1:38 certbot Knows Our Certificate
    2:06 A Renewal Test Fails
    2:41 A Path Rule for the Challenge
    3:31 The Renewal Test Passes
    3:52 HAProxy Reads Our PEM File
    4:23 Writing the Deploy Hook
    4:59 Testing the Whole Chain
    5:36 HAProxy's Own ACME Client
    6:17 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    sudo certbot certificates
    sudo certbot renew --dry-run

    # frontend cloudfiles
    acl is_acme path_beg /.well-known/acme-challenge/
    http-request redirect scheme https unless { ssl_fc } || is_acme
    use_backend acme if is_acme

    backend acme
    server certbot 127.0.0.1:8888

    # deploy hook: /etc/letsencrypt/renewal-hooks/deploy/haproxy.sh
    #!/bin/sh
    cat /etc/letsencrypt/live/cloudfiles-lab.com/fullchain.pem /etc/letsencrypt/live/cloudfiles-lab.com/privkey.pem > /etc/haproxy/certs/cloudfiles-lab.com.pem
    systemctl reload haproxy

    sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/haproxy.sh
    sudo certbot renew --dry-run --run-deploy-hooks
    systemctl list-timers certbot.timer

  • Shared Uploads with an Azure File Share5:00

    CloudFiles is about files, so users upload them. In this lecture, we'll see why uploads break behind a load balancer, and fix it with a shared folder: an Azure file share, mounted on every web server.

    Hands-on: an Azure file share (SMB/CIFS) mounted on every web server through /etc/fstab so uploads are shared. The storage key is kept in a root-only credentials file and never shown.

    Chapters:
    0:00 Introduction
    0:15 Each Server Keeps Its Own Files
    0:48 Uploaded, but Only on web1
    1:12 One Folder, Shared by All
    1:44 The Key Stays Secret
    2:15 Telling Linux About the Share
    3:12 Mounting the Share
    3:40 Upload Once, Seen Everywhere
    4:05 Why This Matters for Load Balancing
    4:33 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    sudo apt install -y cifs-utils

    # /etc/smbcredentials/cloudfileslabscom.cred (root only, chmod 600)
    username=YOUR_STORAGE_ACCOUNT
    password=YOUR_STORAGE_KEY

    # one line in /etc/fstab
    //cloudfileslabscom.file.core.windows.net/cloudfiles-share /var/www/uploads cifs nofail,credentials=/etc/smbcredentials/cloudfileslabscom.cred,uid=www-data,gid=www-data,dir_mode=0775,file_mode=0664,serverino,nosharesock,actimeo=30 0 0

    sudo systemctl daemon-reload
    sudo mount -a
    df -h /var/www/uploads

  • HAProxy and Docker6:46

    Many applications today run in Docker containers. In this lecture, we'll see how HAProxy fits in: in front of several containers on one server, and as one service in a Docker Compose application. This one is a theory lecture.

    Theory lecture: HAProxy in front of containers, the official haproxy:3.4 image, Docker's DNS, and HAProxy inside a Docker Compose application with a frontend, an API, Redis and Postgres. The example files were checked with haproxy -c and docker compose config.

    Chapters:
    0:00 Introduction
    0:17 Containers in One Minute
    0:55 HAProxy in Front of Containers
    1:32 Running HAProxy in a Container
    2:33 HAProxy in a Docker Compose Application
    3:31 The Compose File
    4:20 The HAProxy Configuration
    5:31 Host or Container? And TCP Services
    6:04 Recap

    Commands and configuration from this lecture:

    # haproxy.cfg (example, checked with haproxy -c)
    resolvers docker
    nameserver dns1 127.0.0.11:53

    frontend www
    bind :80
    acl is_api path_beg /api/
    use_backend api if is_api
    default_backend ui

    backend ui
    server ui1 ui:80 check resolvers docker init-addr libc,none

    backend api
    balance roundrobin
    server-template api 1-3 api:8080 check resolvers docker init-addr none

    # compose.yaml (example)
    services:
    haproxy:
    image: haproxy:3.4
    ports:
    - "80:80"
    sysctls:
    - net.ipv4.ip_unprivileged_port_start=0
    volumes:
    - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
    depends_on:
    - ui
    - api

    ui:
    image: nginx:1.29

    api:
    build: ./api
    deploy:
    replicas: 3
    environment:
    REDIS_HOST: redis
    DB_HOST: db

    redis:
    image: redis:8

    db:
    image: postgres:17
    environment:
    POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    secrets:
    - db_password
    volumes:
    - dbdata:/var/lib/postgresql/data

    volumes:
    dbdata:

    secrets:
    db_password:
    file: ./db_password.txt

  • High Availability with keepalived6:01

    Our web servers are protected by HAProxy's health checks. But what protects HAProxy itself? In this lecture, we'll see how two HAProxy servers and keepalived keep the site online when one of them fails. This one is a theory lecture.

    Theory lecture: two HAProxy servers and keepalived (VRRP) sharing a virtual IP, what changes in the cloud (Azure), and peers to keep stick tables in sync. The example configs were checked with keepalived -t and haproxy -c.

    Chapters:
    0:00 Introduction
    0:17 A Single Point of Failure
    0:40 Two Proxies, One Virtual IP
    1:11 keepalived and VRRP
    1:50 keepalived.conf on web-proxy
    2:46 The Backup Is Almost the Same
    3:10 Three Situations
    3:55 The Cloud Is Different
    4:38 Keeping the Two Proxies in Sync
    5:24 Recap

    Commands and configuration from this lecture:

    # /etc/keepalived/keepalived.conf on web-proxy (MASTER); on web-proxy02: state BACKUP, priority 100, addresses swapped
    global_defs {
    enable_script_security
    script_user root
    }

    vrrp_script chk_haproxy {
    script "/usr/bin/systemctl is-active --quiet haproxy"
    interval 2
    fall 2
    rise 2
    }

    vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    unicast_src_ip 10.0.1.7
    unicast_peer {
    10.0.1.8
    }
    authentication {
    auth_type PASS
    auth_pass cf2026ha
    }
    virtual_ipaddress {
    10.0.1.100/24
    }
    track_script {
    chk_haproxy
    }
    }

    # peers (stick table sync)
    peers cloudfiles_peers
    peer web-proxy 10.0.1.7:10000
    peer web-proxy02 10.0.1.8:10000

    backend web
    stick-table type ip size 100k expire 30m peers cloudfiles_peers
    stick on src
    server web1 10.0.1.4:80 check
    server web2 10.0.1.5:80 check

  • HTTP Health Checks4:52

    Until now, HAProxy's health checks only asked one question: is the port open? In this lecture, we'll see why that's not enough, and make HAProxy check a real web page.

    Hands-on: option httpchk with http-check send/expect on a real health page, a failure that plain TCP checks missed, and automatic recovery (L4OK vs L7OK and L7STS on the stats page).

    Chapters:
    0:00 Introduction
    0:13 A Check That Only Knocks on the Door
    0:43 A Health Page
    1:25 Broken, but Still UP
    1:59 Checking the Health Page
    2:48 Now HAProxy Sees the Problem
    3:26 Coming Back Automatically
    3:49 Writing a Good Health Page
    4:23 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    # backend web
    option httpchk
    http-check send meth GET uri /health.php ver HTTP/1.1 hdr Host cloudfiles-lab.com
    http-check expect status 200

    sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
    sudo systemctl reload haproxy
    # test the health page: request http://10.0.1.5/health.php with curl -i

  • Rate Limiting with Stick Tables4:38

    A single visitor, or a single robot, can send hundreds of requests per second, and slow the site down for everyone. In this lecture, HAProxy will count each visitor's requests, and stop the ones that send too many.

    Hands-on: per-IP rate limiting with a stick table, track-sc0 and http-request deny with status 429; the log flags and the table contents explained.

    Chapters:
    0:00 Introduction
    0:15 A Table That Counts Requests
    0:54 No Limit Yet
    1:09 Three Lines in the Frontend
    2:08 The Eleventh Request Is Refused
    2:34 The Log and the Table
    3:22 Choosing Limits and Actions
    4:07 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    # frontend cloudfiles
    stick-table type ip size 100k expire 30s store http_req_rate(10s)
    http-request track-sc0 src
    http-request deny deny_status 429 if { sc_http_req_rate(0) gt 10 }

    sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
    sudo systemctl reload haproxy
    echo 'show table cloudfiles' | sudo socat stdio /run/haproxy/admin.sock

  • The Runtime API in Practice4:16

    Until now, every change meant editing the configuration file and reloading HAProxy. The Runtime API lets us change HAProxy while it runs, instantly, without any reload. In this lecture, we'll use it for real tasks.

    Hands-on: the Runtime API through the stats socket: maintenance mode, drain, live weights, inspecting and clearing stick tables, and why runtime changes are lost on reload.

    Chapters:
    0:00 Introduction
    0:17 Talking to HAProxy
    0:58 Maintenance Mode
    1:29 Drain: a Gentler Way Out
    2:07 Changing a Weight
    2:43 Unblocking a Visitor
    3:13 Runtime Changes Live in Memory
    3:46 Recap

    Lab: cloudfiles-lab.com on Azure with HAProxy 3.4.7 LTS. Every command and result in this video was recorded on the real servers; secrets and visitor IP addresses are kept off screen.

    Commands and configuration from this lecture:

    # every command is sent like this
    echo 'show info' | sudo socat stdio /run/haproxy/admin.sock

    disable server web/web2
    enable server web/web2
    set server web/web2 state drain
    set server web/web2 state ready
    set weight web/web1 3
    get weight web/web1
    set weight web/web1 100%
    show table cloudfiles
    clear table cloudfiles

  • HTTP/3 and QUIC4:25

    In this last lecture, we look at the newest version of HTTP, HTTP/3, and the protocol under it, QUIC. We'll see what problem they solve, and how HAProxy serves them. This one is a theory lecture.

    Theory lecture and course finale: HTTP/1.1 vs HTTP/2 vs HTTP/3, QUIC over UDP, head-of-line blocking, Alt-Svc, and the two HAProxy lines that enable HTTP/3 (checked with haproxy -c).

    Chapters:
    0:00 Introduction
    0:15 Three Versions of HTTP
    0:50 One Lost Packet Stops Everyone
    1:25 What QUIC Brings
    2:07 The Alt-Svc Header
    2:52 Two Lines in the Frontend
    3:55 Recap
    4:20 End of the course

    Commands and configuration from this lecture:

    # frontend cloudfiles (example, checked with haproxy -c)
    bind :443 ssl crt /etc/haproxy/certs/cloudfiles-lab.com.pem alpn h2,http/1.1
    bind quic4@:443 ssl crt /etc/haproxy/certs/cloudfiles-lab.com.pem alpn h3
    http-response set-header alt-svc "h3=\":443\"; ma=86400"
    # also needed: UDP port 443 open in the firewall

Requirements

  • For practical Labs students are preferred to have basic knowledge in Linux in addition to VMware or VirtualBox

Description

HAProxy is the most powerful solution used for Load Balancing and High Availability, it is open source, stable and reliable , it can be used to receive  requests  from your clients and to distribute theses requests among your servers.

This course is a crash course, you will be able to move from beginner to advanced HAProxy admin within 80 minutes. In this course you will learn the following topics:

Load Balancing Concepts and Preface

What is HAProxy

Why to Use HAProxy

HAProxy Tasks

Frontend and Backend concepts

Scheduling Algorithms

Configurations

Practical Example

Displaying Statistics and Monitoring

TCP and HTTP modes

ForwardFor Option

Access Lists

Sticky Sessions

SSL configurations

Avoiding Single Point Of Failure

Conclusion


Eventually, this will be a continuous course I am going to add any additional topics I think they are important, and any topics requested by students.

So, if you think that this course can be improved by adding any topics, then please let me know.

Who this course is for:

  • Students interested in High Availability and Load Balancing