BRIX On-Premises > Prepare infrastructure > Load balancer / Configure HAProxy for PostgreSQL

Configure HAProxy for PostgreSQL

The architecture of the highly available BRIX cluster involves the interaction of BRIX application microservices with a PostgreSQL database cluster. To ensure secure load balancing in the system, create a HAProxy configuration for PostgreSQL. This way, in case of a failure, all traffic will be redirected to the Master-Replica(s) cluster, which guarantees continuous operation of the system.

Examples of a HAProxy сonfiguration:

  1. Example HAProxy configuration for connecting to PostgreSQL.
  2. Example HAProxy configuration for connecting to PGBouncer.

Example of a HAProxy configuration for connecting to PostgreSQL

This configuration is prepared for load balancing traffic in a PostgreSQL cluster deployed according to the description in PostgreSQL cluster. HAProxy is used for balancing. It automatically checks port 8008 of Patroni service on PostgreSQL servers with the master role.

The operation traffic to the cluster is distributed as follows:

  • Write operations coming to haproxy-server.your_domain:5000 are directed to the server with the master role.
  • Read operations coming to haproxy-server.your_domain:5001 are directed to servers with the slave role.

In case of a failure, all traffic will be redirected to the Master-Replica(s) cluster, i.e. write and read operations will start to arrive here.

In order to create a HAProxy configuration for PostgreSQL, follow these steps:

  1. Open the haproxy.cfg configuration file for editing using the following command:

sudo nano /etc/haproxy/haproxy.cfg

  1. Edit the haproxy.cfg configuration file:

Configuration example:

Example of a HAProxy configuration using SSL

  1. Restart HAProxy to apply changes:

sudo systemctl restart haproxy

Example of a HAProxy configuration for connecting to PGBouncer

This configuration is prepared for load balancing traffic in a PostgreSQL cluster, deployed according to the description in the PostgreSQL cluster article, through the PGBouncer program. HAProxy is used for balancing. It automatically checks port 8008 of Patroni service on PostgreSQL servers with the master role.

The operation traffic to the cluster is distributed as follows:

  • Write operations coming to haproxy-server.your_domain:5000 are directed to the server with the master role.
  • Read operations coming to haproxy-server.your_domain:5001 are directed to servers with the slave role.

In case of a failure, all traffic will be redirected to the Master-Replica(s) cluster, i.e. write and read operations will start to arrive here.

In order to create a HAProxy configuration through the PGBouncer program, perform the following actions:

  1. Open the configuration file haproxy.cfg for editing using the following command:

sudo nano /etc/haproxy/haproxy.cfg

  1. Edit the haproxy.cfg configuration file:

Configuration example

Example of a HAProxy configuration using SSL

  1. Restart HAProxy to apply changes:

sudo systemctl restart haproxy