Keepalived 双机热备原理
双机主备的缺点:如果在主节点比较稳定,一直没有问题的话,这时备用节点的服务器一直不使用,这样会造成成本开销加大。 因此就要采用双机热备的策略,将备用节点利用起来,在没有问题时,备用节点用来做其他的事情。
原理图
关于DNS轮训,可以在腾讯云或者阿里云控制台配置。
原理
当主节点没有问题时,两个节点各自维护自己的服务请求,当其中一个节点发生故障时,发生故障的节点虚拟IP会与另一个没有故障的 Nginx 绑定,将请求发送到没有故障的节点,当故障恢复后,在自动绑定回原来节点的Nginx。这里就不再区分主节点和备用节点,两个节点互为主备。
实现
规则:以一个虚拟IP分组归为同一个路由
主节点配置:
global_defs {
router_id keep_171
}
vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx_alive_or_not.sh"
! 每隔两秒运行上一行脚本
interval 2
! 如果脚本运行成功,则升级权重+10
weight 10
! 如果脚本运行失败,则降级权重-10
# weight -10
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx_alive # 追踪 nginx 脚本
}
virtual_ipaddress {
192.168.1.161
}
}
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 52
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.162
}
}
备用节点配置:
global_defs {
router_id keep_171
}
vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx_alive_or_not.sh"
! 每隔两秒运行上一行脚本
interval 2
! 如果脚本运行成功,则升级权重+10
weight 10
! 如果脚本运行失败,则降级权重-10
# weight -10
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.161
}
}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx_alive # 追踪 nginx 脚本
}
virtual_ipaddress {
192.168.1.162
}
}