Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 39HAProxy · Web

Install and set up HAProxy (load balancer)

HAProxy is a very fast, very stable load balancer for HTTP and TCP. It spreads traffic over many app servers, removes sick servers with health checks, ends TLS, and balances things like databases and the Kubernetes API. This guide installs HAProxy on RHEL and Ubuntu, writes a full config (TLS, redirects, health checks, a stats page), adds a TCP balancer, sets up logging and SELinux, and shows how to take a server out of rotation without downtime.

  • 10 Lessen
  • 16 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

39.1

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.

ItemValue
Packagehaproxy
Servicehaproxy.service
Config/etc/haproxy/haproxy.cfg
Certificatesone PEM per site (certificate + chain + key), for example /etc/haproxy/certs/
Runtime APIadmin socket, for example /run/haproxy/admin.sock
Logsthrough syslog (/dev/log) → journald / rsyslog
Check confighaproxy -c -f /etc/haproxy/haproxy.cfg

39.2

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 --reload

haproxy_connect_any lets HAProxy connect to backends on any port (SELinux allows only some ports by default).

39.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y haproxy
ufw allow 80,443/tcp

Op 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).

39.4

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.pem
/etc/haproxy/haproxy.cfg
global
    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
SettingWhy
bind :443 ssl crt /etc/haproxy/certs/TLS with every PEM in the folder (chosen by SNI).
redirect scheme httpsHTTP → HTTPS.
option forwardforApps see the real client IP in X-Forwarded-For.
option httpchk GET /health + expect status 200Only healthy servers get traffic.
inter 3s fall 3 rise 2Checked every 3 s; out after 3 failures, back after 2 successes.
backupweb3 gets traffic only when the others are down.
TimeoutsSlow or dead connections are closed; protects against slowloris.
stats on 127.0.0.1 with a passwordA live status page, not public.

Check and start:

haproxy -c -f /etc/haproxy/haproxy.cfg
Configuration file is valid
systemctl enable --now haproxy
systemctl reload haproxy
curl -I https://shop.example.com

39.5

TCP balancing (databases, Kubernetes API)

/etc/haproxy/haproxy.cfg
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 check

This is the classic front of a highly available kubeadm control plane (often two HAProxy servers with keepalived sharing one virtual IP).

39.6

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 lb1

drain = 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.

39.7

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 } (with http-request track-sc0 src).
  • Real health URLs that check the app, not only the port.
  • Keep SELinux on (haproxy_connect_any instead of turning it off); update HAProxy.

39.8

Update and remove

dnf -y upgrade haproxy
apt-get install --only-upgrade -y haproxy
haproxy -c -f /etc/haproxy/haproxy.cfg && systemctl restart haproxy
dnf -y remove haproxy

After a certificate renewal, rebuild the PEM file and reload (a Certbot deploy hook can do both).

39.9

Fix common problems

ProblemCauseFix
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 RHELSELinux blocks the port.setsebool -P haproxy_connect_any 1.
unable to load SSL private keyWrong order or missing key in the PEM.Certificate + chain first, then the key, all in one file.
No logsSyslog not receiving /dev/log local0.Check journalctl -u haproxy; for files add an rsyslog rule for local0.
Apps see the LB IP, not the clientNo X-Forwarded-For handling.option forwardfor; configure the app/web server to trust the LB.
Users are logged out between serversSessions stored on each app server.Shared sessions (Redis), or sticky sessions (cookie SRV insert).

39.10

Next steps

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.

Alle commando's in dit hoofdstuk