keepalived介绍
Keepalived 是一个用于做双机热备(HA)的软件,它的主要功能是实现真实机的故障隔离及负载均衡器间的失败切换,提高系统的可用性。一个LVS服务会有2台服务器运行Keepalived,一台为主服务器(MASTER),一台为备份服务器(BACKUP),但是对外表现为一个虚拟IP,主服务器会发送特定的消息给备份服务器,当备份服务器收不到这个消息的时候,即主服务器宕机的时候, 备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性。
两台机器分别安装keepalived
yum install keepalived -y
配置Keepalived
配置主服务器 keepalved.conf
cat /etc/keepalived/keepalived.conf
global_defs {router_id nginx01
}
vrrp_script chk_nginx_proxy { #<==定义vrrp脚本,检测HTTP端口。script "/etc/keepalived/check_nginx.sh" #<==执行脚本,当nginx服务有问题,就停掉keepalived服务。interval 2 #<==间隔2秒。weight 2
}
vrrp_instance VI_1 {state MASTERinterface ens192virtual_router_id 51priority 150advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {100.100.137.250}track_script { #触发检查chk_nginx_proxy}
}
配置备服务器 keepalved.conf
cat /etc/keepalived/keepalived.conf
global_defs {router_id nginx02
}
vrrp_script chk_nginx_proxy { #<==定义vrrp脚本,检测HTTP端口。script "/etc/keepalived/check_nginx.sh" #<==执行脚本,当nginx服务有问题,就停掉keepalived服务。interval 2 #<==间隔2秒。weight 2
}vrrp_instance VI_1 {state BACKUPinterface ens192virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {100.100.137.250}track_script { #触发检查chk_nginx_proxy}
}
配置nginx脚本
分别在nginx01,nginx02上配置如下脚本
cat /etc/keepalived/check_nginx.sh
#!/bin/basha=$(systemctl is-active nginx)
if [ $a != active ]
thensystemctl stop keepalived
fichmod 755 /etc/keepalived/check_nginx.sh
启动
# 两台都是先启动nginx
systemctl start nginx
systemctl enable nginx
systemctl status nginx# 之后再分别启动keepalived
systemctl start keepalived
systemctl enable keepalived
systemctl status keepalived
这时已经配置好了。测试是否高可用即可。