keepalived保证mysql主从自动切换

关于mysql主从配置请查看文章:https://www.jianshu.com/p/86eac0db552b。
本篇将的是如何用keepalived保证mysql主从的自动切换。
keepalive(mysql) vip:192.168.1.66
keepalive(mysql) master:192.168.1.60
keepalive(mysql) slave:192.168.1.61

1、keepalived 节点配置

global_defs:主要是配置故障发生时的通知对象以及机器标识。

vrrp_instance:用来定义对外提供服务的VIP区域及其相关属性。

virtual_server:虚拟服务器定义

1.1、master节点配置如下:

! Configuration File for keepalived

global_defs {
   router_id MYSQL-1 //表示运行keepalived服务器的一个标识
}

// 定义自动核对mysql的脚本
vrrp_script check_mysql {
    script "/etc/keepalived/bin/check_mysql.sh" 
    interval 22
    weight 2   
} 

vrrp_instance VI_1 {
    state BACKUP //指定keepalived的角色, 两台配置此处均是BACKUP,设为BACKUP将根据优先级决定主或从
    interface eth1 //指定HA监测网络的接口
    virtual_router_id 51 /虚拟路由标识,这个标识是一个数字(取值在0-255之间,用来区分多个instance的VRRP组播),同一个vrrp实例使用唯一的标识,确保和master2相同,同网内不同集群此项必须不同,否则发生冲突。
    priority 100   //用来选举master的,要成为master,该项取值范围是1-255(在此范围之外会被识别成默认值100),此处master2上设置为50
    advert_int 1 //发VRRP包的时间间隔,即多久进行一次master选举(可以认为是健康查检时间间隔)
    nopreempt //不抢占,即允许一个priority比较低的节点作为master,即使有priority更高的节点启动
    authentication {
        //认证区域,认证类型有PASS和HA(IPSEC),推荐使用PASS(密码只识别前8位)
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        // 指定vip地址
        192.168.1.66
    }
    track_script {
        //指定核对的脚本,check_mysql是上述自定义的
        check_mysql
    }

}

virtual_server 192.168.1.66 3306 {
    delay_loop 2  //设置运行情况检查时间,单位是秒
    lb_algo rr   //设置后端调度算法,这里设置为rr,即轮询算法
    lb_kind DR //设置LVS实现负载均衡的机制,有NAT、TUN、DR三个模式可选
    persistence_timeout 50 //会话保持时间,单位是秒。这个选项对动态网页是非常有用的,为集群系统中的session共享提供了一个很好的解决方案。有了这个会话保持功能,用户的请求会被一直分发到某个服务节点,直到超过这个会话的保持时间。
    protocol TCP //指定转发协议类型,有TCP和UDP两种

    real_server 192.168.1.60 3306 {
        weight 3 //配置服务节点的权值,权值大小用数字表示,数字越大,权值越高,设置权值大小为了区分不同性能的服务器
        notify_stop /etc/keepalived/bin/shutdown.sh //检测到mysql服务down后执行的脚本,在一些版本中叫notify_down
        TCP_CHECK {
            connect_timeout 3 //连接超时时间
            nb_get_retry 3 //重连次数
            delay_before_retry 3 //重连间隔时间
            connect_port 3306 //健康检查端口
        }
    }
}

没有注释的代码如下:

! Configuration File for keepalived

global_defs {
   router_id MYSQL-1
}

vrrp_script check_mysql {
    script "/etc/keepalived/bin/check_mysql.sh" 
    interval 22
    weight 2   
} 

vrrp_instance VI_1 {
    state BACKUP 
    interface eth1
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt 
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.66
    }
    track_script {
        check_mysql
    }

}

virtual_server 192.168.1.66 3306 {
    delay_loop 2
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.1.60 3306 {
        weight 3
        notify_stop /etc/keepalived/bin/shutdown.sh 
        TCP_CHECK {
            connect_timeout 3 
            nb_get_retry 3 
            delay_before_retry 3 
            connect_port 3306 
        }
    }
}

上述是mysql的master节点的配置,slave的配置只需修改如下三个参数:

global_defs 的router_id 改为MYSQL-2
vrrp_instance VI_1的priority 改为50;
virtual_server 192.168.1.66 3306的real_server 192.168.1.60 3306改为192.168.1.61 3306;

1.2、slave节点配置如下:

! Configuration File for keepalived

global_defs {
   router_id MYSQL-2
}

vrrp_script check_mysql {
    script "/etc/keepalived/bin/check_mysql.sh" 
    interval 22
    weight 2   
} 

vrrp_instance VI_1 {
    state BACKUP 
    interface eth1
    virtual_router_id 51
    priority 50
    advert_int 1
    nopreempt 
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.66
    }
    track_script {
        check_mysql
    }

}

virtual_server 192.168.1.66 3306 {
    delay_loop 2
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.1.61 3306 {
        weight 3
        notify_stop /etc/keepalived/bin/shutdown.sh 
        TCP_CHECK {
            connect_timeout 3 
            nb_get_retry 3 
            delay_before_retry 3 
            connect_port 3306 
        }
    }
}

