wsaer 发表于 2018-11-8 11:48:06

Nginx HA (主从模式)

要想生活过的好,就要学习学到老。。。
最近学习了Nginx HA,跟大家分享下。
言归正传,先上原理图


通过keepalived提供的VRRP,以及心跳监测技术实现两台主机的双击热备功能。
实验环境:
系统:RHEL6.1
  Nginx:nginx-1.0.8      (http://nginx.org/en/download.html)
  Pcre: Pcre-8.13            (http://sourceforge.net/projects/pcre/files/pcre/8.13/pcre-8.13.tar.gz/download)
  Google-perftools:google-perftools-1.7   (http://code.google.com/p/gperftools/downloads/list)
  Keepalived:      keepalived-1.2.2   (http://www.keepalived.org/download.html)
安装过程:
Nginx安装:
    1.   安装PCRE(正则匹配)模块
tar-zxvf pcre-8.13.tar.gz
cd pcre-8.13
./configure
make && make install
2.   安装google-perftool(google高性能内存操作模块)
tar-zxvf nginx-1.0.8.tar.gz –C /tmp
cdnginx-1.0.8
./configure
make && make install
3.安装nginx
tar zxvf nginx-1.0.8.tar.gz
  cd nginx-1.0.8
  ./configure--user=nobody --group=nobody    #指定运行nginx的用户
  --prefix=/usr/local/nginx \                                    #指定安装位置
  --with-http_stub_status_module \                     #nginx状态监控模块
  --with-http_ssl_module \                                    #https 支持模块
  --with-http_flv_module \                                     #流媒体支持模块
  --with-http_gzip_static_module \                     #gzip支持模块
  --with-google_perftools_module                  #goole-perftools支持模块
  make && make install
keepalived安装
1.       安装Keepalived
tar-zxvf keepalived-1.2.2.tar.gz
cdkeepalived-1.2.2
./configure --prefix=/usr/local/keepalived
make && make install
    2.       配置Keepalived
编辑keepalived主配置文件/etc/keepalived/keepalived.conf,如下:
global_defs {                              #全局配置
   notification_email {                     #邮件通知
   linuxsong49@163.com
   }
   notification_email_from nginx@gyyx.cn
   smtp_server 127.0.0.1
   smtp_connect_timemout 30
   router_id nginx.gyyx.cn                  #router_id 配置(主从必须一致,也可以默认不改)
}
vrrp_script check_run {                  #nginx监控脚本配置
    script"/home/check_nginx.sh"            #脚本位置
weight -5                                  #优先级-5
interval 5                                  #检查5次
}
vrrp_instance VI_1 {                      #监控实例配置
    state MASTER                            #标记状态为主
    interface eth0                        #监听网卡
    virtual_router_id 60                  #实例router_id
    priority 100                            #优先级(优先级大者为主)
mcast_src_ip 192.168.10.2                   #广播地址(本地IP)
    authentication {                        #主从认证配置
      auth_type PASS
      auth_pass 1q2w3e4r
    }
    track_script {                        #启用监控脚本
         check_run
   }
    virtual_ipaddress {                     #虚拟IP配置
      192.168.10.100/24 dev eth0
    }
    virtual_routes {                        #虚拟路由配置
      0.0.0.0/0.0.0.0 via192.168.10.100 dev eth0
    }
}
3.       Nginx监控脚本配置
监控脚本主要通过访问本地的80端口(nginx.conf里配置的监控url),去监控Nginx的服务状态,如果没有问题返回0,如果有问题返回1
#!/bin/bash
#This script is used by keepalived for checking nginx running status
CHECK_TIME=2
check()
{
      curl -m 2 http://127.0.0.1/status >/dev/null 2>&1
      return $?
}
while [ $CHECK_TIME -ne 0 ]
do
      let "CHECK_TIME -= 1"
      check
      NGINX_OK=$?
      if [ $NGINX_OK -eq 0 ];then
                exit 0
      fi
      if [ $NGINX_OK -ne 1 ] &&[ $CHECK_TIME -eq 0 ]
      then
                exit 1
      fi
done
4.    编辑nginx配置文件,加入如下内容
  location /status {
  stub_status on;
  access_log off;
  以上只是主Keepalived的配置文件,可以将配置文件scp到从的上面,然后修改标注状态为MASTER,优先级小于100即可。
5.测试Nginx可以正常启动,访问本地的http://127.0.0.1/status可以正常提供服务,模拟宕机,另外一台接管。



页: [1]
查看完整版本: Nginx HA (主从模式)