Nginx+keepalived双机热备

一:环境介绍:

Nginx Master: 122.112.X.XNginx Backup: 211.101.X.XVIP:Keepalived 1.2.13Nginx安装见nginx安装配置文档Master与Backup的Nginx配置一致.在MASTER跟BACKUP上安装Keepalived:

二:安 装:\\ 两台接入服务器分别安装NginX和keepalived:

#tar zxvf keepalived-1.2.13.tar.gz   #cd /usr/local/keepalived-1.2.13 #./configure  && make && make install       Keepalived configuration      ------------------------      Keepalived version       : 1.2.13      Compiler                 : gcc      Compiler flags           : -g -O2      Extra Lib                : -lssl -lcrypto -lcrypt       Use IPVS Framework       : Yes      IPVS sync daemon support : Yes      IPVS use libnl           : No      fwmark socket support    : Yes      Use VRRP Framework       : Yes      Use VRRP VMAC            : Yes      SNMP support             : No      SHA1 support             : No      Use Debug flags          : No       ( 出现以上提示则安装成功 )       #cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/#mkdir /etc/keepalived#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/#cp /usr/local/sbin/keepalived /usr/sbin/ #service keepalived start|stop                         #做成系统启动服务方便管理.  #echo "/etc/init.d/keepalived start" >> /etc/rc.local        #加入启动服务

三:配置:\\修改MASTER 的keepalived.conf

#vim /etc/keepalived/keepalived.conf  

! Configuration File for keepalived

global_defs {   notification_email {     [email protected]     [email protected]     [email protected]   }   notification_email_from [email protected]   smtp_server 192.168.200.1   smtp_connect_timeout 30   router_id nginx_master}vrrp_script chk_http_port {                 script "/usr/local/scripts/nginx_pid.sh" ###监控脚本          interval 2 ###监控时间          weight 2          }  vrrp_instance VI_1 {          state MASTER ### 设置为主          interface eth0 ### 监控网卡          virtual_router_id 51 ### 这个两台服务器必须一样          priority 101 ### 权重值MASTRE一定要高于BAUCKUP          authentication {          auth_type PASS ### 加密          auth_pass test ### 加密的密码,两台服务器一定要一样,不然会出错          }          track_script {          chk_http_port ### 执行监控的服务          }          virtual_ipaddress {                          192.168.128.177 ### VIP 地址                  }  }  

\\修改BACKUP keepalived.conf

#vim /etc/keepalived/keepalived.conf  

! Configuration File for keepalived

global_defs {   notification_email {     [email protected]     [email protected]     [email protected]   }   notification_email_from [email protected]   smtp_server 192.168.200.1   smtp_connect_timeout 30   router_id nginx_backup}vrrp_script chk_http_port {          script "/opt/nginx_pid.sh"          interval 2          weight 2  }  vrrp_instance VI_1 {          state BACKUP ### 设置为备份机          interface eth0          virtual_router_id 51 ### 与 MASTRE 设置值一样          priority 80 ### 比 MASTRE权重值低            authentication {          auth_type PASS          auth_pass test ### 密码 与 MASTRE 一样          }          track_script {          chk_http_port          }          virtual_ipaddress {          192.168.128.177          }  }  
\\编写监控nginx监控脚本

#vim /usr/local/scripts/nginx_pid.sh 

 #!/bin/bash  A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A  if [ $A -eq 0 ];then <span style="white-space:pre"> </span>## 如果没有进程值得为 零  <span style="white-space:pre">  </span>/usr/local/nginx/sbin/nginx  <span style="white-space:pre">  </span>sleep 3  <span style="white-space:pre">  </span>if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then  <span style="white-space:pre">  </span>killall keepalived ## 则结束 keepalived 进程  <span style="white-space:pre">  </span>fi  fi  

\\测试,分别在两个服务器 启动 nginx 和 keepalived

1>先后在主、从服务器上启动keepalived: /etc/init.d/keepalived start2>在主服务器上查看是否已经绑定了虚拟IP: ip addr3>停止主服务器上的keepalived: /etc/init.d/keepalived stop 然后在从服务器上查看是否已经绑定了虚拟IP:4>启动主服务器上的keepalived,看看主服务器能否重新接管虚拟IP\\监控 Nginx Mastaer 的日志

May 12 17:33:44 localhost Keepalived_vrrp: Configuration is using : 35676 Bytes  May 12 17:33:44 localhost Keepalived: Starting VRRP child process, pid=1245  May 12 17:33:44 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)] May 12 17:33:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE  May 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE  May 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.  May 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.128.177  May 12 17:33:46 localhost avahi-daemon[2344]: Registering new address record for 192.168.128.177 on eth0.  May 12 17:33:46 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.128.177 added  

\\监控 Nginx Backup 的日志

May 11 22:28:21 localhost Keepalived: Starting Keepalived v1.1.15 (05/11,2010)  May 11 22:28:21 localhost Keepalived_vrrp: Using MII-BMSR NIC polling thread...  May 11 22:28:21 localhost Keepalived_vrrp: Registering Kernel netlink reflector  May 11 22:28:21 localhost Keepalived_vrrp: Registering Kernel netlink command channel  May 11 22:28:21 localhost Keepalived_vrrp: Registering gratutious ARP shared channel  May 11 22:28:21 localhost Keepalived: Starting VRRP child process, pid=27040  May 11 22:28:21 localhost Keepalived_vrrp: Opening file ‘/etc/keepalived/keepalived.conf‘.  May 11 22:28:21 localhost Keepalived_vrrp: Configuration is using : 35538 Bytes  May 11 22:28:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE  May 11 22:28:21 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]  May 11 22:28:23 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded  看日志可以看出,两台服务器的 MASTRE 和 BACUKUP 已经都正常了

