自己搭建玩玩,不对的请大佬轻拍
1.Keepalived 原理
keepalived采用VRRP(虚拟路由冗余协议)热备份协议,以软件的方式实现Linux服务器的多机热备。VRRP是针对路由器的一种备份解决方案——由多台路由器组成一个热备
组,通过共用的虚拟IP(VIP)地址对外提供服务;每个热备份组内同一时刻只有一台主路由器提供服务,其他路由器处于冗余状态,若当前在线的路由器失效,则其他路由器会自动接
替(优先级决定接替顺序)虚拟IP地址,以继续提供服务。热备组内的每台路由器都可能成为主路由器,虚拟路由器的IP地址(VIP)可以在热备组内路由器进行转移,所以也称为漂移
IP地址。使用keepalived时,漂移地址的实现不需要手动建立虚接口配置文件(如ens33:0),而是由keepalived根据配置文件自动管理。基于VRRP的热备方式,keepalived可
以用作服务器的故障切换,每个热备组可以有多台服务器——最常用的是多机热备了。在这种多机热备方案中,故障切换主要针对虚拟IP地址的漂移来实现。因此能适用于各种应用服务
器(不管是Web、FTP、Mail、还是SSH、DNS....)。
2.Keepalived 参数说明
keepalived服务安装完成之后,后面的主要工作就是在keepalived.conf文件中配置HA和负载均衡。一个功能比较完整的常用的keepalived配置文件,主要包含三块:全局
定义块、VRRP实例定义块和虚拟服务器定义块。全局定义块是必须的,如果keepalived只用来做ha,虚拟服务器是可选的。下面是一个功能比较完整的配置文件模板:
! Configuration File for keepalived
global_defs { #全局定义部分
notification_email { #设置报警邮件地址,可设置多个
acassen@firewall.loc #接收通知的邮件地址
}
notification_email_from test0@163.com #设置 发送邮件通知的地址
smtp_server smtp.163.com #设置 smtp server 地址,可是ip或域名.可选端口号 (默认25)
smtp_connect_timeout 30 #设置 连接 smtp server的超时时间
router_id LVS_DEVEL #主机标识,用于邮件通知
vrrp_skip_check_adv_addr
vrrp_strict #严格执行VRRP协议规范,此模式不支持节点单播
vrrp_garp_interval 0
vrrp_gna_interval 0
script_user keepalived_script #指定运行脚本的用户名和组。默认使用用户的默认组。如未指定,默认为keepalived_script 用户,如无此用户,则使用root
enable_script_security #如过路径为非root可写,不要配置脚本为root用户执行。
}
vrrp_script chk_nginx_service { #VRRP 脚本声明
script "/etc/keepalived/chk_nginx.sh" #周期性执行的脚本
interval 3 #运行脚本的间隔时间,秒
weight -20 #权重,priority值减去此值要小于备服务的priority值
fall 3 #检测几次失败才为失败,整数
rise 2 #检测几次状态为正常的,才确认正常,整数
user keepalived_script #执行脚本的用户或组
}
vrrp_instance VI_1 { #vrrp 实例部分定义,VI_1自定义名称
state MASTER #指定 keepalived 的角色,必须大写 可选值:MASTER|BACKUP
interface ens33 #网卡设置,lvs需要绑定在网卡上,realserver绑定在回环口。区别:lvs对访问为外,realserver为内不易暴露本机信息
virtual_router_id 51 #虚拟路由标识,是一个数字,同一个vrrp 实例使用唯一的标识,MASTER和BACKUP 的 同一个 vrrp_instance 下 这个标识必须保持一致
priority 100 #定义优先级,数字越大,优先级越高。
advert_int 1 #设定 MASTER 与 BACKUP 负载均衡之间同步检查的时间间隔,单位为秒,两个节点设置必须一样
authentication { #设置验证类型和密码,两个节点必须一致
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个
192.168.119.130
}
track_script { #脚本监控状态
chk_nginx_service #可加权重,但会覆盖声明的脚本权重值。chk_nginx_service weight -20
}
notify_master "/etc/keepalived/start_haproxy.sh start" #当前节点成为master时,通知脚本执行任务
notify_backup "/etc/keepalived/start_haproxy.sh stop" #当前节点成为backup时,通知脚本执行任务
notify_fault "/etc/keepalived/start_haproxy.sh stop" #当当前节点出现故障,执行的任务;
}
virtual_server 192.168.119.130 80 { #定义RealServer对应的VIP及服务端口,IP和端口之间用空格隔开
delay_loop 6 #每隔6秒查询realserver状态
lb_algo rr #后端调试算法(load balancing algorithm)
lb_kind DR #LVS调度类型NAT/DR/TUN
#persistence_timeout 60 同一IP的连接60秒内被分配到同一台realserver
protocol TCP #用TCP协议检查realserver状态
real_server 192.168.119.120 80 {
weight 1 #权重,最大越高,lvs就越优先访问
TCP_CHECK { #keepalived的健康检查方式HTTP_GET | SSL_GET | TCP_CHECK | SMTP_CHECK | MISC
connect_timeout 10 #10秒无响应超时
retry 3 #重连次数3次
delay_before_retry 3 #重连间隔时间
connect_port 80 #健康检查realserver的端口
}
}
real_server 192.168.119.121 80 {
weight 1 #权重,最大越高,lvs就越优先访问
TCP_CHECK { #keepalived的健康检查方式HTTP_GET | SSL_GET | TCP_CHECK | SMTP_CHECK | MISC
connect_timeout 10 #10秒无响应超时
retry 3 #重连次数3次
delay_before_retry 3 #重连间隔时间
connect_port 80 #健康检查realserver的端口
}
}
}
复制
工作模式
# keepalive通过组播,单播等方式(自定义),实现keepalive主备推选。工作模式分为抢占和非抢占(通过参数nopreempt来控制)。
1)抢占模式:
主服务正常工作时,虚拟IP会在主上,备不提供服务,当主服务优先级低于备的时候,备会自动抢占虚拟IP,这时,主不提供服务,备提供服务。
也就是说,工作在抢占模式下,不分主备,只管优先级。
如上配置,不管keepalived.conf里的state配置成master还是backup,只看谁的priority优先级高(一般而言,state为MASTER的优先级要高于BACKUP)。
priority优先级高的那一个在故障恢复后,会自动将VIP资源再次抢占回来!!
2)非抢占模式:
这种方式通过参数nopreempt(一般设置在advert_int的那一行下面)来控制。不管priority优先级,只要MASTER机器发生故障,VIP资源就会被切换到BACKUP上。
并且当MASTER机器恢复后,也不会去将VIP资源抢占回来,直至BACKUP机器发生故障时,才能自动切换回来。
千万注意:
nopreempt这个参数只能用于state为backup的情况,所以在配置的时候要把master和backup的state都设置成backup,这样才会实现keepalived的非抢占模式!
也就是说:
a)当state状态一个为master,一个为backup的时候,加不加nopreempt这个参数都是一样的效果。即都是根据priority优先级来决定谁抢占vip资源的,是抢占模式!
b)当state状态都设置成backup,如果不配置nopreempt参数,那么也是看priority优先级决定谁抢占vip资源,即也是抢占模式。
c)当state状态都设置成backup,如果配置nopreempt参数,那么就不会去考虑priority优先级了,是非抢占模式!即只有vip当前所在机器发生故障,另一台机器才能接管vip。即使优先级高的那一台机器恢复 后也不会主动抢回vip,只能等到对方发生故障,才会将vip切回来。
3.脚本参考(网上找的,仅供参考)
mysql_check.sh
# 脚本主从服务器上面都有,只是从服务器上面的master.sh有些不一样。添加了当slave提升为主库时,发送邮件通知。
[root@mysql-server-01 sh]# cat mysql_check.sh
#!/bin/bash
. /root/.bash_profile
count=1
while true
do
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14520.sock -e "show status;" > /dev/null 2>&1
i=$?
ps aux | grep mysqld | grep -v grep > /dev/null 2>&1
j=$?
if [ $i = 0 ] && [ $j = 0 ]
then
exit 0
else
if [ $i = 1 ] && [ $j = 0 ]
then
exit 0
else
if [ $count -gt 5 ]
then
break
fi
let count++
continue
fi
fi
done
/etc/init.d/keepalived stop
复制
slave上的master.sh
[root@mysql-server-02 sh]# cat master.sh
#!/bin/bash
. /root/.bash_profile
Master_Log_File=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave status\G" | grep -w Master_Log_File | awk -F": " '{print $2}')
Relay_Master_Log_File=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave status\G" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}')
Read_Master_Log_Pos=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave status\G" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}')
Exec_Master_Log_Pos=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave status\G" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}')
i=1
while true
do
if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ]
then
echo "ok"
break
else
sleep 1
if [ $i -gt 60 ]
then
break
fi
continue
let i++
fi
done
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "stop slave;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_support_xa=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global sync_binlog=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_flush_log_at_trx_commit=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '123456';flush privileges;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show master status;" > /tmp/master_status_$(date "+%y%m%d-%H%M").txt
#当slave提升为主以后,发送邮件
echo "#####################################" > /tmp/status
echo "salve已经提升为主库,请进行检查!" >> /tmp/status
ifconfig | sed -n '/inet /{s/.*addr://;s/ .*//;p}' | grep -v 127.0.0.1 >> /tmp/status
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -Nse "show variables like 'port'" >> /tmp/status
echo "#####################################" >> /tmp/status
master=`cat /tmp/status`
echo "$master" | mutt -s "slave to primary!!!" 13143753516@139.com
复制
脚本中检查复制是否延时的思想
1、首先看 Relay_Master_Log_File 和 Master_Log_File 是否有差异
2、如果Relay_Master_Log_File 和 Master_Log_File 有差异的话,那说明延迟很大了
3、如果Relay_Master_Log_File 和 Master_Log_File 没有差异,再来看Exec_Master_Log_Pos 和 Read_Master_Log_Pos 的差异
而不是通过Seconds_Behind_Master去判断,该值表示slave上SQL线程和IO线程之间的延迟,实际上还要考虑到 Master_Log_File 和 Relay_Master_Log_File 是否有差距,更严谨的则是要同时在master上执行show master status进行对比。这也是MHA在切换过程中可以做到的。MMM的切换也只是在从库上执行了show slave status。所以数据一致性要求还是MHA给力。扯远了。^_^
backup.sh
# backup.sh脚本的作用是状态改变为backup以后执行的脚本。
[root@mysql-server-02 sh]# cat backup.sh
#!/bin/bash
. /root/.bash_profile
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '123456';flush privileges;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global event_scheduler=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_support_xa=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global sync_binlog=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_flush_log_at_trx_commit=0;"
复制
stop.sh
# stop.sh 表示keepalived停止以后需要执行的脚本。更改密码,设置参数,检查是否还有写入操作,最后无论是否执行完毕,都退出。
[root@mysql-server-02 sh]# cat stop.sh
#!/bin/bash
. /root/.bash_profile
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '1q2w3e4r';flush privileges;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_support_xa=1;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global sync_binlog=1;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_flush_log_at_trx_commit=1;"
M_File1=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position1=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show master status\G" | awk -F': ' '/Position/{print $2}')
sleep 1
M_File2=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position2=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show master status\G" | awk -F': ' '/Position/{print $2}')
i=1
while true
do
if [ $M_File1 = $M_File1 ] && [ $M_Position1 -eq $M_Position2 ]
then
echo "ok"
break
else
sleep 1
if [ $i -gt 60 ]
then
break
fi
continue
let i++
fi
done
复制
3.Keepalived 安装
环境
OS:Linux 7
master:192.168.65.12
backup:192.168.65.13
VIP:192.168.65.111
下载:https://www.keepalived.org/download.html
复制
安装
# 安装必要安装包(如果 config 的时候没提示则可以不用安装)
yum install -y openssl-devel libnl*
# keepalived 解压
tar zxvf keepalived-2.0.20.tar.gz
# cd 进目录
cd keepalived-2.0.20
# config
./configure --prefix=/usr/local/keepalived/
# make & make install
make && make install
# keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
# 将keepalived主程序加入到环境变量(安装目录下)
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
# keepalived启动脚本(源码目录下),放到/etc/init.d/目录下就可以使用service命令便捷调用
cp /root/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
# 将配置文件放到默认路径下
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
# master 配置文件
复制
master 配置文件
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
lzx@test.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.65.111
}
}
virtual_server 192.168.65.111 3306 {
delay_loop 2
#lb_algo rr
#lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.65.12 3306 {
weight 3
notify_down /tmp/mysql.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
复制
slave 配置文件
! Configuration File for keepalived
global_defs {
notification_email {
lzx@test.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.65.111
}
}
virtual_server 192.168.65.111 3306 {
delay_loop 2
#lb_algo rr
#lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.65.13 3306 {
weight 3
notify_down /tmp/mysql.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
复制
/tmp/mysql.sh 内容
#!/bin/bash
pkill keepalived
# 配置文件是监控的 3306 端口,如果 3306 端口不同,则表示 MySQL 不可访问,故 kill keepalived 服务,让 VIP 自动切换到另一台服务器。
# 这里的判断逻辑很粗暴,可能会有数据丢失的问题,因为如果在主从本来就有复制延迟的情况下,这个时候 VIP 切过去会丢失数据,所以最好是在切过去之后,判断下 slave 是否有延迟,如果有则等待应用完剩下的 relay_log,然后开启读写模式和其他设置等
启动 Keepalived
systemctl start keepalived
systemctl stop keepalived
Keepalived 状态检查
# 检查 Keepalived 服务器状态
[root@ty12 keepalived-2.0.20]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2021-06-02 10:50:55 CST; 7min ago
Process: 16532 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 16533 (keepalived)
CGroup: /system.slice/keepalived.service
├─16533 /usr/local/keepalived/sbin/keepalived -D
├─16534 /usr/local/keepalived/sbin/keepalived -D
└─16535 /usr/local/keepalived/sbin/keepalived -D
Jun 02 10:51:58 ty12 Keepalived_vrrp[16535]: Sending gratuitous ARP on ens33 for 192.168.65.111
Jun 02 10:51:58 ty12 Keepalived_vrrp[16535]: Sending gratuitous ARP on ens33 for 192.168.65.111
Jun 02 10:51:58 ty12 Keepalived_vrrp[16535]: Sending gratuitous ARP on ens33 for 192.168.65.111
Jun 02 10:51:58 ty12 Keepalived_vrrp[16535]: Sending gratuitous ARP on ens33 for 192.168.65.111
Jun 02 10:52:03 ty12 Keepalived_vrrp[16535]: Sending gratuitous ARP on ens33 for 192.168.65.111
Jun 02 10:52:03 ty12 Keepalived_vrrp[16535]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.65.111
Jun 02 10:52:03 ty12 Keepalived_vrrp[16535]: Sending gratuitous ARP on ens33 for 192.168.65.111
Jun 02 10:52:03 ty12 Keepalived_vrrp[16535]: Sending gratuitous ARP on ens33 for 192.168.65.111
Jun 02 10:52:03 ty12 Keepalived_vrrp[16535]: Sending gratuitous ARP on ens33 for 192.168.65.111
Jun 02 10:52:03 ty12 Keepalived_vrrp[16535]: Sending gratuitous ARP on ens33 for 192.168.65.111
# 检查进程
[root@ty13 keepalived-2.0.20]# ps -ef|grep kee
root 15457 1 0 11:02 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D
root 15458 15457 0 11:02 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D
root 15459 15457 0 11:02 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D
# 检查 VIP
[root@ty12 keepalived-2.0.20]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d2:d6:11 brd ff:ff:ff:ff:ff:ff
inet 192.168.65.12/24 brd 192.168.65.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.65.111/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::4bf2:931f:4b6e:f889/64 scope link
valid_lft forever preferred_lft forever
# 这里服务器状态运行正常,VIP 也出现了,说明 Keepalived 运行正常
复制
4.Keepalived + MySQL 测试
这里 MySQL 是用的主从架构
查看当前 VIP 所在机器
[root@ty12 keepalived-2.0.20]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d2:d6:11 brd ff:ff:ff:ff:ff:ff
inet 192.168.65.12/24 brd 192.168.65.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.65.111/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::4bf2:931f:4b6e:f889/64 scope link
valid_lft forever preferred_lft forever
复制
通过 VIP 连接数据库,insert 测试数据,并查看 slave 同步状态
[root@ty12 keepalived-2.0.20]# mysql -h192.168.65.111 -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 682
Server version: 5.7.26-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database ty;
Query OK, 1 row affected (0.01 sec)
mysql> use ty;
Database changed
mysql> create table test1(id int);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into test1 values(1),(2),(3);
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from test1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
# slave 同步状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.65.12
Master_User: rpl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: bin.000004
Read_Master_Log_Pos: 1540
Relay_Log_File: relay.000004
Relay_Log_Pos: 1741
Relay_Master_Log_File: bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1540
Relay_Log_Space: 2179
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1921686512
Master_UUID: 1968d31e-9777-11eb-a69f-000c29d2d611
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 1968d31e-9777-11eb-a69f-000c29d2d611:1-8
Executed_Gtid_Set: 1968d31e-9777-11eb-a69f-000c29d2d611:1-8,
22fa7eb0-9777-11eb-a555-000c29b4829a:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.01 sec)
mysql> select * from ty.test1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.01 sec)
复制
master 停止 MySQL 服务,检查 Keepalived 、VIP 状态
# 停止 master 上 MySQL 服务
[root@ty12 keepalived-2.0.20]# mysqladmin shutdown -uroot -p123
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
2021-06-02T03:16:33.014021Z mysqld_safe mysqld from pid file /data/3306/data/ty12.pid ended
[1]+ Done mysqld_safe --defaults-file=/data/3306/my.cnf
[root@ty12 keepalived-2.0.20]# ps -ef|grep mysql
root 16586 3482 0 11:16 pts/2 00:00:00 grep --color=auto mysql
# 查看 Keepalived 服务、进程状态
[root@ty12 keepalived-2.0.20]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Jun 02 11:16:26 ty12 Keepalived_healthcheckers[16534]: TCP_CHECK on service [192.168.65.12]:tcp:3306 failed aft...ies.
Jun 02 11:16:26 ty12 Keepalived_healthcheckers[16534]: Removing service [192.168.65.12]:tcp:3306 to VS [192.168...3306
Jun 02 11:16:26 ty12 Keepalived_healthcheckers[16534]: Lost quorum 1-0=1 > 0 for VS [192.168.65.111]:tcp:3306
Jun 02 11:16:26 ty12 Keepalived_healthcheckers[16534]: Remote SMTP server [127.0.0.1]:25 connected.
Jun 02 11:16:26 ty12 Keepalived[16533]: Stopping
Jun 02 11:16:26 ty12 Keepalived_healthcheckers[16534]: Shutting down service [192.168.65.12]:tcp:3306 from VS [...3306
Jun 02 11:16:26 ty12 Keepalived_vrrp[16535]: (VI_1) sent 0 priority
Jun 02 11:16:26 ty12 Keepalived_vrrp[16535]: (VI_1) removing VIPs.
Jun 02 11:16:27 ty12 Keepalived_vrrp[16535]: Stopped - used 0.001400 user time, 0.247919 system time
Jun 02 11:16:27 ty12 Keepalived[16533]: Stopped Keepalived v2.0.20 (01/22,2020)
Hint: Some lines were ellipsized, use -l to show in full.
[root@ty12 keepalived-2.0.20]# ps -ef|grep kee
root 16588 3482 0 11:16 pts/2 00:00:00 grep --color=auto kee
# 可以看到 MySQL 、Keepalived 服务均已停止,因为 Keepalived 配置文件中设置了如果 检测不到 MySQL 的 3306 端口,则停止 Keepalived。
# 这个时候 VIP 已经飘逸到 slave 上了,可以通过 /var/log/messages 日志查看
[root@ty13 keepalived-2.0.20]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:b4:82:9a brd ff:ff:ff:ff:ff:ff
inet 192.168.65.13/24 brd 192.168.65.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.65.111/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::f150:77f3:561b:2fdf/64 scope link
valid_lft forever preferred_lft forever
复制
通过 VIP 连接数据库测试
[root@ty12 keepalived-2.0.20]# mysql -h192.168.65.111 -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 489
Server version: 5.7.26-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select * from ty.test1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
mysql> insert into ty.test1 values(4);
Query OK, 1 row affected (0.00 sec)
mysql> select * from ty.test1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.01 sec)
# 可以看到当 master 宕机后,通过 VIP 还是可以连到数据库,并 insert 数据
复制
评论



