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

 

之前已经介绍了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

posted @ 2016-12-08 18:36 散尽浮华 阅读(...) 评论(...) 编辑 收藏