Nginx HA安装


原理概述

  

wKioL1PMvpWwLmPxAACUr3w9OPw112.jpg

  通过keepalived提供的VRRP,以及心跳监测技术实现两台主机的双击热备功能。

安装环境描述

   硬件环境:

                   CPU:                                      Intel(R) Xeon(R) CPU E5506 虚拟机

                   Mem:                          1G

                   Disk:                                      10G

         软件环境:

                   OS:                               RHEL5.332bit

                   Nginx:                         nginx-1.0.8

Pcre:                            pcre-8.13

                   Google-perftoolsgoogle-perftools-1.7

                   Keepalived            keepalived-1.2.2

安装过程

 Nginx安装

  1. 安装PCRE(正则匹配)模块

tar  -zxvf pcre-8.13.tar.gz �CC /tmp

cd  /tmp/pcre && ./configure &&make && make install

 

  1. 安装google-perftool(google高性能内存操作模块)

tar  -zxvfnginx-1.0.8.tar.gz �CC /tmp

cd  /tmp/nginx-1.0.8&& ./configure &&make && make install

        

  1. 安装nginx

./configure  --user=nobody --group=nobody --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  -zxvfkeepalived-1.2.2.tar.gz �CC /tmp

cd  /tmp/keepalived&& ./configure �Cprefix=/ && make && make install

        

  1. 配置Keepalived

编辑keepalived主配置文件/etc/keepalived/keepalived.conf,如下:

 

global_defs{                                                                                         #全局配置

   notification_email {                                                                      #邮件通知配置

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 127.0.0.1

   smtp_connect_timemout 30

   router_id nginx.gyyx.cn                                                              #router_id配置(主从必须一致)

}

 

vrrp_scriptcheck_run {                                                                      #nginx监控脚本配置

    script "/home/check_nginx.sh"

    weight -5

    interval 5

}

 

vrrp_sync_groupVG1 {

     group {

       VI_1

     }

}

 

vrrp_instanceVI_1 {                                                                           #监控实例配置

    state BACKUP                                                                             #标记状态为从

    interface eth1                                                                             #监听网卡

    virtual_router_id 60                                                                  #实例router_id

    priority 90                                                                                     #优先级(优先级大者为主)

    mcast_src_ip 10.0.1.218                                                         #广播地址(本地IP)

    authentication {                                                                          #主从认证配置

        auth_type PASS

        auth_pass 1q2w3e4r

    }

    track_script {                                                                               #启用监控脚本

         check_run

     }

    virtual_ipaddress {                                                                     #虚拟IP配置

        219.238.232.219/26 dev eth0

        219.238.232.227/26 dev eth0

    }

    virtual_routes {                                                                                     #虚拟路由配置

        0.0.0.0/0.0.0.0 via 219.238.232.195 deveth0

    }

}

 

  1. Nginx监控脚本配置

监控脚本主要通过访问本地的1080端口(nginx.conf里配置的监控url),去监控Nginx的服务状态,如果没有问题返回0,如果有问题返回1

#!/usr/bin/env bash

 

#This script is used by keepalived for checking nginx running status

 

CHECK_TIME=2

 

check()

{

        curl -m 2http://127.0.0.1:1080/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

 

安装测试

1.      Nginx 可以正常提供服务。

2.      http://127.0.0.1:1080/status 状态URL可以正常提供服务

3.      Keepalived可以正常启动

4.      模拟服务宕机后,另一台热备机器可以接管资源

结论

Keepavlied的双机主要需要注意的是对于服务的监控,尽可能的监控到最贴近用户请求的过程。


你可能感兴趣的:(虚拟机,技术,软件,硬件,Intel)