keepAlived主备及双主

Posted happydreamzjl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepAlived主备及双主相关的知识,希望对你有一定的参考价值。

nginx用默认配置即可

1、主备配置

1、主keepAlived配置

vrrp_instance VI_1 
    state MASTER   #主备区分
    interface eth0
    virtual_router_id 51  #主备一致
    priority 100   #主备区分
    advert_int 1
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.244.200
    
    track_script 
        chk_nginx
    

2、备keepAlived配置

vrrp_instance VI_1 
    state BACKUP  #主备区分
    interface eth0
    virtual_router_id 51   #主备一致
    priority 50     #主备区分
    advert_int 1

    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.244.200
    

2、互为主备

1、第一台keepAlived配置

vrrp_instance VI_1 
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.244.200
    


vrrp_instance VI_2 
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 50
    advert_int 1

    authentication 
        auth_type PASS
        auth_pass 2222
    
    virtual_ipaddress 
        192.168.244.201
    

2、第二台keepAlived配置

vrrp_instance VI_1 
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1

    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.244.200
    

vrrp_instance VI_2 
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1

    authentication 
        auth_type PASS
        auth_pass 2222
    
    virtual_ipaddress 
        192.168.244.201
    

 

3、检测nginx,实现主备高可用

1、第一台keepAlived配置

vrrp_script chk_nginx 
    script "/zjl/program/nginx/conf/checkNginxAlived.sh"
    interval 2
    weight 2


vrrp_instance VI_1 
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.244.200
    
    track_script 
        chk_nginx
    


vrrp_instance VI_2 
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 50
    advert_int 1

    authentication 
        auth_type PASS
        auth_pass 2222
    
    virtual_ipaddress 
        192.168.244.201
    
    track_script 
        chk_nginx
    

2、第二台keepAlived配置

vrrp_script chk_nginx 
    script "/zjl/program/nginx/conf/checkNginxAlived.sh"
    interval 2
    weight 2

vrrp_instance VI_1 
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1

    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.244.200
    
    track_script 
        chk_nginx
    

vrrp_instance VI_2 
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1

    authentication 
        auth_type PASS
        auth_pass 2222
    
    virtual_ipaddress 
        192.168.244.201
    
    track_script 
        chk_nginx
    

3、监测脚本

#!/bin/bash
psnginx=`ps -C nginx --no-heading|wc -l`
if [ $psnginx == 0 ] ; then
    echo "nginx服务已关闭,尝试重新启动..."
    /zjl/commond/nginxbegin-start.sh
    sleep 3
    if [ `ps -C nginx --no-heading|wc -l` -eq 0 ] ; then
        echo "nginx尝试启动失败关闭keepAlived"
        killall keepalived
    else
        echo "nginx重新启动成功"
    fi
else
    echo "nginx服务正常..."
fi

以上是关于keepAlived主备及双主的主要内容,如果未能解决你的问题,请参考以下文章

keepalived主从及双主配置

Keepalived 的script及双主

nginx+keepalived高可用及双主模式

nginx+keepalived高可用及双主模式

Centos7+nginx+keepalived高可用及双主模式

keepalived+nginx+apache双活搭建(双网卡模式)