load-balancing - 使用 HAProxy for Postgresql 9.4 的负载平衡

标签 load-balancing postgresql-9.3 database-replication haproxy multi-master-replication

我已经在 4 个节点(虚拟机)之间使用 BDR(双向复制)设置了 PostgreSQL 的多主复制。

现在我想为高可用性放置一个负载平衡器。为此,我在不同的虚拟机上安装并配置了“HAProxy”,该虚拟机正在监听 5432/tcp 以进行连接。 haproxy的配置如下:

listen pgsql_bdr *:5432
    mode tcp
    option  httpchk
    balance roundrobin
    server master 192.168.123.1:5432 check backup
    server slave1 192.168.123.2:5432 check
    server slave2 192.168.123.3:5432 check
    server slave3 192.168.123.4:5432 check

HAProxy服务器的ip地址是192.168.123.5

我在我的应用程序中使用了 HAproxy 服务器的 IP 地址来连接数据库(必须将连接重定向到实际的数据库服务器)。但当时我收到以下错误:

Error connecting to the server: server closed the connection unexpectedly. This probably means the server terminated abnormally before or while processing the request.



请注意,我已尝试使用 2 种方式解决问题。 1st 我禁用了所有服务器(HAProxy 和所有 postgres 服务器)上的防火墙,并且我尝试用以下内容替换配置:
listen pgsql_bdr 0.0.0.0:5432

或者
listen pgsql_bdr 127.0.0.1:5432

或者
listen pgsql_bdr localhost:5432

但在我的情况下,一切都不起作用。

请帮我解决问题。在这种情况下我做错了什么?

提前致谢!

最佳答案

检查您的haproxy /stats ,我猜所有后端都被标记了,由于错误检查 - postgres 可能不会响应 200 - option httpchk .使用 pgsql-check

option pgsql-check user healcheckuser

我也不知道 BDR 要求/限制,但在从站设置中很常见,有一些池化器,例如pgbouncer - 在客户端旁边,或在每个后端之前,或任何需要的地方。

编辑

要检查统计信息,您可以使用 stats 套接字:
# stats about all frontents and backends in a csv format
echo "show stat" | socat unix-connect:/run/haproxy/admin.sock stdio

# version, PID, current connections, session rates, tasks, etc
echo "show info" | socat unix-connect:/run/haproxy/admin.sock stdio

# sessions with the used backend/frontend, the source
echo "show sess" | socat unix-connect:/run/haproxy/admin.sock stdio

# informations about errors if there are any
echo "show errors" | socat unix-connect:/run/haproxy/admin.sock stdio

当我用 postgres 9.4 测试时,(一些奴隶,没有 BDR),haproxy 1.5.14,没有检查
global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

listen pgsql *:15432
    mode tcp
    balance roundrobin
    server master dev1.db.fed:5432
    server slave1 dev2.db.fed:5432

我能够毫无问题地连接:
root@lb:/# echo "show sess" | socat unix-connect:/run/haproxy/admin.sock stdio
0x20d89f0: proto=tcpv4 src=10.163.74.109:46815 fe=pgsql be=pgsql srv=slave1 ts=08 age=3m8s calls=3 rq[f=848202h,i=0,an=00h,rx=,wx=,ax=] rp[f=048202h,i=0,an=00h,rx=,wx=,ax=] s0=[7,8h,fd=1,ex=] s1=[7,8h,fd=2,ex=] exp=
0x20e11a0: proto=unix_stream src=unix:1 fe=GLOBAL be=<NONE> srv=<none> ts=09 age=0s calls=2 rq[f=c08200h,i=0,an=00h,rx=30s,wx=,ax=] rp[f=008002h,i=0,an=00h,rx=,wx=,ax=] s0=[7,8h,fd=3,ex=] s1=[7,0h,fd=-1,ex=] exp=30s

查询正在运行,数据即将到来,似乎没问题。

但同样,我目前无法测试 BDR。

关于load-balancing - 使用 HAProxy for Postgresql 9.4 的负载平衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34511423/

相关文章:

amazon-web-services - AWS 将流量从 NLB 传递到 ALB?

mysql - 如何查找mysql DB是slave?

amazon-ec2 - EC2 中的 Elastic Load Balancing 有多好

postgresql - 定期 RDS Postgresql 复制延迟

sql - 不区分大小写的外键 PostgreSQL

sql - 从两个不同的选择语句中获取两行

mysql - Percona 状态转移失败

mysql 'replace into' 仅在复制从属上失败

load-balancing - HAProxy 通用计数器和棒表

nginx - 关于 nginx 上游模块中的 ip_hash