1.3 check_mysql.sh和shutdown.sh文件

check_mysql.sh

#!/bin/bash
MYSQL_HOST=localhost 
MYSQL_USER=root 
MYSQL_PASSWORD=Dogoicq@Sec 
   
mysql -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >/dev/null 2>&1 
if [ $? == 0 ] 
then 
    echo " $host mysql login successfully " 
    exit 0 
else 
    service keepalived stop 
    exit 2 
fi

shutdown.sh

#!/bin/bash
service keepalived stop 

2、启动keepalive

命令:service keepalived start
启动后检查本地地址是否启动成功:ip addr

3: eth1:  mtu 1500 qdisc mq state UP qlen 1000
    link/ether 6c:92:bf:5d:04:d3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.60/26 brd 192.168.1.63 scope global eth1
       valid_lft forever preferred_lft forever
    inet 192.168.1.66/32 scope global eth1
       valid_lft forever preferred_lft forever

看到inet 192.168.1.66/32 即表明keepalive启动成功。
启动过程中遇到的问题:
1)刚开始配置的是eth0,启动后没有查看到192.168.1.66/32,查看日志/var/log/messages发现:

Keepalived_vrrp[16653]: Kernel is reporting: interface eth0 DOWN

查看ip addr发现,eth0是关闭的:

2: eth0:  mtu 1500 qdisc mq state DOWN qlen 1000

指定网卡为eht1:

vrrp_instance VI_1 {
    interface eth1
}

3、远程登录mysql

在远程登录之前,确保mysql master和slave节点有被远程访问的权限:

grant all PRIVILEGES on *.* to 'root'@'192.168.1.59' identified by 'pass123';

在192.168.1.59机器上登录192.168.1.66的mysql:

mysql -uroot -ppass123 -h 192.168.1.66
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.66' (113)

连接不上vip的mysql,检查发现是因为192.168.1.60和192.168.1.61防火墙的3306端口没看,开启3306端口:

iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
service iptables save
service iptables restart

再次尝试,即可连接。

4、测试keeplaived+mysql

通过vip登录后,查看当前连接:

mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+
1 row in set (0.01 sec)

server_id为1是master节点, server_id为2是slave节点。
vip mysql插入一条数据,查看slave节点数据库:

 vip:
 
 insert into runoob_tbl(runoob_title) values("adad");
 
 slave:
 
 MySQL [sysctest]> select * from runoob_tbl;
+-----------+----------------+---------------+-----------------+
| runoob_id | runoob_title   | runoob_author | submission_date |
+-----------+----------------+---------------+-----------------+
|         1 | al             | bb            | 2018-03-08      |
|         2 | ap             | bb            | 2018-03-08      |
|         3 | aq             | bb            | 2018-03-08      |
|         4 | aaaaaaaaaaaa   | bb            | 2018-03-08      |
|         5 | aaaaaaaaadddaa | bb            | 2018-03-08      |
|         6 | aaaaaaaeeeddaa | bb            | 2018-03-08      |
|         7 | aaac           | bb            | 2018-03-09      |
|         8 | adad           |               | NULL       

发现可正常插入数据。

测试master节点挂点后,vip能否自动切换:

master:
service mysqld stop

vip:
mysql> show variables like 'server_id';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    8
Current database: *** NONE ***

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+
1 row in set (0.03 sec)

mysql> select * from runoob_tbl;
+-----------+----------------+---------------+-----------------+
| runoob_id | runoob_title   | runoob_author | submission_date |
+-----------+----------------+---------------+-----------------+
|         1 | al             | bb            | 2018-03-08      |
|         2 | ap             | bb            | 2018-03-08      |
|         3 | aq             | bb            | 2018-03-08      |
|         4 | aaaaaaaaaaaa   | bb            | 2018-03-08      |
|         5 | aaaaaaaaadddaa | bb            | 2018-03-08      |
|         6 | aaaaaaaeeeddaa | bb            | 2018-03-08      |
|         7 | aaac           | bb            | 2018-03-09      |
|         8 | adad           |               | NULL            |
+-----------+----------------+---------------+-----------------+

发现vip正常切换到slave节点。同时shutdown脚本被调用,本地的keepalived进程被杀死。

ps -aux | grep keepa
root     39672  0.0  0.0 115764  1000 pts/0    S+   14:56   0:00 grep --color=auto keepalived

重启mysql进程后,记得手动重启keepalived程序。

防火墙问题

在一个时间vip只指向一个真实的IP。因此在ip addr的时候,只有一台机器可以看到vip。如下所示:

    inet 192.168.1.60/26 brd 192.168.1.191 scope global eth1
       valid_lft forever preferred_lft forever
    inet 192.168.1.66/32 scope global eth1
       valid_lft forever preferred_lft forever

网上有说通过配置防火墙支持vrrp协议。

iptables -A INPUT -d 224.0.0.18 -j ACCEPT

最好不要这么设置,因为设置完后,会出现机器reject所有的请求,无法ssh,scp等

你可能感兴趣的:(keepalived保证mysql主从自动切换)