环境:
两台NGINX,使用keepalived实现故障漂移
MASTER机器脚本内容:
#/bin/bash
#auto install keepalived
#2020年7月31日 12:05:54
#by author toyix
##########################
ETH=ens33
yum install -y keepalived*
cd /etc/keepalived/
cp keepalived.conf{,.bak}
sed -i '1,$d' keepalived.conf
cat>keepalived.conf <<-EOF
! Configuration File for Keepalived
global_defs {
notification_email {
631646620@qq.com
}
notification_email_from keepalived@yjy.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/data/sh/check_nginx.sh"
interval 2
}
#VIP1
vrrp_instance VI_1 {
state BACKUP
interface ${ETH}
virtual_router_id 151
priority 100
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.31
}
track_script {
chk_nginx
}
}
EOF
mkdir -p /data/sh
cat>/data/sh/check_nginx.sh <<-EOF
#!/bin/bash
#2020年7月29日 21:17:29
#auto check nginx service
###############################
CHECK_NUM=\`netstat -tnlp|grep -w "nginx: master"|grep 80|wc -l\`
if [ \${CHECK_NUM} -eq 0 ];then
systemctl stop keepalived.service
pkill keepalived
fi
EOF
chmod +x /data/sh/check_nginx.sh
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ${ETH} --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
systemctl start keepalived.service
BACKUP机器脚本内容:
#/bin/bash
#auto install keepalived
#2020年7月31日 12:05:54
#by author toyix
##########################
ETH=ens33
yum install -y keepalived*
cd /etc/keepalived/
cp keepalived.conf{,.bak}
sed -i '1,$d' keepalived.conf
cat>keepalived.conf <<-EOF
! Configuration File for Keepalived
global_defs {
notification_email {
631646620@qq.com
}
notification_email_from keepalived@yjy.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/data/sh/check_nginx.sh"
interval 2
}
#VIP1
vrrp_instance VI_1 {
state BACKUP
interface ${ETH}
virtual_router_id 151
priority 90
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.31
}
track_script {
chk_nginx
}
}
EOF
mkdir -p /data/sh
cat>/data/sh/check_nginx.sh <<-EOF
#!/bin/bash
#2020年7月29日 21:17:29
#auto check nginx service
###############################
CHECK_NUM=\`netstat -tnlp|grep -w "nginx: master"|grep 80|wc -l\`
if [ \${CHECK_NUM} -eq 0 ];then
systemctl stop keepalived.service
pkill keepalived
fi
EOF
chmod +x /data/sh/check_nginx.sh
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ${ETH} --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
systemctl start keepalived.service
执行过程
[root@localhost src]# sh autoinstallkeepalived.sh
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package keepalived.x86_64 0:1.3.5-16.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================================================
Installing:
keepalived x86_64 1.3.5-16.el7 base 331 k
Transaction Summary
=======================================================================================================================================================================
Install 1 Package
Total download size: 331 k
Installed size: 1.0 M
Downloading packages:
keepalived-1.3.5-16.el7.x86_64.rpm | 331 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : keepalived-1.3.5-16.el7.x86_64 1/1
Verifying : keepalived-1.3.5-16.el7.x86_64 1/1
Installed:
keepalived.x86_64 0:1.3.5-16.el7
Complete!
Warning: ALREADY_ENABLED: rule '['--in-interface', 'ens33', '--destination', '224.0.0.18', '--protocol', 'vrrp', '-j', 'ACCEPT']' already is in 'ipv4:filter:INPUT'
success
success
[root@localhost src]#
}
[root@localhost src]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:30:51:a0 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.21/24 brd 192.168.1.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.1.31/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::22b6:b244:5f25:faf4/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost src]#
当关闭nginx时,发生MASTER漂移
[root@localhost src]# pkill nginx
Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.1.31
Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:50:02 localhost systemd: Stopped LVS and VRRP High Availability Monitor.
Jul 31 14:50:02 localhost Keepalived[17220]: Stopping
Jul 31 14:50:02 localhost Keepalived_healthcheckers[17221]: Stopped
Jul 31 14:50:02 localhost Keepalived_vrrp[17222]: VRRP_Instance(VI_1) sent 0 priority
Jul 31 14:50:02 localhost Keepalived_vrrp[17222]: VRRP_Instance(VI_1) removing protocol VIPs.
Jul 31 14:50:02 localhost Keepalived_vrrp[17222]: Stopped
Jul 31 14:50:02 localhost Keepalived[17220]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Jul 31 14:47:54 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) removing protocol VIPs.
Jul 31 14:47:54 localhost Keepalived_vrrp[13746]: Using LinkWatch kernel netlink reflector...
Jul 31 14:47:54 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jul 31 14:47:54 localhost Keepalived_vrrp[13746]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Jul 31 14:47:54 localhost Keepalived_vrrp[13746]: VRRP_Script(chk_nginx) succeeded
Jul 31 14:50:02 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) Entering MASTER STATE
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.1.31
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: Sending gratuitous ARP on ens33 for 192.168.1.31
----------------------end