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

 

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

由以前的配置信息可知:
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.21node

主主模式須要兩個負載均衡的VIP,
以前設置了VIP(103.110.98.20)
因此還須要設置另外一個VIP(103.110.98.21)nginx

修改keepalived的配置vim

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 bash

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 restartspa

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

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

 

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

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

相關文章
相關標籤/搜索