博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用Haproxy来做PostgreSQL的负载均衡
阅读量:6991 次
发布时间:2019-06-27

本文共 4990 字,大约阅读时间需要 16 分钟。

  hot3.png

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,可以用来做web或者数据库的负载均衡,在一定范围内读写能力与添加的服务器数量成正比。haproxy用来做mysql负载均衡的文章很多,这里简单介绍下配合postgresql的使用。
一、环境
Centos 6.7
PostgreSQL 9.5.2
Haproxy 1.6
虚机服务列表
haproxy ip 192.168.226.148 (pg agent)
datanode1 ip 192.168.226.133 (pg server)
datanode2 ip 192.168.226.143 (pg server)
二、软件安装
#yum install gcc gcc-c++ make#groupadd haproxy#useradd haproxy –g haproxy -s /sbin/nologin#wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.4.tar.gz#cd haproxy-1.6.4#make TARGET=linux2628 PREFIX=/usr/local/haproxy#make install PREFIX=/usr/local/haproxy#mkdir -p /etc/haproxy/#cp examples/haproxy.init /etc/init.d/haproxy#chmod +x /etc/init.d/haproxy#ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
三、软件配置
1.主要配置文件haproxy.cfg
[root@localhost ~]# vi /etc/haproxy/haproxy.cfgglobal	log 127.0.0.1 local0 info	maxconn 4096	user haproxy	group haproxy	daemon	nbproc 1	chroot /usr/local/haproxy	pidfile /var/run/haproxy.piddefaults	log global	mode tcp	option tcplog	option dontlognull	option redispatch	retries 3	maxconn 2000	timeout connect 10s	timeout client 60s	timeout server 60sfrontend postgres-front	bind *:1949	mode tcp	default_backend postgresqllisten  postgresql	mode tcp	option tcplog	balance leastconn	#balance roundrobin	#option mysql-check user haproxy	#server mysql-1 192.168.184.131:3306 weight 1 check inter 2000 rise 2 fall 3	#server mysql-2 192.168.184.132:3306 weight 1 check inter 2000 rise 2 fall 3       option pgsql-check user haproxy       server postgre_1 192.168.184.131:1949 weight 1 check inter 2000 rise 2 fall 3       server postgre_2 192.168.184.132:1949 weight 1 check inter 2000 rise 2 fall 3listen admin-stats	mode http	option httplog	bind 0.0.0.0:8888	stats enable	stats refresh 30s	stats uri /dbs	stats realm welcome login\ Haproxy	stats auth admin:admin	stats admin if TRUE [root@localhost ~]#
2.日志文件
[root@localhost ~]# vi /etc/rsyslog.d/haproxy.conf$ModLoad imudp$UDPServerRun 514local0.* /usr/local/haproxy/haproxy.log[root@localhost ~]# vi /etc/sysconfig/rsyslogSYSLOGD_OPTIONS="-c 2 -r -m 0"[root@localhost ~]# service rsyslog restart
四、使用
1.启动关闭
[root@localhost ~]# chkconfig haproxy on[root@localhost ~]# service haproxy start/stop/restart
2.验证
打开浏览器,输入 http://192.168.226.148:8888/dbs,用户密码是上面配置的admin/admin 会出来如下页面 ,如果一台pg挂了,则相关node行数据会显示显眼的红色
3.服务端验证
在133和143服务器上各建一个库db_kenyon,创建haproxy的用户,在下面建不同的表来验证
133上建表tbl_1
[postgres@localhost ~]$ psql
psql (9.5.2) Type "help" for help.
postgres=# create database db_kenyon;
CREATE DATABASE
postgres=# \c db_kenyon
You are now connected to database "db_kenyon" as user "postgres".
db_kenyon=# create table tbl_1(id int);
CREATE TABLE
同样,143上建表tbl_2
在148上执行验证,148上建postgresql的客户端,并在133和143的pg_hba.conf里设置允许148为trust访问
host all all 192.168.226.148/32 trust
[root@localhost ~]# psql -h127.0.0.1 -p13306 -Uhaproxy db_kenyon -c"\dt"
 List of relations
 Schema | Name | Type | Owner
 --------+-------+-------+----------
