keepalive的HA分为抢占模式和非抢占模式
抢占模式指:在master挂机恢复后会把VIP从backup节点中抢占过来,再次升级为master。
非抢占模式指:在master挂机恢复后不会抢占backup再次升级master。
配置
基本环境:nginx01Master:192.168.80.137
nginx02Backup:192.168.80.132
VIP :192.168.80.100
关闭iptables,firewall,selinux
基本操作:
时间校准:update ntp.api.bz
抢占模式配置
#####master(192.168.80.137)keepalived.conf配置如下>#######
[root@liusq-2 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 4
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.80.100
}
track_script {
chk_nginx
}
}
#####master(192.168.80.132)keepalived.conf配置如下>#######
[root@liusq-1 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 4
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.80.100
}
track_script {
chk_nginx
}
}
##################nginx_check.sh#############
[root@liusq-2 keepalived]# cat nginx_check.sh
#!/bin/sh
# check nginx server status
NGINX=/usr/local/nginx/sbin/nginx
PORT=80
netstat -antp|grep nginx
if [ $? -ne 0 ];then
$NGINX -s stop
$NGINX
sleep 3
netstat -antp|grep nginx
[ $? -ne 0 ] && service keepalived stop
fi
非抢占模式
#####master(192.168.80.10)配置如下>#######
global_defs {
router_id nginx_01 #标识本节点的名称,通常为hostname
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface enp0s3
virtual_router_id 51
mcast_src_ip 192.168.1.201
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.210
}
track_script {
chk_nginx # nginx存活状态检测脚本
}
}
#####backup(192.168.80.20)配置如下>#######
global_defs {
router_id nginx_02
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface enp0s3
virtual_router_id 51
mcast_src_ip 192.168.1.202
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.210
}
track_script {
chk_nginx
}
}
和非抢占模式的配置相比,只改了两个地方:
1> 在vrrp_instance块下两个节点各增加了nopreempt指令,表示不争抢vip
2> 节点的state都为BACKUP
两个keepalived节点都启动后,默认都是BACKUP状态,双方在发送组播信息后,会根据优先级来选举一个MASTER出来。
由于两者都配置了nopreempt,所以MASTER从故障中恢复后,不会抢占vip。这样会避免VIP切换可能造成的服务延迟。
有兴趣学习交流IT行业技术的兄弟们,请加群89331935,加的时候请备注:CSDN博客看到的
我微信号:Agoni399 加的时候请备注:CSDN博客看到的