MHA+LVS+Keepalived搭建MySQL高可用集群

本文讲解MHA+LVS+Keepalived搭建MySQL集群的方法,当主库故障时通过MHA实现快速完成主从切换。并且,以此为基础,能够很轻松的实现横向扩展,比如读写分离、扩展主库、扩展从库。

系统环境

操作系统 CentOS 7.2 x64
数据库 MySQL 5.7
应用平台 MHA / Perl / LVS / Keepalived

服务器部署方案及说明

  1. 如果需要实现MHA高可用,可以将它部署在两台LVS中,或者再加一台,同样通过keepalived实现高可用。
  2. LVS通过Keepalived实现高可用,其功能是负载多台MySQL从库,实现MySQL读分离,此时从库可以随意横向扩展。
  3. 当MySQL主库出现故障后,MHA将MySQL-candicate切换IP并提升为主库,同时将其VIP漂移至其他从库服务器上。

服务器IP 主机名 应用 角色 备注
10.10.10.11 MYSQL-A mysql mha-node master server_id=11
10.10.10.12 MYSQL-B mysql mha-node candicate server_id=12
10.10.10.13 MYSQL-C mysql mha-node slave server_id=13
10.10.10.101 MHA mha-manager mha-node monitor
10.10.10.201 LVS-A ipvsadm keepalived lvs ha 10.10.10.200
10.10.10.202 LVS-B ipvsadm keepalived

部署MySQL

安装数据库

在各数据库节点上安装MySQLl5.7,安装方法略。

数据库集群方案为1主2从,2个从库中,1个用于主从切换,1个恒从库用于同步relay-log。

配置同步账号

在各数据库创建用于mha的复制账号:

grant all privileges on *.* to 'repl'@'10.10.10.%' identified by 'repltellu';
flush privileges;

配置master

修改配置/etc/my.cnf

[server]
server_id=11
log_bin=master-log
relay-log=relay-bin
skip_name_resolve=ON
innodb_file_per_table=ON
max_connections=65536

配置slave

修改配置/etc/my.cnf

[server]
# 修改server_id
server_id=1[x]
log-bin=master-bin
relay-log=relay-bin
skip_name_resolve=ON
innodb_file_per_table=ON
max_connections=65536
# 关闭中继日志自动修剪
relay_log_purge=0
# 将从节点设置为只读
read_only=1

定时清理relay-log:

# 创建日志目录
mkdir /opt/mha/log
# 修改crontab
crontab -e

加入以下定时任务:

# 每天凌晨2点清理日志
0 2 * * * /usr/bin/purge_relay_logs --user=root --disable_relay_log_purge >> /opt/mha/log/purge_relay_logs.log 2>&1

从库开启绑定VIP的服务

修改文件/etc/init.d/realserver

#./etc/rc.d/init.d/functions

LVS_VIP=10.10.10.200
case "$1" in
start)
ifconfig lo:0 $LVS_VIP netmask 255.255.255.255 broadcast $LVS_VIP
/sbin/route add -host $LVS_VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "Server Started"
;;
stop)
ifconfig lo:0 down
route del $LVS_VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "Server Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0

启动服务realserver:

chmod +x realserver
service realserver start

部署MHA

0x01 安装mha4mysql-manager

首先,安装基础依赖:

yum -y install epel-release
yum -y install perl perl-devel perl-CPAN perl-Config-Tiny perl-DBD-MySQL perl-ExtUtils-MakeMaker perl-Log-Dispatch.noarch perl-Parallel-ForkManager.noarch

然后,下载MHA资源包并解压,依次安装node和manager,本文以0.57版本为例:

# 编译安装方法
tar -zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install

# rpm安装方法
rpm -ivh mha4mysql-manager-0.57-0.el7.noarch.rpm

0x02 配置mha4mysql-manager

在各数据库创建用于mha的复制账号:

grant all privileges on *.* to 'repl'@'10.10.10.%' identified by 'repltellu';
flush privileges;

将源码包中的masterha_default.cnf和app1.cnf复制到/etc/mha/,编辑配置/etc/mha/app1.cnf

