keepalived+nginx 高可用性配置

keepalived+nginx 高可用性配置
安装keepalived  ipvsadm
yum -y install keepalived ipvsadm


master keepalived 配置
global_defs {
   router_id mmo-nginx-master
}
vrrp_script chk_http_port {  
    script "/usr/local/nginx/chk_nginx.sh"
    interval 1  
}  

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass !QAZ2wsx
    }
    virtual_ipaddress {
        10.9.16.137
    }
    track_script {  
        chk_http_port  
    } 
}




backup keepalived 配置


global_defs {
   router_id mmo-nginx-back
}
vrrp_script chk_http_port {  
    script "/usr/local/nginx/chk_nginx.sh"
    interval 1  
}  

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass !QAZ2wsx
    }
    virtual_ipaddress {
        10.9.16.137
    }
    track_script {  
        chk_http_port  
    } 
}


nginx健康检查脚本chk_nginx.sh
#!/bin/sh
# check nginx server status
NGINX=/usr/local/nginx/sbin/nginx
PORT=80

nmap localhost -p $PORT | grep "$PORT/tcp open"
#echo $?
if [ $? -ne 0 ];then
    $NGINX -s stop
    $NGINX
    sleep 5
    nmap localhost -p $PORT | grep "$PORT/tcp open"
    [ $? -ne 0 ] && /etc/init.d/keepalived stop
fi


nmap 安装
yum -y install nmap

查看keepalived日志
tail -f /var/log/messages

注意
关闭防火墙,如果开启防火墙,防火墙要配置

-A INPUT -i eth0 -p vrrp -j ACCEPT

猜你喜欢

转载自gangling.iteye.com/blog/2293094
今日推荐