1.10 高可用服务只是针对物理服务器
高可用服务自身往往只能解决针对物理服务器宕机切换,但是如果服务器不宕机,而nginx负载服务宕了。此时VIP是不漂移的,因此,高可用切换就会出现问题,那如何解决呢?
功能:keepalived判断nginx负载服务是否异常,如果nginx服务异常,则关闭Nginx服务,实现服务器的切换。
1)分别在lb01,lb02上配置如下脚本
[root@lb01 scripts]# cat chk_nginx_proxy.sh
# !/bin/sh
# 如果nginx服务停止,则关闭keep.
if [ `netstat -lntup|grep nginx|wc -l` -ne 1 ];then
systemctl stop keepalived
fi
[root@lb01 scripts]# chmod +x chk_nginx_proxy.sh
[root@lb01 scripts]# /server/scripts/chk_nginx_proxy.sh ##要可以执行
测试杀掉nginx后,会不会停止keep服务:
[root@lb01 scripts]# pkill nginx
[root@lb01 scripts]# sh chk_nginx_proxy.sh
[root@lb01 scripts]# ps -ef|egrep "nginx|keep"
root 1769 1197 0 18:35 pts/0 00:00:00 grep -E --color=auto nginx|keep
2)分别在lb01,lb02上配置keepalived.conf
lb01上配置keepalived.conf
[root@lb01 scripts]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb01
}
vrrp_script chk_nginx_proxy {#<==定义vrrp脚本,检测HTTP端口。
script "/server/scripts/chk_nginx_proxy.sh"#<==执行脚本,当nginx服务有问题,就停掉keepalived服务。
interval 2 #<==间隔2秒。
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 53
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:3
}
track_script {
chk_nginx_proxy #<==触发检查。
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:4
}
}
lb02上配置keepalived.conf
[root@lb02 scripts]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}
vrrp_script chk_nginx_proxy {#<==定义vrrp脚本,检测HTTP端口。
script "/server/scripts/chk_nginx_proxy.sh"#<==执行脚本,当nginx服务有问题,就停掉keepalived服务。
interval 2 #<==间隔2秒。
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 53
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:3
}
track_script {
chk_nginx_proxy #<==触发检查。
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:4
}
}
测试过程笔记略(见视频讲解)
本文链接:https://www.yunweibase.com/archives/189
网友评论comments