[server default]
# mha工作目录
manager_workdir=/opt/mha/app1
# mha日志目录
manager_log=/opt/mha/app1/manager.log
# mha复制账号
# user=mysql
# password=mysqltellu
# ssh_user=mysql
repl_user=repl
repl_password=repltellu
# mha检测延时
ping_interval=1
# mha检测脚本
# shutdown_script="/opt/mha/scripts/power_manager"
# master_ip_failover_script="/opt/mha/scripts/master_ip_failover"
# master_ip_online_change_script="/opt/mha/scripts/master_ip_online_change"
# report_script="/opt/mha/scripts/send_report"

[server1]
hostname=10.10.10.11
master_binlog_dir="/data/mysql/master-log"
candidate_master=1

[server2]
hostname=10.10.10.12
master_binlog_dir="/data/mysql/master-log"
candidate_master=1

[server3]
hostname=10.10.10.13
no_master=1

在源码包的samples目录中,包含以上配置中conf和scripts文件。

0x03 启动mha4myql-manager

# 启动
masterha_manager --conf=/etc/mha/app1.cnf 2>&1 &
# 停止
masterha_stop --conf=/etc/mha/app1.cnf

1x01 部署mha4mysql-node

各数据库节点安装mha4mysql-node:

# 编译安装方法
tar -zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install

# rpm安装方法
rpm -ivh mha4mysql-node-0.57-0.el7.noarch.rpm

MHA管理端与各数据库节点、各数据库节点之间配置免密登录:

# 配置MHA管理端与各数据库节点免密
ssh-keygen -t rsa -P ''
cat .ssh/id_rsa.pub >> .ssh/authorized_keys
chmod go= .ssh/authorized_keys
for i in 11 12 13;do scp -p /root/.ssh/id_rsa /root/.ssh/authorized_keys root@10.10.10.$i:/root/.ssh/ ;done
# 参照上述方法实现其他节点间的免密

测试配置结果:

masterha_check_ssh --conf=/etc/mha/app1.cnf
masterha_check_repl --conf=/etc/mha/app1.cnf

部署LVS+keepalived

# 安装ipvsadm+Keepalived
yum -y install ipvsadm keepalived

配置LVS

开启路由转发功能:

sed -i 's/ip_forward = 0/ip_forward = 1/p' /etc/sysctl.conf
sysctl -p

配置Keepalived

分离keepalived日志:

sed -i 's/KEEPALIVED_OPTIONS="-D"/KEEPALIVED_OPTIONS="-D -d -S 0"/g' /etc/sysconfig/keepalived

修改配置/etc/keepalived/keepalived.conf

global_defs {
    router_id LVS
}

# 如果masterha_manager非lvs集群,不需要此段
# 其中check_mha4mysql_manager.sh在资源包中
vrrp_script check_masterha_manager {
    script "/opt/scripts/check_mha4mysql_manager.sh"
    interval 3
}

vrrp_instance MYSQL_SLAVE {

    state MASTER    #从lvs上把 MASTER 改为 BACKUP
    interface eno16777736
    virtual_router_id 200
    priority 100    #从lvs上把 100 改为 90
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.200
    }

    nopreempt
    # smtp_alter

    # 若配置了mha通过lvs高可用须取消以下3行注释,本人的mha是单机所以不需要执行以下脚本
    # notify_master "/opt/scripts/notify_master.sh"
    # notify_backup "/opt/scripts/notify_stop.sh"
    # notify_stop "/opt/scripts/notify_stop.sh"
}

virtual_server_group MysqlRead { 
    10.10.10.200 3306
} 

virtual_server 10.10.10.200 3306 {
    delay_loop 6
    lb_algo wlc
    lb_kind DR
    nat_mask 255.255.255.0 
    #persistence_timeout 
    protocol TCP

    # 如果想加设一台专用故障转移的备机
    # sorry_server 10.10.10.10 3306
    real_server 10.10.10.12 3306 {
        weight 6
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }

    real_server 10.10.10.13 3306{
    weight 0
    TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306
        }
    }
}

原创文章禁止转载:技术学堂 » MHA+LVS+Keepalived搭建MySQL高可用集群

赞 (0) 打赏

精彩评论

9+6=

感谢您的支持与鼓励

支付宝扫一扫打赏

微信扫一扫打赏