keepalived对nginx高可用演练脚本

Posted 技术为王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived对nginx高可用演练脚本相关的知识,希望对你有一定的参考价值。

keepalived对nginx高可用演练脚本

 

参考文章:http://deidara.blog.51cto.com/400447/302402/

 

1、安装nginx、keepalived、epel-release源
yum install -y epel-release
yum install -y nginx
yum install -y keepalived



2、配置好nginx


3、设置keepalived配置文件
#主机
vi /etc/keepalived/keepalived.conf
vrrp_script chk_nginx { 
                script "/usr/local/nginx_pid.sh"        
                interval 2                                                
                fall 2       #测试失败的次数
                rise 1     #测试成功的次数
} 
vrrp_instance VI_1 { 
        state MASTER                                
        interface eth0                          
        virtual_router_id 51                   
        priority 100                                
        advert_int 5
    authentication { 
                     auth_type PASS           
                     auth_pass eric              
        } 
        track_script { 
                chk_nginx                     
        } 
        virtual_ipaddress { 
             192.168.14.206                          
        } 
} 

#备机
vi /etc/keepalived/keepalived.conf
vrrp_script chk_nginx { 
                script "/usr/local/nginx_pid.sh"        
                interval 2                                                
                fall 2       
                rise 1     
} 
vrrp_instance VI_1 { 
        state BACKUP                                
        interface eth0                          
        virtual_router_id 51                   
        priority 99                                
        advert_int 5
    authentication { 
                     auth_type PASS           
                     auth_pass eric              
        } 
        track_script { 
                chk_nginx                     
        } 
        virtual_ipaddress { 
             192.168.14.206                          
        } 
} 



4、自备都建nginx_chk脚本
vi /opt/nginx_chk.sh 
#!/bin/bash
# version 0.0.2
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 3
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
           killall keepalived                        ## 则结束 keepalived 进程
    fi
fi

chmod +x  /opt/nginx_pid.sh 


5、启动nginx和keepalived
/etc/init.d/nginx start
/etc/init.d/keepalived start

6、查看日志和ip
ip addr
tailf /var/log/messages


7、故障演练
killall nginx
tailf /var/log/messages  #查看两边的日志







   

 

以上是关于keepalived对nginx高可用演练脚本的主要内容,如果未能解决你的问题,请参考以下文章

keepalived实战

keepalived实战

nginx+keepalived的高可用脚本

keepalived高可用集群技术介绍及实战演练

nginx + keepalived 实现高可用性

Nginx+Keepalived高可用集群应用实践