nginx+keepalived高可用web架构

1、下载所需的软件包

(1)、keepalived软件包
    keepalived-1.1.20.tar.gz
(2)、nginx软件包
    nginx-1.1.6.tar.gz
(3)、nginx模块软件包
    libunwind-0.99.tar.gz
    agentzh-encrypted-session-nginx-module-v0.02-0-gc752861.tar.gz
    chunkin-nginx-module-0.23rc2.tar.gz
    google-perftools-1.8.3.tar.gz
    pcre-8.11.tar.gz
    simpl-ngx_devel_kit-v0.2.17-10-g4192ba6.tar.gz

2、安装脚本

#!/bin/bash
#
#
# this is install nginx
#
#
keepalived_dir=/usr/local/keepalived
tar_dir=/usr/src
kernel=$(uname -r)    
software_dir=/root/software

cd $software_dir
yum install gcc gcc-c++ autoconf automake wget openssl-devel kernel-devel  pcre-devel zlib-devel -y
echo "tar and mv nginx module"
tar -zxf agentzh-encrypted-session-nginx-module-v0.02-0-gc752861.tar.gz 
mv agentzh-encrypted-session-nginx-module-c752861 encrypted-session-nginx-module

tar -zxf chunkin-nginx-module-0.23rc2.tar.gz 
mv chunkin-nginx-module-0.23rc2 chunkin-nginx-module

tar -zxf simpl-ngx_devel_kit-v0.2.17-10-g4192ba6.tar.gz 
mv simpl-ngx_devel_kit-4192ba6 ngx_devel_kit

echo "configure and install pcre"
tar zxf pcre-8.11.tar.gz 
cd pcre-8.11
make clean
./configure 
make && make install


echo "configure and install google-perftools use"
cd $software_dir
tar -zxf libunwind-0.99.tar.gz 
cd libunwind-0.99
make clean
./configure  CFLAGS=-fPIC 
make CFLAGS=-fPIC
make install CFLAGS=-fPIC 

echo "configure and install google-perftools"
cd $software_dir
tar zxf google-perftools-1.8.3.tar.gz 
cd google-perftools-1.8.3
./configure --enable-frame-pointers #64
make -j 4 && make install

ln -s /usr/local/lib/libprofiler.so.0 /lib/
ln -s /usr/local/lib/libprofiler.so.0 /usr/lib
ln -s /usr/local/lib/libprofiler.so.0 /lib64
ln -s /usr/local/lib/libprofiler.so.0 /usr/lib64

ln -s /usr/local/lib/libunwind.so.7 /lib/
ln -s /usr/local/lib/libunwind.so.7 /usr/lib
ln -s /usr/local/lib/libunwind.so.7 /lib64
ln -s /usr/local/lib/libunwind.so.7 /usr/lib64

echo "configure and install nginx"
cd $software_dir
tar -zxf nginx-1.1.6.tar.gz 
cd nginx-1.1.6
make clean
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-file-aio --with-http_stub_status_module --with-http_sub_module --with-http_addition_module --with-http_random_index_module --with-http_secure_link_module --with-http_dav_module --with-http_mp4_module --with-http_degradation_module --with-http_gzip_static_module --with-google_perftools_module --with-http_flv_module --add-module=/root/software/chunkin-nginx-module --add-module=/root/software/ngx_devel_kit/ --add-module=/root/software/encrypted-session-nginx-module --with-pcre
make && make install
cd $software_dir

useradd www  #or configure nginx.conf nobody 
/usr/local/nginx/sbin/nginx      #启动nginx

#
#
#this is for install keepalived bash
#
#

cd $software_dir
tar -zxf keepalived-*.tar.gz -C $tar_dir
cd /usr/src/keepalived-*
./configure --prefix=$keepalived_dir --sysconf=/etc --with-kernel-dir=/usr/src/kernels/$kernel*
make && make install
    
cp /usr/local/keepalived/sbin/keepalived /usr/sbin
cp /usr/local/keepalived/bin/genhash /usr/sbin/
/etc/init.d/keepalived restart

3、keepalived配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id httpd
}

vrrp_script check_http {
        script "/root/bash/check_httpd.sh"
        weight -5
        interval 1
    }

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 240
    priority 98
    advert_int 1
    track_script {
        check_http
    }
    authentication {
        auth_type PASS
        auth_pass 3333
    }
    virtual_ipaddress {
       192.168.80.200/24 dev eth0
    }
}

4、check_httpd.sh监测脚本

#!/bin/bash

CHECK_TIME=2

check()
{
        curl http://127.0.0.1/ >/dev/null 2>&1
        ret=$?
        if [ $ret -ne 0 ];then
                return $ret;
        fi
}
while [ $CHECK_TIME -ne 0 ]
do
        let "CHECK_TIME -= 1"
        check
        HTTP_OK=$?
        if [ $HTTP_OK -eq 0 ];then
                exit 0
        fi
        if [ $HTTP_OK -ne 0 ] &&  [ $CHECK_TIME -eq 0 ]
        then
            exit 1
        fi
done