HAProxy is a high availability and load balancing tool. In the BRIX system architecture, HAProxy is utilized to construct a fault-tolerant design when organizing a high-availability BRIX cluster. Depending on the architecture being set up, the necessary HAProxy configuration is chosen. This article outlines the installation of HAProxy and keepalived for Ubuntu Linux 20.04 and 22.04.
Installation consists of six steps:
- Prepare nodes (servers).
- Install HAProxy.
- Install keepalived.
- Configure keepalived.
- Basic HAProxy configuration.
- Add configurations for BRIX components balancing.
Step 1: Prepare nodes (servers)
- Create two nodes (servers) with sequentially numbered host name:
- haproxy-server1.your_domain, 192.168.1.1;
- haproxy-server2.your_domain, 192.168.1.2.
- Create mappings for host names in the DNS server and add the corresponding A-record for the virtual IP.
haproxy-server.your_domain <-> 192.168.1.13.
If not possible, add the necessary records in /etc/hosts
.
- Enable
net.ipv4.ip_nonlocal_bind, net.ipv4.ip_forward
kernel parameters by executing the following commands:
sudo echo net.ipv4.ip_nonlocal_bind=1 >> /etc/sysctl.conf
sudo echo net.ipv4.ip_forward=1 >> /etc/sysctl.conf
sudo sysctl -p
Step 2: Install HAProxy
- Install HAProxy on all nodes with the command:
sudo apt install haproxy -y
- Start the HAProxy service on all nodes and add it to autostart:
sudo systemctl enable --now haproxy
Step 3: Install keepalived
Install keepalived on all nodes:
sudo apt update
sudo apt install keepalived
Step 4: Configure keepalived
- Create the keepalived directory:
sudo mkdir -p /usr/libexec/keepalived
- Create the
haproxy_check.sh
script file for monitoring the HAProxy health:
sudo nano /usr/libexec/keepalived/haproxy_check.sh
- Add the health check script to the
haproxy_check.sh
file:
#!/bin/bash
/bin/kill -0 $(cat /var/run/haproxy.pid)
- Create the keepalived configuration file on each node:
sudo nano /etc/keepalived/keepalived.conf
- Add an example keepalived configuration for the haproxy-server1.your_domain node to the keepalived.conf file
keepalived.conf
:
global_defs {
router_id haproxy-server1
enable_script_security
script_user root
}
vrrp_script haproxy_check {
script "/usr/libexec/keepalived/haproxy_check.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
interface eth0
virtual_router_id 101
priority 100
advert_int 2
state MASTER
virtual_ipaddress {
192.168.1.13
}
track_script {
haproxy_check
}
authentication {
auth_type PASS
auth_pass SecretPassword
}
}
- Add an example keepalived configuration for the haproxy-server2.your_domain node to the keepalived.conf file
keepalived.conf
:
global_defs {
router_id haproxy-server2
enable_script_security
script_user root
}
vrrp_script haproxy_check {
script "/usr/libexec/keepalived/haproxy_check.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
interface eth0
virtual_router_id 101
priority 90
advert_int 2
state BACKUP
virtual_ipaddress {
192.168.1.13
}
track_script {
haproxy_check
}
authentication {
auth_type PASS
auth_pass SecretPassword
}
}
Where:
router_id
is the router ID, a unique value on each node;script_user
is the user running VRRP scripts;interface
is the interface name to which keepalived will be attached;virtual_router_id
is the virtual router ID, a shared value on all nodes;priority
is the priority of nodes within the virtual route;state
is the node role type in the virtual router;virtual_ipaddress
is the virtual IP;auth_type
is the authentication type in the virtual router;auth_pass
is the password.
- Restart keepalived on all nodes:
sudo systemctl restart keepalived
Step 5: Basic HAProxy configuration
- Move the default configuration file:
sudo mv /etc/haproxy/haproxy.cfg{,.original}
- Create and open a new configuration file for editing:
sudo nano /etc/haproxy/haproxy.cfg
Example of basic HAProxy configuration for the haproxy.cfg file
Basic configuration section describes the parameters of the HAProxy server operation: operating mode, timeouts, connection count, enabling the web interface, etc. The example provides the necessary configuration for the HAProxy server operation. For more details on the used parameters, refer to the official HAProxy Documentation. global |
- Restart HAProxy:
sudo systemctl restart haproxy
Step 6: Add configurations for BRIX components balancing
BRIX component configurations are added to the haproxy.cfg
file as needed.
Depending on the architecture being set up, add configurations:
- HAProxy for PostgreSQL.
- HAProxy for RabbitMQ.
- HAProxy for Redis Sentinel.
- HAProxy for S3 Minio.
- HAProxy for BRIX web.
- HAProxy for document server.