node1 192.168.136.55
node2 192.168.136.56
两台机器都安装dnf install keepalived nginx
[root@node1 ~]# echo "web test page, ip is `hostname -I`." > /usr/share/nginx/html/index.html
[root@node2 ~]# echo "web test page, ip is `hostname -I`." > /usr/share/nginx/html/index.html
主的配置
[root@node1 ~]# vim /etc/keepalived/keepalived.conf
global_defs {router_id Nginx1
}vrrp_instance Nginx {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.136.100}
}
从的配置
[root@node2 ~]# vim /etc/keepalived/keepalived.conf
global_defs {router_id Nginx2
}vrrp_instance Nginx {state BACKUPinterface ens160virtual_router_id 51priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.136.100}
}
[root@node1 ~]# systemctl start keepalived.service
[root@node2 ~]# systemctl start keepalived.service
测试 访问虚拟IP 访问的是master
当master的keepalived停掉
访问的是backup
健康检查Nginx的状态
目前 Keepalived 仅能通过主机是否宕机来进行业务切换,如果仅 Nginx 业务出现故障是无法 切换的,需在 Keepalive 中添加健康检查来达到检测 Nginx 是否可用的目的。
[root@openEuler-node1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {router_id Nginx1
}vrrp_script chk_ngx{script "killall -0 nginx"interval 1
}vrrp_instance Nginx {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {chk_ngx}virtual_ipaddress {192.168.136.100}
}
node2上修改类似 killall -0 nginx 一刀切 直接切换
[root@openEuler-node2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id Nginx2
}vrrp_script chk_ngx{script "killall -0 nginx"interval 1
}vrrp_instance Nginx {state BACKUPinterface ens160virtual_router_id 51priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {chk_ngx}virtual_ipaddress {192.168.136.100}
}
当MATSER的nginx停掉之后
当检测到nginx异常,然后停止keepalive,VIP漂移
1、检测到web服务异常
2、重启web
3、启动不了,再VIP漂移
更改配置文件
node1 和Node2一样
vim /etc/keepalived/chk_ngx.sh
#!/bin.bash
systemctl status nginx | grep "active (running)" > /dev/null
if [ $? -ne 0 ]; thensystemctl restart nginx &> /dev/nullsleep 1systemctl status nginx | grep "active (running)" > /dev/nullif [ $? -ne 0 ]; thensystemctl stop keepalivedelseexitfi
fi
[root@openEuler-node1 ~]# chmod +x /etc/keepalived/chk_ngx.sh
检验1 将node1上nginx stop了
检验2 将node1上 nginx 配置文件修改
变成backup了
配置非抢占 模式
1、主备模式都要配置成BACKUP
2、主配置非抢占:nopreempt
即使主重新修复好 也不会变成它