温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Haproxy+Keepalived+MySQL实现读均衡负载

发布时间:2020-08-11 00:12:47 来源:ITPUB博客 阅读:128 作者:kunlunzhiying 栏目:MySQL数据库

环境说明:

     本系统对数据安全性不高,TPS操作很低,主要是查询的环境。

本方案架构图如下:
     应用服务器对只读的应用程序连接虚拟IP地址,连接到haproxy,然后通过haproxy将TCP协议转移到下面的3个数据库服务器中。
Haproxy在此做4层的TCP交换服务。keepalived为了防止haproxy单点故障。
      
为什么不用LVS?
    我们的环境服务器少,haproxy和lvs相比性能差不多,且haproxy自带有mysql check,不需要额外的写mysql侦测脚本。

Haproxy+Keepalived+MySQL实现读均衡负载

1.haproxy安装


下载地址:http://www.haproxy.org/download/1.5/src/haproxy-1.5.5.tar.gz

依赖包安装:
#yum install gcc gcc-c++ make zlib-devel bzip2-devel openssl-devel

安装pcre,自带的pcre-7.8-3.1.el6.x86_64貌似版本低了,导致无法安装proxy
#tar xzvf pcre-8.34.tar.gz
#cd pcre-8.34
#./configure --prefix=/usr \
--docdir=/usr/share/doc/pcre-8.34 \
--enable-utf --enable-unicode-properties \
--enable-pcregrep-libz --enable-pcregrep-libbz2
#make
#make check
#make install

script "/etc/keepalived/check_haproxy.sh"

interval 2
weight 2


global_defs {
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER 
interface bond0
virtual_router_id 51
priority 150 
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
 
track_script {
chk_http_port
}
 
virtual_ipaddress {
192.168.231.18 
}
}
}

配置侦测haproxy状态脚本:
#vi /etc/keepalived/check_haproxy.sh
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];then
  /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.conf
sleep 3

if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
  /etc/init.d/keepalived stop
fi
fi

启动keepalived服务:
# /etc/init.d/keepalived start

添加新的协议地址:
#ip add
[root@mss-dn03 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 90:e2:ba:23:87:04 brd ff:ff:ff:ff:ff:ff
3: eth2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 90:e2:ba:23:87:05 brd ff:ff:ff:ff:ff:ff

9: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 6c:ae:8b:26:52:3b brd ff:ff:ff:ff:ff:ff
    inet 192.168.231.7/24 brd 192.168.231.255 scope global bond0
    inet6 fe80::6eae:8bff:fe26:523b/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever

设置开启自启动:
# chkconfig  --level 0123456 keepalived on
# chkconfig --list keepalived


测试过程中发现,master宕机后,VIP需要几十秒才能ping通,切换很慢:
arping -I bond0 -c 5 -s VIP GATEWAY
arping -I bond0 -c 5 -s 192.168.231.18 192.168.231.254



Getting started with HAProxy and MySQL replication(能实现读写路由):
    http://blog.yannickjaquier.com/linux/getting-started-with-haproxy-and-mysql-replication.html
Haproxy配置日志:
    http://www.linuxidc.com/Linux/2012-06/63585.htm


来自为知笔记(Wiz)


向AI问一下细节
推荐阅读:
  1. 配置MST实现负载均衡
  2. Nginx是怎么实现负载均衡的?Nginx负载均衡实现案例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

猜你喜欢

AI