Nginx+keepalived 雙機熱備(主主模式)

散盡浮華發表於2016-12-08

 

之前已經介紹了Nginx+Keepalived雙機熱備的主從模式,今天在此基礎上說下主主模式的配置。

由之前的配置資訊可知:
master機器(master-node):103.110.98.14/192.168.1.14      VIP1:103.110.98.20
slave機器(slave-node):103.110.98.24/192.168.1.24       VIP2:103.110.98.21

主主模式需要兩個負載均衡的VIP,
之前設定了VIP(103.110.98.20)
所以還需要設定另一個VIP(103.110.98.21)

修改keepalived的配置

1)master負載機上的keepalived配置:(注意,這裡是雙主配置,MASTER-BACKUP和BACKUP-MASTER;如果是多主,比如三主,就是MATER-BACKUP-BACKUP、BACKUP-MASTER-BACKUP和BACKUP-BACKUP-MASTER
注意:
配置中的虛擬路由標識virtual_router_id在MASTER和BACKUP處配置不能一樣(但在主從模式下配置是一樣的)

[root@master-node ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived     
  
global_defs {
notification_email {     
ops@wangshibo.cn   
tech@wangshibo.cn
}
  
notification_email_from ops@wangshibo.cn  
smtp_server 127.0.0.1      
smtp_connect_timeout 30    
router_id master-node     
}
  
vrrp_script chk_http_port {      
    script "/opt/chk_nginx.sh"   
    interval 2                   
    weight -5                    
    fall 2                    
    rise 1                    
}
  
vrrp_instance VI_1 { 
    state MASTER    
    interface em1          
    mcast_src_ip 103.110.98.14 
    virtual_router_id 51        
    priority 101                 
    advert_int 1                 
    authentication {             
        auth_type PASS          
        auth_pass 1111           
    }

track_script {                     
   chk_http_port                    
}

virtual_ipaddress {         
    103.110.98.20
}

notify_master "/etc/keepalived/clean_arp.sh 103.110.98.20"
}

vrrp_instance VI_2 {            
    state BACKUP           
    interface em1            
    mcast_src_ip 103.110.98.24  
    virtual_router_id 52       
    priority 99               
    advert_int 1               
    authentication {            
        auth_type PASS         
        auth_pass 1111          
    }
 
track_script {                     
   chk_http_port                 
}
virtual_ipaddress {        
    103.110.98.21
    }
notify_master "/etc/keepalived/clean_arp.sh 103.10.86.21"
}

[root@master-node ~]# vim /etc/keepalived/clean_arp.sh         //更新vip的arp記錄到閘道器(注意指令碼中的網路卡別填錯了,要跟vip所在網路卡一致)
#!/bin/sh
VIP=$1
GATEWAY=103.110.98.1                                                         //負載均衡器的公網閘道器地址
/sbin/arping -I em1 -c 5 -s $VIP $GATEWAY &>/dev/null
[root@master-node ~]# chmod 755 /etc/keepalived/clean_arp.sh

2)slave負載機上的keepalived配置:

[root@slave-node ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived     
  
global_defs {
notification_email {     
ops@wangshibo.cn   
tech@wangshibo.cn
}
  
notification_email_from ops@wangshibo.cn  
smtp_server 127.0.0.1      
smtp_connect_timeout 30    
router_id slave-node     
}
  
vrrp_script chk_http_port {      
    script "/opt/chk_nginx.sh"   
    interval 2                   
    weight -5                    
    fall 2                    
    rise 1                    
}
  
vrrp_instance VI_1 { 
    state BACKUP    
    interface em1          
    mcast_src_ip 103.110.98.14
    virtual_router_id 51        
    priority 99                 
    advert_int 1                 
    authentication {             
        auth_type PASS          
        auth_pass 1111           
    }

track_script {                     
   chk_http_port                    
}

virtual_ipaddress {         
    103.110.98.20
}

notify_master "/etc/keepalived/clean_arp.sh 103.110.98.20"
}

vrrp_instance VI_2 {            
    state MASTER           
    interface em1            
    mcast_src_ip 103.110.98.24 
    virtual_router_id 52       
    priority 101               
    advert_int 1               
    authentication {            
        auth_type PASS         
        auth_pass 1111          
    }
 
track_script {                     
   chk_http_port                 
}
virtual_ipaddress {        
    103.110.98.21
    }
notify_master "/etc/keepalived/clean_arp.sh 21"
}

[root@slave-node ~]# vim /etc/keepalived/clean_arp.sh
#!/bin/sh
VIP=$1
GATEWAY=103.110.98.1
/sbin/arping -I em1 -c 5 -s $VIP $GATEWAY &>/dev/null
[root@slave-node ~]# chmod 755 /etc/keepalived/clean_arp.sh

重啟master和slave負載機的keepalive(保證兩臺機器的ngixn和keepalived服務都啟動)
[root@master-node ~]# /etc/init.d/keepalived restart
[root@slave-node ~]# /etc/init.d/keepalived restart

將nginx中配置的域名解析到這兩個VIP地址上:
103.110.98.20 dev.wangshibo.com
103.110.98.21 dev.wangshibo.com

瀏覽器訪問是正常的(如果master或slave有一臺當機,或其中一個VIP故障,只要另一臺是正常的就行)

 

關閉兩臺負載機其中一臺的keepalived服務,那麼它的VIP就會自動漂移到另一臺機器上。
關閉兩臺機器的nginx,會自動重啟(前提是keepalived服務要啟動)!對網站域名的訪問絲毫不受影響。

[root@master-node ~]# pkill -9 nginx
root 32365 9775 0 19:04 pts/0 00:00:00 grep --color=auto nginx
[root@master-node ~]# ps -ef|grep nginx
root 32367 9775 0 19:04 pts/0 00:00:00 grep --color=auto nginx
[root@master-node ~]# ps -ef|grep nginx
root 32369 32368 0 19:04 ? 00:00:00 /bin/bash /opt/chk_nginx.sh
root 32374 1 0 19:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www 32376 32374 1 19:04 ? 00:00:00 nginx: worker process
www 32377 32374 1 19:04 ? 00:00:00 nginx: worker process
www 32378 32374 1 19:04 ? 00:00:00 nginx: worker process
www 32379 32374 1 19:04 ? 00:00:00 nginx: worker process
www 32380 32374 1 19:04 ? 00:00:00 nginx: worker process
www 32381 32374 1 19:04 ? 00:00:00 nginx: worker process
www 32382 32374 1 19:04 ? 00:00:00 nginx: worker process
www 32383 32374 1 19:04 ? 00:00:00 nginx: worker process
www 32384 32374 0 19:04 ? 00:00:00 nginx: cache manager process
www 32385 32374 0 19:04 ? 00:00:00 nginx: cache loader process
root 32387 9775 0 19:04 pts/0 00:00:00 grep --color=auto nginx

相關文章