HAProxy+keepalived+MySQL 实现MHA中slave集群负载均衡的高可用

HAProxy+keepalived+MySQL实现MHA中slave集群的负载均衡的高可用

Ip地址划分:

240    mysql_b2

242    mysql_b1

247    haproxy1

249    haproxy2

243    VIP

248    client

1、安装HAProxy

yum install gcc -y

make TARGET=linux26 PREFIX=/usr/local/haproxy

make install PREFIX=/usr/local/haproxy

cp /usr/src/haproxy/haproxy-1.5.3/examples/haproxy.cfg /usr/local/haproxy/

cp /usr/src/haproxy/haproxy-1.5.3/examples/haproxy.init /etc/init.d/haproxy

chmod 755 /etc/init.d/haproxy

ln -s /usr/local/haproxy/sbin/* /usr/sbin/

mkdir /etc/haproxy

mkdir /usr/share/haproxy

ln -s /usr/local/haproxy/haproxy.cfg /etc/haproxy/

cd ..

1.2 设置配置文件

1.2.1 拷贝配置文件

[root@HAProxy_247 haproxy]# cp haproxy.cfg  /usr/local/haproxy/

cp: overwrite `/usr/local/haproxy/haproxy.cfg'? y

1.2.2  编辑配置文件

[root@HAProxy_247 haproxy]# vim /usr/local/haproxy/haproxy.cfg

注意修改RS的IP:

HAProxy+keepalived+MySQL 实现MHA中slave集群负载均衡的高可用

1.3 启动HAProxy服务

[root@HAProxy_247 haproxy]# service haproxy start

HAProxy+keepalived+MySQL 实现MHA中slave集群负载均衡的高可用

1.4 访问HAProxy的网页

http://localhost:8899/admin-status

用户名密码查看配置文件设置

1.5 客户端测试

HAProxy+keepalived+MySQL 实现MHA中slave集群负载均衡的高可用

2、安装配置keepalived

2.1 修改yum仓库

[aa]

baseurl=file:///mnt/LoadBalancer

gpgcheck=0

ebabled=1

2.2 安装keepalived

yum install keepalived -y

2.3 修改247上配置文件(覆盖原配置文件即可)

注:keepalived工作机制--同一组(virtual_router_id)的keepalived互相检测存活,优先级高的作用时,优先级低的沉睡。当优先级高的宕掉时,优先级低的顶替其工作。

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

vrrp_script chk_haproxy {

    script "/etc/keepalived/chk.sh"

    interval 

}

vrrp_instance VI_1 {

    state BACKUP

    nopreempt

    interface eth0

    virtual_router_id 51      #定义分组ID,同一组ID相同

    priority 100    #优先级

    advert_int 

    authentication {

        auth_type PASS

        auth_pass abcd

    }

    virtual_ipaddress {

        192.168.18.243      #VIP

    }

    track_script {

        chk_haproxy

    }

    notify_backup "/etc/init.d/haproxy restart"      //切换主/备/出错时执行的脚本

    notify_fault "/etc/init.d/haproxy stop"

}

2.4 修改249上的主配置文件

! Configuration File for keepalived

vrrp_script chk_haproxy {

    script "/etc/keepalived/chk.sh"

    interval 

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 99

    advert_int 

    authentication {

        auth_type PASS

        auth_pass abcd

    }

    virtual_ipaddress {

        192.168.18.243

    }

    track_script {

        chk_haproxy

    }

    notify_backup "/etc/init.d/haproxy restart"

    notify_fault "/etc/init.d/haproxy stop"

}

2.5 编写脚本,并放到指定位置

vim  /etc/keepalived/chk.sh

#!/bin/bash

if [ $(ps -C haproxy --no-header | wc -l) -eq  ]; then    #判断haproxy是否存活,如果宕了,停止keepalived服务

       /etc/init.d/keepalived stop

fi

chmod +x chk.sh

2.6 开启keepalived并验证

service keepalived restart

HAProxy+keepalived+MySQL 实现MHA中slave集群负载均衡的高可用

HAProxy+keepalived+MySQL 实现MHA中slave集群负载均衡的高可用

在247上关闭HAProxy后:

HAProxy+keepalived+MySQL 实现MHA中slave集群负载均衡的高可用

HAProxy+keepalived+MySQL 实现MHA中slave集群负载均衡的高可用

2.7 客户端测试

HAProxy+keepalived+MySQL 实现MHA中slave集群负载均衡的高可用

MHA中failover后,进入HAProxy配置文件中将new_master从HAProxy分发列表中剔除

上一篇:STM8S PWM输出停止后 IO口电平输出


下一篇:Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)