۱. نصب
sudo dnf install -y haproxy
haproxy -v۲. گواهی
HAProxy گواهی و کلید را در یک فایل PEM میخواهد:
sudo mkdir -p /etc/haproxy/certs
sudo sh -c 'cat /etc/pki/tls/certs/portal.crt /etc/pki/tls/private/portal.key > /etc/haproxy/certs/portal.pem'
sudo chmod 600 /etc/haproxy/certs/portal.pem۳. پیکربندی
/etc/haproxy/haproxy.cfg را با این محتوا جایگزین کنید (اول از نسخهی اصلی کپی بگیرید):
global
log /dev/log local0
maxconn 20000
user haproxy
group haproxy
daemon
ssl-default-bind-options ssl-min-ver TLSv1.2
defaults
mode http
log global
option httplog
option forwardfor
timeout connect 5s
timeout client 60s
timeout server 60s
frontend web
bind :80
bind :443 ssl crt /etc/haproxy/certs/portal.pem alpn h2,http/1.1
http-request redirect scheme https code 301 unless { ssl_fc }
http-request set-header X-Forwarded-Proto https if { ssl_fc }
default_backend app
backend app
balance roundrobin
option httpchk GET /health
http-check expect status 200
server web01 192.168.10.21:8080 check inter 3s fall 3 rise 2
server web02 192.168.10.22:8080 check inter 3s fall 3 rise 2
listen stats
bind 127.0.0.1:8404
stats enable
stats uri /
stats refresh 10sصفحهی آمار فقط روی localhost است. برای دیدنش از تونل SSH استفاده کنید: ssh -L 8404:127.0.0.1:8404 lb01.
۴. SELinux
سیاست SELinux فقط اتصال HAProxy به چند نوع پورت شناختهشده را مجاز میداند. اگر بعد از راهاندازی همهی سرورها DOWN شدند و در لاگ audit خطای denied { name_connect } دیدید، این boolean را روشن کنید:
sudo ausearch -m avc -c haproxy -ts recent
sudo setsebool -P haproxy_connect_any 1۵. اجرا
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
sudo systemctl enable --now haproxy
sudo firewall-cmd --permanent --add-service={http,https} && sudo firewall-cmd --reload۶. آزمایش و نگهداری
for i in 1 2 3 4; do curl -sk https://portal.corp.sahor.ir/ | head -1; done
echo "show servers state" | sudo socat stdio /var/lib/haproxy/stats 2>/dev/null || trueبرای بارگذاری دوبارهی تنظیمات بدون قطع اتصالها، همیشه اول تنظیمات را بررسی کنید:
sudo haproxy -c -f /etc/haproxy/haproxy.cfg && sudo systemctl reload haproxyبرای خارج کردن موقت یک سرور در زمان بهروزرسانی، در خط مربوط کلمهی disabled را اضافه کنید و reload کنید. اتصالهای فعلی تمام میشوند و اتصال جدیدی به آن سرور نمیرود.