Postgresql 我们如何使用HAproxy实现对postgres master的路由请求

Postgresql 我们如何使用HAproxy实现对postgres master的路由请求,postgresql,haproxy,Postgresql,Haproxy,我正在尝试为postgres数据库设置负载平衡。我们有一个博士后的主人和奴隶。我配置了HA代理以实现负载平衡。简单的负载平衡工作正常 此数据库用于bitbucket 当bitbucket通过loadbalancer将数据写入数据库时,如果它正在命中master(主控)则没有问题,但当它命中处于读取模式的Slave(从控)时会遇到问题,因为它无法更新数据库 我怎么能说HAproxy将请求转发给博士后硕士 从要在主机和从机上执行的pg_stat_复制中选择*并根据输出将请求路由到流媒体主机,我们是否

我正在尝试为postgres数据库设置负载平衡。我们有一个博士后的主人和奴隶。我配置了HA代理以实现负载平衡。简单的负载平衡工作正常

此数据库用于bitbucket

当bitbucket通过loadbalancer将数据写入数据库时,如果它正在命中master(主控)则没有问题,但当它命中处于读取模式的Slave(从控)时会遇到问题,因为它无法更新数据库

我怎么能说HAproxy将请求转发给博士后硕士

从要在主机和从机上执行的pg_stat_复制中选择*并根据输出将请求路由到流媒体主机,我们是否可以使用HAproxy实现它

谢谢,
Karthik

这可以通过自定义运行状况检查来完成,只有在服务器是主服务器的情况下才会成功。所有其他从属服务器都将被标记下来

 backend pgsql
   mode tcp
   option tcp-check
   tcp-check send-binary 0000002c # length
   tcp-check send-binary 00030000 # version
   tcp-check send-binary 75736572 # 'user'.bytes.map { |c| c.to_s(16) }
   tcp-check send-binary 00 # string terminator
   tcp-check send-binary 6170706c69636174696f6e # 'application'
   tcp-check send-binary 00 # string terminator
   tcp-check send-binary 6461746162617365 # 'database'
   tcp-check send-binary 00 # string terminator
   tcp-check send-binary 706f737467726573 # 'postgres'
   tcp-check send-binary 00 # string terminator
   tcp-check send-binary 00 # name/value terminator
   tcp-check expect string R
   tcp-check send-binary 51 # 'Q'
   tcp-check send-binary 00000020 # length
   tcp-check send-binary 73656c6563742070675f69735f696e5f7265636f7665727928293b # 'select pg_is_in_recovery();'
   tcp-check send-binary 00 # string terminator
   tcp-check send-binary 58 # 'X'
   tcp-check send-binary 00000004 # length
   tcp-check expect string f
   tcp-check expect string Z
   server pg1 1.2.3.4:5432 check
   server pg2 5.6.7.8:5432 check
tcp检查线路使用Postgres客户机协议发送和接收二进制数据包,该协议在tcp检查线路中定义

此外,您还可以拥有辅助“只读”后端,该后端仅在主后端出现故障时使用。这是额外的前端/后端配置

 frontend pgsql
   mode tcp
   bind *:5432
   acl readonly.pgsql nbsrv(pgsql) eq 0
   use_backend readonly.pgsql if readonly.pgsql
   acl pgsql nbsrv(pgsql) gt 0
   use_backend pgsql if pgsql

 <...primary backend...>

 backend readonly.pgsql
   mode tcp
   option tcp-check
   tcp-check send-binary 0000002c # length
   tcp-check send-binary 00030000 # version
   tcp-check send-binary 75736572 # 'user'.bytes.map { |c| c.to_s(16) }
   tcp-check send-binary 00 # string terminator
   tcp-check send-binary 6170706c69636174696f6e # 'application'
   tcp-check send-binary 00 # string terminator
   tcp-check send-binary 6461746162617365 # 'database'
   tcp-check send-binary 00 # string terminator
   tcp-check send-binary 706f737467726573 # 'postgres'
   tcp-check send-binary 00 # string terminator
   tcp-check send-binary 00 # name/value terminator
   tcp-check expect string R
   tcp-check send-binary 58 # 'X'
   tcp-check send-binary 00000004 # length
   server pg1 1.2.3.4:5432 check
   server pg2 5.6.7.8:5432 check
如果将奴隶标记为“下岗”是一种良好的HAProxy健康检查方法,这是有争议的。上述方法确实有效,但在没有HAProxy的情况下无法实现