安装keepalived
yum install -y keepalived
keepalived配置文件(主)
! Configuration File for keepalived
global_defs {
router_id lvs-10
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
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
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.99
}
}
|
keepalived配置文件(从)
! Configuration File for keepalived
global_defs {
router_id lvs-20
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP #也可以MASTER
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.99
}
}
|
nginx_check.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/server/nginx/sbin/nginx #如果nginx没有启动就先启动nginx
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
service keepalived stop #如果nginx还是没有启动就关闭keepalived把虚拟IP让出去(注意不同操作系统的命令有所不同)
fi
fi