首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx+keepalived双机热备(主主模式)

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

作者头像
洗尽了浮华
发布2018-01-23 11:12:22
1.3K0
发布2018-01-23 11:12:22
举报
文章被收录于专栏:散尽浮华散尽浮华

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-12-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
负载均衡
负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档