本文章配套视频 | https://www.ixigua.com/7079764967548781086?id=7082376934071468580 |
本专栏全部文章 | https://blog.csdn.net/tonghu_note/category_11716546.html |
总目录 | https://blog.csdn.net/tonghu_note/article/details/124333034 |
来我的dou音 aa10246666, 看配套视频
主节点 node1 mysql 8.0.28 | 10.211.55.9 |
从节点 node2 mysql 8.0.28 | 10.211.55.4 |
Keepalived 2.1.5 | 10.211.55.100(VIP) |
本节课程前提条件是node1(master)和node2(slave)的主从架构已经搭建完毕,如果你不知道如何搭建主从,那么可以参考这篇文章 《童虎学习笔记》5分钟搭建MySQL主从复制_童虎学习笔记的博客-CSDN博客童虎学习笔记的博客_CSDN博客-领域博主https://blog.csdn.net/tonghu_note来我的西瓜视频,看免费配套视频https://www.ixigua.com/home/2058760810138187来我的dou音 aa10246666, 看配套视频一、实战环境主节点 node1 mysql 810.211.55.9从节点 node2 mysql 810.211.55.4二、搭建主从1、主库新建主从同步帐号repl...https://blog.csdn.net/tonghu_note/article/details/123808841
主从上都要有这个脚本,用于判断mysql进程是否存在,脚本内容如下
root@node1:/usr/local/scripts# cat chk_mysql_alived.sh
#!bin/bash
ss -tnl|grep 3306 >/dev/null 2>&1
if [ $? -eq 0 ]
then
echo " mysql is alived "
exit 0
else
echo " mysql is dead "
systemctl stop keepalived
exit 2
fi
授权执行权限
chmod u+x /usr/local/scripts/chk_mysql_alived.sh
执行一下脚本试试效果
root@node1:/usr/local/scripts# sh chk_mysql_alived.sh
mysql is alived
root@node1:/usr/local/scripts# echo $?
0
apt-get install -y ipset
apt-get install -y keepalived
如果你用的是centos,可以用yum安装
root@node1:~# cat /etc/keepalived/keepalived.conf
vrrp_script chk_mysql_alived {
script "/usr/local/scripts/chk_mysql_alived.sh"
interval 2
weight 2
}global_defs {
router_id LVS_DEVEL_1
}vrrp_instance VI_1 {
state BACKUP
interface enp0s5
track_interface {
enp0s5
}
virtual_router_id 58
priority 200
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql_alived
}
virtual_ipaddress {
10.211.55.100
}
}
以下是对Keepalived配置文件的说明
vrrp_script chk_mysql_alived { #检测pg服务是否在运行
script "/usr/local/scripts/chk_mysql_alived.sh"
interval 2 #脚本执行间隔,每2s检测一次
weight 2
}global_defs {
router_id LVS_DEVEL_1
}vrrp_instance VI_1 {
state BACKUP #这里所有节点都定义为BACKUP
interface enp0s5 #指定虚拟ip的网卡接口
track_interface {
enp0s5
}
virtual_router_id 58 #路由器标识,主从必须是一致的
priority 200 #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。
advert_int 1
nopreempt #不抢占模式,在优先级高的机器上设置即可,优先级低的机器可不设置
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql_alived
}
virtual_ipaddress {
10.211.55.100 #虚拟IP
}
}
启动Keekpalived服务
systemctl start keepalived.service
查看Keepalived服务状态 systemctl status keepalived.service
root@node1:/usr/local/scripts# systemctl status keepalived.service
● keepalived.service - Keepalive Daemon (LVS and VRRP)
Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2022-04-03 05:17:09 EDT; 5min ago
Main PID: 63356 (keepalived)
Tasks: 2 (limit: 2263)
Memory: 1.6M
CPU: 87ms
CGroup: /system.slice/keepalived.service
├─63356 /usr/sbin/keepalived --dont-fork
└─63357 /usr/sbin/keepalived --dont-forkApr 03 05:17:09 node1 Keepalived_vrrp[63357]: Registering Kernel netlink command channel
Apr 03 05:17:09 node1 Keepalived_vrrp[63357]: Opening file '/etc/keepalived/keepalived.conf'.
Apr 03 05:17:09 node1 Keepalived_vrrp[63357]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
Apr 03 05:17:09 node1 Keepalived_vrrp[63357]: (/etc/keepalived/keepalived.conf: Line 26) (VI_1) track script chk_pg_alived not found, ignoring...
Apr 03 05:17:09 node1 Keepalived_vrrp[63357]: WARNING - script '/usr/local/scripts/chk_mysql_alived.sh' is not executable for uid:gid 0:0 - disabling.
Apr 03 05:17:09 node1 Keepalived_vrrp[63357]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
Apr 03 05:17:09 node1 Keepalived_vrrp[63357]: (VI_1) Ignoring track_interface enp0s5 since own interface
Apr 03 05:17:09 node1 Keepalived_vrrp[63357]: Registering gratuitous ARP shared channel
Apr 03 05:17:09 node1 Keepalived_vrrp[63357]: (VI_1) Entering BACKUP STATE (init)
Apr 03 05:17:12 node1 Keepalived_vrrp[63357]: (VI_1) Entering MASTER STATE
root@node1:/usr/local/scripts#
查看虚ip是否启用 ip a
root@node1:/usr/local/scripts# ip a
1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: enp0s5:mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:1c:42:8b:90:29 brd ff:ff:ff:ff:ff:ff
inet 10.211.55.9/24 brd 10.211.55.255 scope global dynamic enp0s5
valid_lft 1424sec preferred_lft 1424sec
inet 10.211.55.100/32 scope global enp0s5
valid_lft forever preferred_lft forever
inet6 fdb2:2c26:f4e4:0:21c:42ff:fe8b:9029/64 scope global dynamic mngtmpaddr
valid_lft 2591556sec preferred_lft 604356sec
inet6 fe80::21c:42ff:fe8b:9029/64 scope link
valid_lft forever preferred_lft forever
root@node2:/usr/local/scripts# cat /etc/keepalived/keepalived.conf
vrrp_script chk_mysql_alived {
script "/usr/local/scripts/chk_mysql_alived.sh"
interval 2
weight 2
}global_defs {
router_id LVS_DEVEL_1
}vrrp_instance VI_1 {
state BACKUP
interface enp0s5
track_interface {
enp0s5
}
virtual_router_id 58
priority 160
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql_alived
}
virtual_ipaddress {
10.211.55.100
}
}
启动Keekpalived服务
systemctl start keepalived.service
查看虚ip是否启用 ip a,从库上虚ip不会启用
主库停mysql服务
mysqladmin -uroot -proot shutdown
确认主库虚ip已经消失 ip a
root@node1:/usr/local/scripts# ip a
1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: enp0s5:mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:1c:42:8b:90:29 brd ff:ff:ff:ff:ff:ff
inet 10.211.55.9/24 brd 10.211.55.255 scope global dynamic enp0s5
valid_lft 994sec preferred_lft 994sec
inet6 fdb2:2c26:f4e4:0:21c:42ff:fe8b:9029/64 scope global dynamic mngtmpaddr
valid_lft 2591807sec preferred_lft 604607sec
inet6 fe80::21c:42ff:fe8b:9029/64 scope link
valid_lft forever preferred_lft forever
确认主库Keepalived已经停止 systemctl status keepalived.service
root@node1:/usr/local/scripts# systemctl status keepalived.service
● keepalived.service - Keepalive Daemon (LVS and VRRP)
Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sun 2022-04-03 05:59:18 EDT; 12s ago
Process: 65224 ExecStart=/usr/sbin/keepalived --dont-fork $DAEMON_ARGS (code=exited, status=0/SUCCESS)
Main PID: 65224 (code=exited, status=0/SUCCESS)
CPU: 295msApr 03 05:58:24 node1 Keepalived_vrrp[65225]: VRRP_Script(chk_mysql_alived) succeeded
Apr 03 05:58:24 node1 Keepalived_vrrp[65225]: (VI_1) Changing effective priority from 200 to 202
Apr 03 05:58:28 node1 Keepalived_vrrp[65225]: (VI_1) Entering MASTER STATE
Apr 03 05:59:17 node1 systemd[1]: Stopping Keepalive Daemon (LVS and VRRP)...
Apr 03 05:59:17 node1 Keepalived[65224]: Stopping
Apr 03 05:59:17 node1 Keepalived_vrrp[65225]: (VI_1) sent 0 priority
Apr 03 05:59:18 node1 Keepalived_vrrp[65225]: Stopped
Apr 03 05:59:18 node1 Keepalived[65224]: Stopped Keepalived v2.1.5 (07/13,2020)
Apr 03 05:59:18 node1 systemd[1]: keepalived.service: Succeeded.
Apr 03 05:59:18 node1 systemd[1]: Stopped Keepalive Daemon (LVS and VRRP).
root@node1:/usr/local/scripts#
root@node2:/usr/local/scripts# ip a
1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: enp0s5:mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:1c:42:47:2f:1d brd ff:ff:ff:ff:ff:ff
inet 10.211.55.4/24 brd 10.211.55.255 scope global dynamic enp0s5
valid_lft 1386sec preferred_lft 1386sec
inet 10.211.55.100/32 scope global enp0s5
valid_lft forever preferred_lft forever
inet6 fdb2:2c26:f4e4:0:21c:42ff:fe47:2f1d/64 scope global dynamic mngtmpaddr
valid_lft 2591966sec preferred_lft 604766sec
inet6 fe80::21c:42ff:fe47:2f1d/64 scope link
valid_lft forever preferred_lft forever
root@node2:/usr/local/scripts#
上述实验中,node1已经坏掉,node2现在是主节点,那么问题来了,如何以node2为主节点,重新搭一台从库,并在这台从库上启动Keepalived,以保证当node2坏掉的时候虚ip可以成功切换到新搭建的从库上呢?