public | tbl_2 | table | postgres
(1 row)
[root@localhost ~]# psql -h127.0.0.1 -p13306 -Uhaproxy db_kenyon -c"\dt"
 List of relations
 Schema | Name | Type | Owner
 --------+-------+-------+----------
public | tbl_1 | table | postgres
(1 row)
可以看到实现了负载均衡
五、性能简单压测
在两台服务器的db_kenyon库里建同样的表tbl_1,并插入10000条数据
[postgres@localhost ~]$ psql db_kenyonpsql (9.5.2)Type "help" for help.db_kenyon=# select * from tbl_1; id ----(0 rows)db_kenyon=# insert into tbl_1 select generate_series(1,10000);INSERT 0 10000db_kenyon=# \q[postgres@localhost ~]$[postgres@localhost ~]$ more test.conf \set naccounts 10000 * :scale\setrandom id 1 :naccountsSELECT id FROM tbl_1 WHERE id = :id;--单台压测[postgres@localhost ~]$ pgbench -c 25 -j 25 -M prepared -n -s 500 -T 60 db_kenyon  -f ./test.conf transaction type: Custom queryscaling factor: 500query mode: preparednumber of clients: 25number of threads: 25duration: 60 snumber of transactions actually processed: 94052latency average: 15.949 mstps = 1619.015406 (including connections establishing)tps = 1622.670100 (excluding connections establishing)--加了haproxy压测[root@localhost ~]# pgbench -c 25 -j 25 -M prepared -n -s 500 -T 60 -h127.0.0.1 -p13306 -Uhaproxy  db_kenyon -f ./test.conftransaction type: Custom queryscaling factor: 500query mode: preparednumber of clients: 25number of threads: 25duration: 60 snumber of transactions actually processed: 175073latency average: 8.568 mstps = 2916.996809 (including connections establishing)tps = 2918.372106 (excluding connections establishing)可以看到tps增加了接近一倍,假如slave节点机子越多,该tps会有更大的提高
六、关键参数及其他
主要挑几个cfg配置文件里的参数说明:
mode tcp :采用tcp模式
option pgsql-check user haproxy :haproxy内置的postgres检测模块,后面的haproxy是数据库内用户名
server postgre_1 192.168.184.131:1949 weight 1 check inter 2000 rise 2 fall 3 :设置访问节点服务,每2秒检测超过3次失败则失败,从失败转成成功则需要检测2次,weight是分配权重。同样的模块还有mysql-check 
balance leastconn :这个算法是最少连接数,还有个注释掉的roundrobin是轮训的方式

系统可以考虑加上keepalived和pgbouncer来搭整套环境,haproxy除了可以做负载均衡,还可以用来做读的ha和简单的状态监控,可以有效降低单台服务器的负载,单台压测cpu基本饱和,加了haproxy后每台cpu还能空出20%出来。

七、参考

http://www.haproxy.com/

转载于:https://my.oschina.net/Kenyon/blog/664102

你可能感兴趣的文章
同一个微信 不同的识别码 微信多开
查看>>
服务器中误删除虚拟机数据恢复过程
查看>>
iis 不能解析aspx
查看>>
使用wireshark观察SSL/TLS握手过程--双向认证/单向认证
查看>>
序列化框架性能对比(kryo、hessian、java、protostuff)
查看>>
8.31 17.1-17.5
查看>>
DevOps和容器:本地or云端,如何选择?
查看>>
人生中第一篇TOP期刊
查看>>
LVS调度策略
查看>>
MySql游标使用
查看>>
监控IO性能| free命令 |ps命令 |查看网络状态 |linux下抓包
查看>>
上传文件绕过姿势
查看>>
STP与MSTP概述
查看>>
Nginx日志分析和参数详解
查看>>
【有奖征文】51CTO男/女子图鉴 哪一面才是真的你
查看>>
MATLAB编程与应用系列-第2章 数组及矩阵的创建及操作(2)
查看>>
一步一步实现web程序信息管理系统之二----后台框架实现跳转登陆页面
查看>>
接口和抽象类是否继承了Object
查看>>
小白都能看懂的Linux系统下安装配置Zabbix
查看>>
服务器被传***怎么办
查看>>