Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Know it before you install
A frontend receives the traffic (address, port, TLS). A backend is a group of servers with a balancing method and health checks. defaults holds the settings shared by all. mode http understands HTTP (headers, paths, redirects); mode tcp just forwards bytes.
| Item | Value |
|---|---|
| Package | haproxy |
| Service | haproxy.service |
| Config | /etc/haproxy/haproxy.cfg |
| Certificates | one PEM per site (certificate + chain + key), for example /etc/haproxy/certs/ |
| Runtime API | admin socket, for example /run/haproxy/admin.sock |
| Logs | through syslog (/dev/log) → journald / rsyslog |
| Check config | haproxy -c -f /etc/haproxy/haproxy.cfg |
Install on RHEL, Rocky, Alma and Fedora
dnf -y install haproxy
haproxy -v
setsebool -P haproxy_connect_any 1
firewall-cmd --permanent --add-service={http,https}
firewall-cmd --reloadhaproxy_connect_any lets HAProxy connect to backends on any port (SELinux allows only some ports by default).
Install on Ubuntu and Debian
apt-get update
apt-get install -y haproxy
ufw allow 80,443/tcpOp het werk
For the newest LTS branch on Ubuntu/Debian, the HAProxy team's packages (haproxy.debian.net) give a chosen version line (for example 3.2 LTS).
First configuration: an HTTPS load balancer
Make the PEM file for the site (certificate, chain, then the private key):
mkdir -p /etc/haproxy/certs
cat /etc/letsencrypt/live/shop.example.com/fullchain.pem /etc/letsencrypt/live/shop.example.com/privkey.pem > /etc/haproxy/certs/shop.example.com.pem
chmod 600 /etc/haproxy/certs/shop.example.com.pemglobal
log /dev/log local0
maxconn 20000
user haproxy
group haproxy
daemon
stats socket /run/haproxy/admin.sock mode 660 level admin
ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
defaults
mode http
log global
option httplog
option dontlognull
option forwardfor
timeout connect 5s
timeout client 30s
timeout server 30s
timeout http-request 10s
frontend fe_https
bind :80
bind :443 ssl crt /etc/haproxy/certs/ alpn h2,http/1.1
http-request redirect scheme https code 301 unless { ssl_fc }
http-response set-header Strict-Transport-Security "max-age=31536000"
http-request deny if { path_beg /.git /.env }
default_backend be_shop
backend be_shop
balance roundrobin
option httpchk GET /health
http-check expect status 200
default-server inter 3s fall 3 rise 2
server web1 10.0.1.11:8080 check
server web2 10.0.1.12:8080 check
server web3 10.0.1.13:8080 check backup
listen stats
bind 127.0.0.1:8404
stats enable
stats uri /
stats refresh 10s
stats auth admin:change-me-long| Setting | Why |
|---|---|
bind :443 ssl crt /etc/haproxy/certs/ | TLS with every PEM in the folder (chosen by SNI). |
redirect scheme https | HTTP → HTTPS. |
option forwardfor | Apps see the real client IP in X-Forwarded-For. |
option httpchk GET /health + expect status 200 | Only healthy servers get traffic. |
inter 3s fall 3 rise 2 | Checked every 3 s; out after 3 failures, back after 2 successes. |
backup | web3 gets traffic only when the others are down. |
| Timeouts | Slow or dead connections are closed; protects against slowloris. |
stats on 127.0.0.1 with a password | A live status page, not public. |
Check and start:
TCP balancing (databases, Kubernetes API)
frontend fe_k8s_api
mode tcp
bind :6443
option tcplog
default_backend be_k8s_api
backend be_k8s_api
mode tcp
balance roundrobin
option tcp-check
server cp1 10.0.3.11:6443 check
server cp2 10.0.3.12:6443 check
server cp3 10.0.3.13:6443 checkThis is the classic front of a highly available kubeadm control plane (often two HAProxy servers with keepalived sharing one virtual IP).
Daily use: runtime API and the real uses
haproxy -c -f /etc/haproxy/haproxy.cfg && systemctl reload haproxy
echo "show stat" | socat stdio /run/haproxy/admin.sock | cut -d, -f1,2,18 | column -s, -t
echo "show servers state" | socat stdio /run/haproxy/admin.sock
echo "set server be_shop/web1 state drain" | socat stdio /run/haproxy/admin.sock
echo "set server be_shop/web1 state maint" | socat stdio /run/haproxy/admin.sock
echo "set server be_shop/web1 state ready" | socat stdio /run/haproxy/admin.sock
journalctl -u haproxy -f
ssh -L 8404:127.0.0.1:8404 lb1drain = no new users, current ones finish; maint = out of rotation for maintenance; ready = back. That is how you update app servers one by one with zero downtime.
Real uses at work:
- Load balancing web apps and APIs with health checks and TLS.
- The HA front of Kubernetes control planes, databases (read replicas), mail and LDAP.
- Rate limits and blocks with stick tables during attacks.
Make it safe
Security checklist:
- TLS 1.2+ only; HSTS; PEM files mode
600. - Stats page and admin socket only on localhost or with a password; never public.
- Timeouts set (
http-request,client,server) against slow attacks. - Rate limit with stick tables, for example:
stick-table type ip size 100k expire 10m store http_req_rate(10s)+http-request deny if { sc_http_req_rate(0) gt 100 }(withhttp-request track-sc0 src). - Real health URLs that check the app, not only the port.
- Keep SELinux on (
haproxy_connect_anyinstead of turning it off); update HAProxy.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
| 503 Service Unavailable "No server is available" | All backend servers fail the health check. | Stats page / show stat; curl http://10.0.1.11:8080/health from the LB; fix the check URL. |
cannot bind socket [0.0.0.0:443] | Another program uses the port, or no rights. | ss -ltnp 'sport = :443'; stop Nginx/Apache there. |
Backend DOWN with Layer4 connection problem on RHEL | SELinux blocks the port. | setsebool -P haproxy_connect_any 1. |
unable to load SSL private key | Wrong order or missing key in the PEM. | Certificate + chain first, then the key, all in one file. |
| No logs | Syslog not receiving /dev/log local0. | Check journalctl -u haproxy; for files add an rsyslog rule for local0. |
| Apps see the LB IP, not the client | No X-Forwarded-For handling. | option forwardfor; configure the app/web server to trust the LB. |
| Users are logged out between servers | Sessions stored on each app server. | Shared sessions (Redis), or sticky sessions (cookie SRV insert). |
Next steps
- Quick how-tos: Balance traffic, Highly available IP with keepalived.
- Command page: haproxy, Command page: keepalived.
- At work: The fast load balancer, HAProxy in DevOps.
- The other proxies: Nginx, Traefik. Certificates: Certbot.
- Shared sessions: Install and set up Redis.
1What is the difference between drain and maint for a server?
drain lets current users finish but sends no new ones; maint takes the server out at once.
2All servers are DOWN and users see 503, but the apps run. What do you check?
The health check: the URL and expected status, and on RHEL the SELinux boolean haproxy_connect_any.