Fork me on GitHub

Nginx+keepalived双机热备(主主模式)

IP说明:

master机器(master-node):10.0.0.5/172.16.1.5   VIP1:10.0.0.3
slave机器(slave-node): 10.0.0.6/172.16.1.6   VIP2:10.0.0.4

注意事项:

双主配置:MASTER-BACKUP和BACKUP-MASTER;

如果是三主,就是MATER-BACKUP-BACKUP、BACKUP-MASTER-BACKUP和BACKUP-BACKUP-MASTER;

配置中的虚拟路由标识virtual_router_id在MASTER和BACKUP处配置不能一样,但在主从模式下配置是一样的.

1.master上的keepalived配置

global_defs {
   notification_email {
    174646513@qq.com 
   }
   notification_email_from 17461651@qq.com
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lb01
}

vrrp_script chk_http_port {
    script "/service/scripts/chk_nginx.sh"
    interval 2
    weight -5
    fall 2
    rise 1
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    mcast_src_ip 10.0.0.5
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3
    }

    track_script {
        chk_http_port
    }
notify_master "/etc/keepalived/clean_arp.sh 10.0.0.3"
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    mcast_src_ip 10.0.0.6
    virtual_router_id 52
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.4
    }

    track_script {
        chk_http_port
    }
notify_master "/etc/keepalived/clean_arp.sh 10.0.0.4"
}

2.更新vip的arp记录到网关的脚本

cat /etc/keepalived/clean_arp.sh
#!/bin/sh
VIP=$1
GATEWAY=10.0.0.2  # 负载均衡器的网关地址
/sbin/arping -I em1 -c 5 -s $VIP $GATEWAY &>/dev/null
chmod 755 /etc/keepalived/clean_arp.sh

3.slave上的keepalived配置

global_defs {
   notification_email {
    174646513@qq.com 
   }
   notification_email_from 17461651@qq.com
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lb01
}

vrrp_script chk_http_port {
    script "/service/scripts/chk_nginx.sh"
    interval 2
    weight -5
    fall 2
    rise 1
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    mcast_src_ip 10.0.0.5
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3
    }

    track_script {
        chk_http_port
    }
notify_master "/etc/keepalived/clean_arp.sh 10.0.0.3"
}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    mcast_src_ip 10.0.0.6
    virtual_router_id 52
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.4
    }

    track_script {
        chk_http_port
    }
notify_master "/etc/keepalived/clean_arp.sh 10.0.0.4"
}

在测试机10.0.0.51上修改/etc/hosts文件,将三个域名分别指向10.0.0.3、10.0.0.4,测试--正常.

双主模式总结:谁是MASTER,谁的优先级就高,谁的虚拟IP就生效.

双主模式参考博客:https://www.cnblogs.com/kevingrace/p/6146031.html

 

posted @ 2018-12-24 12:18  法外狂徒  阅读(245)  评论(0编辑  收藏  举报