如何配置HaProxy以平衡对PostgreSQL集群所有节点的读取请求?

如何配置HaProxy以平衡对PostgreSQL集群所有节点的读取请求?,postgresql,configuration,load-balancing,haproxy,Postgresql,Configuration,Load Balancing,Haproxy,我在Patenti上有一个PostgreSQL集群(Haproxy+Keepalived+etcd)——一个主节点和两个备用节点 目前,Haproxy的配置方式如下: 连接到主节点的端口5000 连接到备用节点的端口5001 如何配置Haproxy,以便使用端口5001连接到备用节点和主节点 下面是我的haproxy.cfg: global maxconn 1000 nbproc 2 defaults log global mode tcp retries 2 timeout client

我在Patenti上有一个PostgreSQL集群(Haproxy+Keepalived+etcd)——一个主节点和两个备用节点

目前,Haproxy的配置方式如下:

  • 连接到主节点的端口
    5000
  • 连接到备用节点的端口
    5001
如何配置Haproxy,以便使用端口
5001
连接到备用节点和主节点

下面是我的
haproxy.cfg

global
maxconn 1000
nbproc 2

defaults
log global
mode tcp
retries 2
timeout client 30m
timeout connect 4s
timeout server 30m
timeout check 5s

listen stats
  mode http
  bind *:7000
  stats enable
  stats uri /

frontend ft_postgresql
bind *:5000
default_backend postgres-patroni

frontend ft_postgresql_replica
bind *:5001
default_backend postgres-patroni-replica

backend postgres-patroni
  option httpchk OPTIONS /master

  http-check expect status 200
  default-server inter 3s fall 3 rise 2

  server node_one ip.to.node.one:5432 maxconn 1000 check port 8008 on-marked-down shutdown-sessions
  server node_two ip.to.node.two:5432 maxconn 1000 check port 8008 on-marked-down shutdown-sessions
  server node_three ip.to.node.three:5432 maxconn 1000 check port 8008 on-marked-down shutdown-sessions

backend postgres-patroni-replica
  option httpchk OPTIONS /replica

  http-check expect status 200
  default-server inter 3s fall 3 rise 2

  server node_one ip.to.node.one:5432 maxconn 1000 check port 8008 on-marked-down shutdown-sessions
  server node_two ip.to.node.two:5432 maxconn 1000 check port 8008 on-marked-down shutdown-sessions
  server node_three ip.two.node.three:5432 maxconn 1000 check port 8008 on-marked-down shutdown-sessions

在一份Patenti文档中,我发现了/health端点:

仅当PostgreSQL启动并运行时返回HTTP状态代码200

我尝试在haproxy配置中使用该端点,它的工作原理与预期的一样,当所有节点都处于活动状态时,我会给出所有3个节点,而不会给出未处于运行状态的节点

因此,如果要将所有节点添加到haproxy balance,请在haproxy.conf中创建一个新的后端

backend postgres-patroni-all
  option httpchk OPTIONS /health

  http-check expect status 200
  default-server inter 3s fall 3 rise 2

  server node_one ip.to.node.one:5432 maxconn 1000 check port 8008 on-marked-down shutdown-sessions
  server node_two ip.to.node.two:5432 maxconn 1000 check port 8008 on-marked-down shutdown-sessions
  server node_three ip.two.node.three:5432 maxconn 1000 check port 8008 on-marked-down shutdown-sessions
和该后端的前端,例如在5002端口中:

frontend ft_postgresql_all
bind *:5002
default_backend postgres-patroni-all

它起作用了!谢谢你的回复。