zoukankan      html  css  js  c++  java
  • Nginx+keepalived双机热备

    1. Nginx Master: 192.168.128.134  
    2. Nginx Backup: 192.168.128.138  
    3. VIP:192.168.128.177  

    Nginx安装见nginx安装配置文档

    Master与Backup的Nginx配置一致.

    在MASTER跟BACKUP上安装Keepalived:

    1. #tar zxvf keepalived-1.1.15.tar.gz -C ../software  
    2. #cd ../software/keepalived-1.1.15  
    3. #./configure –prefix=/usr/local/keepalived && make && make install  
    4. #cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/  
    5. #cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/  
    6. #mkdir /etc/keepalived  
    7. #cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/  
    8. #cp /usr/local/keepalived/sbin/keepalived /usr/sbin/  
    9. #service keepalived start|stop #做成系统启动服务方便管理.  

    修改MASTER 的keepalived.conf

    1. #vim /etc/keepalived/keepalived.conf  
    2. vrrp_script chk_http_port {         
    3.         script "/usr/local/scripts/nginx_pid.sh" ###监控脚本  
    4.         interval 2 ###监控时间  
    5.         weight 2  
    6.         }  
    7. vrrp_instance VI_1 {  
    8.         state MASTER ### 设置为主  
    9.         interface eth0 ### 监控网卡  
    10.         virtual_router_id 51 ### 这个两台服务器必须一样  
    11.         priority 101 ### 权重值MASTRE一定要高于BAUCKUP  
    12.         authentication {  
    13.         auth_type PASS ### 加密  
    14.         auth_pass test ### 加密的密码,两台服务器一定要一样,不然会出错  
    15.         }  
    16.         track_script {  
    17.         chk_http_port ### 执行监控的服务  
    18.         }  
    19.         virtual_ipaddress {                  
    20.         192.168.128.177 ### VIP 地址          
    21.         }  
    22. }  


    修改BACKUP keepalived.conf

    1. #vim /etc/keepalived/keepalived.conf  
    2. vrrp_script chk_http_port {  
    3.         script "/opt/nginx_pid.sh"  
    4.         interval 2  
    5.         weight 2  
    6. }  
    7. vrrp_instance VI_1 {  
    8.         state BACKUP ### 设置为备份机  
    9.         interface eth0  
    10.         virtual_router_id 51 ### 与 MASTRE 设置值一样  
    11.         priority 80 ### 比 MASTRE权重值低  
    12.   
    13.         authentication {  
    14.         auth_type PASS  
    15.         auth_pass test ### 密码 与 MASTRE 一样  
    16.         }  
    17.         track_script {  
    18.         chk_http_port  
    19.         }  
    20.         virtual_ipaddress {  
    21.         192.168.128.177  
    22.         }  
    23. }  

    编写监控nginx监控脚本

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


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

    1. #/usr/local/nginx/sbin/nginx  
    2. #/etc/init.d/keepalived start  

    监控 Nginx Mastaer 的日志

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

    监控 Nginx Backup 的日志

    1. May 11 22:28:21 localhost Keepalived: Starting Keepalived v1.1.15 (05/11,2010)  
    2. May 11 22:28:21 localhost Keepalived_vrrp: Using MII-BMSR NIC polling thread...  
    3. May 11 22:28:21 localhost Keepalived_vrrp: Registering Kernel netlink reflector  
    4. May 11 22:28:21 localhost Keepalived_vrrp: Registering Kernel netlink command channel  
    5. May 11 22:28:21 localhost Keepalived_vrrp: Registering gratutious ARP shared channel  
    6. May 11 22:28:21 localhost Keepalived: Starting VRRP child process, pid=27040  
    7. May 11 22:28:21 localhost Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.  
    8. May 11 22:28:21 localhost Keepalived_vrrp: Configuration is using : 35538 Bytes  
    9. May 11 22:28:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE  
    10. May 11 22:28:21 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]  
    11. May 11 22:28:23 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded  

    看日志可以看出,两台服务器的 MASTRE 和 BACUKUP 已经都正常了

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

    1. May 11 22:28:21 localhost Keepalived: Starting VRRP child process, pid=27040  
    2. May 11 22:28:21 localhost Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.  
    3. May 11 22:28:21 localhost Keepalived_vrrp: Configuration is using : 35538 Bytes  
    4. May 11 22:28:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE  
    5. May 11 22:28:21 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]  
    6. May 11 22:28:23 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded  
    7. May 11 22:29:25 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE  
    8. May 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE  
    9. May 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.  
    10. 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的日志

    1. May 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert  
    2. May 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE  
    3. May 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.  
    4. May 11 22:30:32 localhost avahi-daemon[2409]: Withdrawing address record for 192.168.128.177 on eth0.  

    可以看出又变回了backup.

  • 相关阅读:
    零点存在定理与介值定理
    moco入门
    git的基础操作-入门
    有道笔记链接地址 -----关于python
    Linux学习---linux的svn的配置与安装
    Linux学习---linux系统下安装配置Jenkins
    Linux学习-linux系统下安装jdk和tomcat,以及遇到的问题清单
    Linux学习-linux系统下python升级到python3.6步骤详解,以及遇到的问题解决
    Linux学习---Linux目录结构说明
    电脑屏幕左上角出现一个小的白块
  • 原文地址:https://www.cnblogs.com/tonykan/p/3532304.html
Copyright © 2011-2022 走看看