HAProxy, which stands for High Availability Proxy, is a popular open-source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spread requests across multiple servers. It is widely used for its reliability, high performance, and low memory footprint. HAProxy can handle loads from tens to hundreds of thousands of concurrent connections and is often used to improve the performance and reliability of a server environment by distributing the workload across multiple servers.
To install HAProxy on your system, use the following command:
`dnf install -y haproxy``
Adjust the SELinux policies to allow HAProxy to function correctly:
setsebool -P httpd_can_network_connect_db 1
setsebool -P httpd_can_network_connect 1
setsebool -P haproxy_connect_any 1
setsebool -P nis_enabled 1
Create a directory for additional configuration files and update the HAProxy service's options to include this directory:
mkdir /etc/haproxy/haproxy.d
sed -i 's/OPTIONS=""/OPTIONS="-f \/etc\/haproxy\/haproxy.d"/' /etc/sysconfig/haproxy
This is the base configuration for HAProxy, which sets up global settings and default options for processing HTTP and TCP requests:
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 8192
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
########################################################################################
## Dont edit This File, any frontends and backends belong into /etc/haproxy/haproxy.d ##
########################################################################################
Configuration for a Patroni cluster, demonstrating how to set up HAProxy to manage database connections:
## Patroni Cluster 0
###########################################################################
frontend patroni0_frontend
bind *:5432
mode tcp
maxconn 100
timeout client 3600s
default_backend patroni0_backend
backend patroni0_backend
mode tcp
option httpchk
timeout check 5s
timeout connect 2s
timeout server 3600s
http-check expect status 200
default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
server node01 10.0.0.111:6432 maxconn 100 check port 8008
server node02 10.0.0.112:6432 maxconn 100 check port 8008
Setup for a Galera cluster to balance MySQL database connections:
## Galera Cluster
###########################################################################
frontend galera_frontend
bind *:3306
mode tcp
default_backend galera_backend
backend galera_backend
mode tcp
balance roundrobin
server node01 10.0.0.111:3306 check
server node02 10.0.0.112:3306 check