现在我们把Master的Nginx停掉.查看Backup的日志

[plain] view plaincopyMay 11 22:28:21 localhost Keepalived: Starting VRRP child process, pid=27040  May 11 22:28:21 localhost Keepalived_vrrp: Opening file ‘/etc/keepalived/keepalived.conf‘.  May 11 22:28:21 localhost Keepalived_vrrp: Configuration is using : 35538 Bytes  May 11 22:28:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE  May 11 22:28:21 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]  May 11 22:28:23 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded  May 11 22:29:25 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE  May 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE  May 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.  May 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.128.177  可以看出backup已经变成master

现在再启动原来master的nginx,再查看backup的日志

[plain] view plaincopyMay 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert  May 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE  May 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.  May 11 22:30:32 localhost avahi-daemon[2409]: Withdrawing address record for 192.168.128.177 on eth0.  可以看出又变回了backup.

时间: 2024-11-08 21:46:16

Nginx+keepalived双机热备的相关文章

Nginx+keepalived双机热备+负载均衡 ???待续

keepalived+nginx双机热备+负载均衡 最近因业务扩展,需要将当前的apache 转为nginx(web), 再在web前端放置nginx(负载均衡).同时结合keepalived 对前端nginx实现HA.nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能.在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发,从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都 由

nginx + keepalived 双机热备

序 双机热备是指两台机器都在运行,但并非两台机器同时在提供服务. 当提供服务的一台出现故障的时候,另外一台会马上自动接管并且提供服务,且切换的时间非常短. keepalived的工作原理是VRRP——虚拟路由冗余协议. 测试环境如下: ip vip master 192.168.174.135 192.168.174.140 backup 192.168.174.137 192.168.174.140 nginx 安装 sudo apt-get install nginx  查找配置文件位置 s

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

双机高可用一般是通过虚拟IP(飘移IP)方法来实现的,基于Linux/Unix的IP别名技术. 双机高可用方法目前分为两种: 1)双机主从模式:即前端使用两台服务器,一台主服务器和一台热备服务器,正常情况下,主服务器绑定一个公网虚拟IP,提供负载均衡服务,热备服务器处于空闲状态:当主服务器发生故障时,热备服务器接管主服务器的公网虚拟IP,提供负载均衡服务:但是热备服务器在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案不经济实惠. 2)双机主主模式:即前端使用两台负载均衡服

Nginx+Keepalived(双机热备)搭建高可用负载均衡环境(HA)

原文:https://my.oschina.net/xshuai/blog/917097 摘要: Nginx+Keepalived搭建高可用负载均衡环境(HA) http://blog.csdn.net/xyang81/article/details/52554398可以看更多介绍 Keepalived的介绍可以百度一堆一堆的资料.一定要看看哦. 1.基于上一篇博客总结,再次安装一个虚拟机当backup服务器,这个服务器只安装Keepalived+Nginx即可 2.Master还是上一篇博文的

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

IP说明: master机器(master-node):10.0.0.5/172.16.1.5   VIP1:10.0.0.3slave机器(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_r

keepalived双机热备nginx

nginx目前是我最常用的反向代理服务,线上环境为了能更好的应对突发情况,一般会使用keepalived双机热备nginx或者使用docker跑nginx集群,keepalived是比较传统的方式,虽然用docker跑nginx集群更方便,但传统的方式总是有他的可取之处,并且多学一些东西也很好.以后也会写如何使用docker跑nginx集群. 环境准备: 2台centos: 192.168.0.105 和192.168.0.118, 虚拟IP(VIP)为192.168.0.119 配置keepa

&#8203;Keepalived双机热备

Keepalived双机热备 Keepalived是专门针对LVS设计的一款强大的辅助工具,主要用来提供故障切换和健检查功能,如判断LVS负载调度器.节点服务器的可用性,及时隔离并替换为新的服务器,当故障主机恢复后将其重新加入群集.在非LVS群集环境中使用时Keepalived也可以作为热备软件使用. Keepalived采用VRRP(virtual  router  redundancy protocol,虚拟路由冗余协议)热备份协议,以软件的方式实现linux服务器的多机热备功能.VRRP是

Centos 7 keepalived双机热备

Centos 7 keepalived双机热备 操作环境:keepalived热备份(双机热备)Centos 7系统ip地址: 192.168.80.100 --主服务器Centos 7系统ip地址: 192.168.80.101 --从服务器windows系统ip地址: 192.168.80.20 --测试客户端 Centos 7系统ip地址: 192.168.80.100操作如下:1. keepalived软件安装yum install keepalived -y 2.修改配置文件vi /e

centos 7之keepalived双机热备理论+配置文件详解

一.keepalived工作原理及作用: keepalived最初是专门针对LVS设计的一款强大的辅助工具,主要用来提供故障切换和健康检 查功能--判断LVS负载调度器.节点服务器的可用性,及时隔离并替换为新的服务器,当故 障主机回复后将其重新加入群集.单独部署LVS环境的话,调度器发生宕机的话,整个群集就 失效了,某一个web节点宕机后,客户端在访问时,总会碰上访问不到网页的情况,所以,将 keepalived和LVS结合起来,才可形成一个真正的高可用群集,当然,后端的共享存储也必须 搭建一个