欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LVS+Keepalived+MysqlCluster搭建高可用的Mysql系统(实测可行)

程序员文章站 2024-03-20 21:01:28
...

LVS+Keepalived+MysqlCluster搭建高可用的Mysql系统(实测可行)

环境

  • CentOS7.0
    -mysql-cluster-gpl-7.1.9a-linux-i686-glibc23.tar.gz
  • keepalived-1.4.2.tar.gz

方案

类型 ip
Management node 192.168.1.2
Data node 192.168.1.2
Data node 192.168.1.3
MASTER 192.168.1.11
BACKUP 192.168.1.13
BACKUP 192.168.1.14
SQL node(LVS节点) 192.168.1.3
SQL node(LVS节点) 192.168.1.4

安装MysqlCluster

在此不再赘述,安装详情见我的上一篇文章MysqlCluster安装实测可行

安装keepalived

安装必要的插件(官网上要求的和一些必备的)
 - yum install curl gcc openssl-devel libnl3-devel net-snmp-devel
 - yum install -y libnfnetlink-devel(解决错误:configure: error: libnfnetlink headers missing)
 - yum install kernel-devel ipvsadm -y
安装
  • 上传文件到/etc/local/,解压
  • tar -zxvf keepalived-1.4.2.tar.gz
  • cd keepalived-1.4.2
  • ./configure –prefix=/usr/local/keepalived(prefix:指定安装目录)
  • make
  • make install
    -完成以上步骤 ,安装就完成了,目录结构如下:
    LVS+Keepalived+MysqlCluster搭建高可用的Mysql系统(实测可行)

配置MASTER和BACKUP

  • 在 /etc/keepalived创建keepalived.conf,文件内容如下:
global_defs {
    router_id LVS_DEVEL
}
vrrp_instance VI_MYSQL_1 {
        state MASTER //(备机:BACKUP)
        interface ens33
        virtual_router_id 55
        priority 150 //(备机:100)
        advert_int 5
        authentication {
                auth_type PASS
                auth_pass 010203
        }
        virtual_ipaddress {
                192.168.1.200
        }
}
virtual_server 192.168.1.200 3306 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        persistence_timeout 6
        protocol TCP

        real_server 192.168.1.2 3306 {
                weight 1
                TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 3306
                }
        }
        real_server 192.168.1.3 3306 {
                weight 1
                TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 3306
                }
        }
        real_server 192.168.1.4 3306 {
                weight 1
                TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 3306
                }
        }CKUP
}
  • 备机使用scp命令从主机拷贝
/etc/keepalived/keepalived.conf 192.168.1.14:/etc/keepalived/`
  • 移动启动相关的文件
cp  /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

配置LVS节点

(这一步至关重要,不然VIP无法跟LVS节点建立连接,keealived运行正常,但是client就是连不上VIP)


  • vi /etc/rc.d/init.d/realserver.sh 文件内容如下:
    SNS_VIP=192.168.1.200设定VIP,如果你的VIP不是该ip,请修改
#!/bin/bash
# description: Config realserver lo and apply noarp
SNS_VIP=192.168.1.200

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

case "$1" in
start)
       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
       /sbin/route add -host $SNS_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 "RealServer Start OK"

       ;;
stop)
       ifconfig lo:0 down
       route del $SNS_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 "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac

exit 0
  • 赋予该执行权限
1.chmod +x /etc/rc.d/init.d/functions
2.chmod +x /etc/rc.d/init.d/realserver.sh
  • 启动
/etc/rc.d/init.d/realserver.sh start 
  • ifconfig查看新增的虚拟ip
    LVS+Keepalived+MysqlCluster搭建高可用的Mysql系统(实测可行)

启动keepalived

  • 启动项在 cd /etc/init.d
  • ./keealived start
  • 查看日志文件
    tail -f /var/log/message
    LVS+Keepalived+MysqlCluster搭建高可用的Mysql系统(实测可行)
  • ipvsadm
    LVS+Keepalived+MysqlCluster搭建高可用的Mysql系统(实测可行)

  • 连接VIP
    LVS+Keepalived+MysqlCluster搭建高可用的Mysql系统(实测可行)

  • ipvsadm -lc
    LVS+Keepalived+MysqlCluster搭建高可用的Mysql系统(实测可行)

测试

  • 当主机挂了,备机就会变成主机提供服务
  • 当主机恢复后,备机将从主机回到备机状态
  • keepalived主机会监听LVS节点,当有SQL(LVS)挂点或上线,主机会第一时间与其建立联系或者断开连接

总结

配置LVS节点至关重要,务必配置。

LVS+keepalived+MysqlCluster实现了mysql的高可用性,高扩展。单MysqlCluster是无法完成这个任务的,keepalived实现了负载均衡,从而将高可用体现了出来。