Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can haproxy manage load balancing in case of primary goes down. #680

Closed
Ashishveer0710 opened this issue Jun 24, 2024 · 3 comments
Closed
Labels
question Further information is requested

Comments

@Ashishveer0710
Copy link

How can we ensure in case of primary goes down and vip still points to the same server that my write operation always goes to primary server without failure.

If it is not possible can we have pgpool in this setup?

@vitabaks
Copy link
Owner

HAProxy checks the role of each server through the Patroni REST API, so it does not matter which balancing the connection is made through, since each haproxy will know about the current cluster topology.

image

@vitabaks vitabaks added the question Further information is requested label Jun 24, 2024
@Ashishveer0710
Copy link
Author

to ensure connection pooling and load balancing without write queries going to read replicas , how should I proceed ?

For connection pooling , I use below query :

psql -p 15432 (pgbouncer port)

and for haproxy :

psql -p 25432 (haproxy port) :: question : is this connection using connection pooling as well as explained in the above image and is it going to only master ?

I am getting " update cannot be done in read-only transaction" error .

@vitabaks
Copy link
Owner

is this connection using connection pooling as well as explained in the above image and is it going to only master ?

yes, if pgbouncer_install is true then the connection will be via pgbouncer. See the haproxy configuration there should be a pgbouncer port.

I am getting " update cannot be done in read-only transaction" error .

Perhaps your application maintains a permanent connection to the database and HAProxy does not break these connections when changing the Postgres role.
In this scenario, I use a script in patroni_callbacks that will restart haproxy if the Postgres role has been changed. In this case, permanent connections will be forcibly terminated and the application will be able to make a new connection to the new Primary.

Example:

patroni.yml

  callbacks:
      on_role_change: '/etc/patroni/haproxy_restart.sh'

haproxy_restart.sh

#!/bin/bash
# /etc/patroni/haproxy_restart.sh
# restart haproxy on all servers in the cluster
for host in $(patronictl -c /etc/patroni/patroni.yml list -f json | jq -r '.[] | .Host'); do
  ssh \
  -o StrictHostKeyChecking=no \
  -o UserKnownHostsFile=/dev/null \
  postgres@"${host}" \
  sudo systemctl restart haproxy
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants