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 MASTER
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.136.100
}
}
从的配置
[root@node2 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id Nginx2
}
vrrp_instance Nginx {
state BACKUP
interface ens160
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_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 MASTER
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_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 keepalived
global_defs {
router_id Nginx2
}
vrrp_script chk_ngx{
script "killall -0 nginx"
interval 1
}
vrrp_instance Nginx {
state BACKUP
interface ens160
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_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 ]; then
systemctl restart nginx &> /dev/null
sleep 1
systemctl status nginx | grep "active (running)" > /dev/null
if [ $? -ne 0 ]; then
systemctl stop keepalived
else
exit
fi
fi
[root@openEuler-node1 ~]# chmod +x /etc/keepalived/chk_ngx.sh
检验1 将node1上nginx stop了
检验2 将node1上 nginx 配置文件修改
变成backup了
配置非抢占 模式
1、主备模式都要配置成BACKUP
2、主配置非抢占:nopreempt
文章来源:https://www.toymoban.com/news/detail-860143.html
即使主重新修复好 也不会变成它文章来源地址https://www.toymoban.com/news/detail-860143.html
到了这里,关于keepalived检测Nginx高可用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!