PostgreSQL client libraries like libpq and jdbc support client-connection failover. The connection string contains multiple servers, for example, host=srv1,srv2. The client library loops over the available hosts to find a connection that's available and capable of read-write or read-only operations. This configuration allows clients to follow the primary cluster during a switchover.
VIP & Keepalived
When using multiple HAProxy instances and you need a way to decide which proxy host to connect to, the easiest solution is to use Keepalived and a VIP (virtual IP).
This example shows installing and configuring on RHEL8.
First, install Keepalived on each hosts running HAProxy:
For this example, use 192.168.121.121 as the virtual IP. Configure Keepalived:
Configure the firewall if needed:
Finally, enable and start the Keepalived service:
Once started, the virtual IP is attached to one of the hosts. To verify, try to reach it: