﻿# High availability HAProxy

> [HTML Version](fail-safe-haproxy.html)

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:

1. [Prepare nodes (servers)](#prepare-nodes).

2. [Install HAProxy](#install-haproxy).

3. [Install keepalived](#install-keepalived).

4. [Configure keepalived](#set-up-keepalived).

5. [Basic HAProxy configuration](#set-up-haproxy-configuration).

6. [Add configurations for BRIX components balancing](#add-configurations).

## Step 1: Prepare nodes (servers)

1. 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**.

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 \[OBJECT\].

3. Enable \[OBJECT\] 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

1. Install HAProxy on all nodes with the command:

````
sudo apt install haproxy -y

2. ````
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

1. Create the keepalived directory:

````
sudo mkdir -p /usr/libexec/keepalived

2. ````
Create the \[OBJECT\] script file for monitoring the HAProxy health:

````
sudo nano /usr/libexec/keepalived/haproxy\_check.sh

3. ````
Add the health check script to the \[OBJECT\] file:

````
\#\!/bin/bash  
/bin/kill -0 \$(cat /var/run/haproxy.pid)

4. ````
Create the keepalived configuration file on each node:

````
sudo nano /etc/keepalived/keepalived.conf

5. ````
Add an example keepalived configuration for the **haproxy-server1.your\_domain** node to the keepalived.conf file \[OBJECT\]:

````
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  
  \}  
\}

6. ````
Add an example keepalived configuration for the **haproxy-server2.your\_domain** node to the keepalived.conf file \[OBJECT\]:

````
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:

- \[OBJECT\] is the router ID, a unique value on each node;

- \[OBJECT\] is the user running VRRP scripts;

- \[OBJECT\] is the interface name to which keepalived will be attached;

- \[OBJECT\] is the virtual router ID, a shared value on all nodes;

- \[OBJECT\] is the priority of nodes within the virtual route;

- \[OBJECT\] is the node role type in the virtual router;

- \[OBJECT\] is the virtual IP;

- \[OBJECT\] is the authentication type in the virtual router;

- \[OBJECT\] is the password.

7. Restart keepalived on all nodes:

````
sudo systemctl restart keepalived

## ````
Step 5: Basic HAProxy configuration

1. Move the default configuration file:

````
sudo mv /etc/haproxy/haproxy.cfg\{,.original\}

2. ````
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](http://docs.haproxy.org/).

````
global  
    maxconn 100000  
    log /dev/log local0  
    log /dev/log local1 notice  
    chroot /var/lib/haproxy  
    stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners  
    stats timeout 30s  
    user haproxy  
    group haproxy  
    daemon  
  
  
  
defaults  
    mode tcp  
    log global  
    retries 2  
    timeout queue 5s  
    timeout connect 5s  
    timeout client 60m  
    timeout server 60m  
    timeout check 15s  
  
  
  
listen stats  
    mode http  
    bind haproxy-server.your\_domain:7000  
    stats enable  
    stats uri /  
  
  
  
\### Web BRIX365 ###  
  
\### Web BRIX365 ###  
  
  
  
\### PostgreSQL ###  
  
\### PostgreSQL ###  
  
  
  
\### RabbitMQ ###  
  
\### RabbitMQ ###  
  
  
  
\### S3 MinIO ###  
  
\### S3 MinIO ###  
  
  
  
\### Docs Server ###  
  
\### Docs Server ###
````

3. ````
Restart HAProxy:

````
sudo systemctl restart haproxy

## ````
Step 6: Add configurations for BRIX components balancing

BRIX component configurations are added to the \[OBJECT\] file as needed. 

Depending on the architecture being set up, add configurations:

- [HAProxy for PostgreSQL](haproxy-postgresql.md).

- [HAProxy for RabbitMQ](haproxy-rabbitmq.md).

- [HAProxy for Redis Sentinel](haproxy-redis.md).

- [HAProxy for S3 Minio](haproxy-s3-minio.md).

- [HAProxy for BRIX web](haproxy-web-elma365.md).

- [HAProxy for document server](haproxy-docs-server.md).