1. 程式人生 > >Nginx+keepalived雙機熱備(主主模式)

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 {
    [email protected] 
   }
   notification_email_from [email protected]
   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 {
    [email protected] 
   }
   notification_email_from 
[email protected]
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