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

Configure HAProxy  for PostgreSQL

The architecture of the highly available ELMA365 cluster involves the interaction of ELMA365 application microservices with a PostgreSQL database cluster. The HAProxy configuration discussed in this article is designed for load balancing traffic to the Master-Replica(s) cluster.

Example HAProxy сonfigurations:

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

Example 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 queries the Patroni service port 8008 to automatically determine the active PostgreSQL server with the master role;
  • 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:5001are directed to servers with the slave role.

Open the haproxy.cfg configuration file for editing:

sudo nano /etc/haproxy/haproxy.cfg

Example configuration to set up load balancing for a PostgreSQL cluster using HAProxy to add to the file haproxy.cfg:

### PostgreSQL ###
listen postgres_master
    bind haproxy-server.your_domain:5000
    option tcplog
    option httpchk OPTIONS /master
    http-check expect status 200
    default-server inter 3s fastinter 1s fall 3 rise 4 on-marked-down shutdown-sessions
    server postgres-server1 postgres-server1.your_domain:5432 check port 8008
    server postgres-server2 postgres-server2.your_domain:5432 check port 8008
    server postgres-server3 postgres-server3.your_domain:5432 check port 8008
 
listen postgres_replicas
    bind haproxy-server.your_domain:5001
    option tcplog
    option httpchk OPTIONS /replica
    balance roundrobin
    http-check expect status 200
    default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
    server postgres-server1 postgres-server1.your_domain:5432 check port 8008
    server postgres-server2 postgres-server2.your_domain:5432 check port 8008
    server postgres-server3 postgres-server3.your_domain:5432 check port 8008
### PostgreSQL ###

Example HAProxy configuration using SSL

Restart HAProxy with the command:

sudo systemctl restart haproxy

Example 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 installed as described in Install PGBouncer.

  • HAProxy queries the Patroni service port 8008 to automatically determine the active PostgreSQL server with the master role;
  • 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.

Open the configuration file haproxy.cfg for editing:

sudo nano /etc/haproxy/haproxy.cfg

Example configuration to set up load balancing for PGBouncer with HAProxy to add to the file haproxy.cfg:

### PostgreSQL ###
listen postgres_master
    bind haproxy-server.your_domain:5000
    option tcplog
    option httpchk OPTIONS /master
    http-check expect status 200
    default-server inter 3s fastinter 1s fall 3 rise 4 on-marked-down shutdown-sessions
    server postgres-server1 postgres-server1.your_domain:6432 check port 8008
    server postgres-server2 postgres-server2.your_domain:6432 check port 8008
    server postgres-server3 postgres-server3.your_domain:6432 check port 8008
 
listen postgres_replicas
    bind haproxy-server.your_domain:5001
    option tcplog
    option httpchk OPTIONS /replica
    balance roundrobin
    http-check expect status 200
    default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
    server postgres-server1 postgres-server1.your_domain:6432 check port 8008
    server postgres-server2 postgres-server2.your_domain:6432 check port 8008
    server postgres-server3 postgres-server3.your_domain:6432 check port 8008
### PostgreSQL ###

Example HAProxy configuration using SSL

Restart HAProxy with the command:

sudo systemctl restart haproxy

Found a typo? Highlight the text, press ctrl + enter and notify us