随着nginx的发展越是成熟,nginx的高并发、高稳定性及支持反向代理等特性使得其倍受青睐。下面笔者就来对nginx+keepalived的高可用架构进行简单搭建。笔者水平有限,仅供参考,如有不足望指正。见笑了。^_^ ^_^ 一、实验环境 系统环境:centos 6.0 x86_64 nginx版本 nginx-1.2.7 keepalived版本 keepalived-1.26.tar.gz nginx_1 192.168.1.105 (master) nginx_2 192.168.1.106 (backup) vip 192.168.1.200 二、nginx安装配置 分别在105、106俩台实验机器上执行下面脚本,安装配置nginx:
#!/bin/bash ##This is a auto install nginx shell scripts. ##li 2013-03-16 ##defie path DIR1=/usr/local/src/ DIR2=/usr/local/nginx/ DIR3=http://nginx.org/download/nginx-1.2.7.tar.gz DIR4=/usr/local/src/nginx-1.2.7.tar.gz DIR5=nginx-1.2.7 yum install pcre pcre-devel -y if [ $UID -ne 0 ];then The shell scripts user must is root or adminstrator,please change user. sleep 3 exit fi if [ ! -d $DIR2 ];then mkdir -p /usr/local/nginx/ else echo "The directory was exit." fi download () { cd $DIR1 ;wget $DIR3 if [ ! -f $DIR4 ];then ehco 'The nginx source packages download was defeated,please check.' else echo 'The nginx source packages was downloaded.' fi } install () { tar zxvf $DIR4 ;cd $DIR5 &&./configure --prefix=$DIR2 &&make &&make install if [ $? -ne 0 ];then echo 'The nginx insall was defeated,please check.' else echo 'The nginx was insatlled.' fi } start () { /usr/local/nginx/sbin/nginx &&ps -ef |grep nginx |grep -v grep } stop () { pkill nginx } download &&install &&start &&stop 执行该脚本过程中如无报错,说明我们的nginx安装成功。
三、安装配置keepalived
在105跟106上执行该脚本,内容如下:
#!/bin/bash ##auto install keepalived shell scripts. ##li 2013-03-16 ##define PATH DIR1=/usr/local/src/ DIR2=/usr/local/ DIR3=keepalived-1.2.6.tar.gz DIR4=keepalived-1.2.6 DIR5=/usr/src/kernels/2.6.32-220.e16.x86_64/ if [ $UID -ne 0 ];then echo 'The shell scripts user must be root or administrator,please change user.' sleep 3 exit 0 fi download () { cd $DIR1 ;wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz if [ $? -ne 0 ];then echo 'Download keepalived source package was failed,please check network or download state.' else echo 'Download keepalived source package successfully.' fi } install () { tar zxvf $DIR3 &&cd $DIR4 &&./configure --with-kernel-dir=$DIR5 &&make &&make install if [ $? -ne 0 ];then echo 'Install keepalived was failed,please check.' else echo 'Install keepalived successfully.' fi } configure_keepalived () { cp $DIR2/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ &&cp $DIR2/etc/sysconfig/keepalived /etc/sysconfig/ &&cp $DIR2/sbin/keepalived /sbin/ &&mkdir -p /ect/keepalived.conf if [ $? -ne 0 ];then echo 'Keepalived configure was failed,please check.' else echo 'Keepalived configure successfully.' fi }
PS3="Please insert select Menu:" select i in "download" "install" "configure_keepalived" do $i done (添加这五行,在执行脚本过后会随后根据选择的选项去执行下一步动作,方便只下载等说什么的单一动作,执行完成后按 alt+c 退出)
执行过程中无报错,则说明keepalived安装成功。下一步,新建keepalived配置文件keepalived.conf 内容如下:
! Configuration File for keepalived
global_defs {
notification_email {
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/data/sh/check_nginx.sh"
interval 2
weight 2
}
# VIP1
vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151
priority 100
advert_int 5
nopreempt
authentication {
auth_typePASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200
}
track_script {
chk_nginx
}
}
在106上的配置文件/etc/keepalived/keepalived上修改参数:state为BACKUP模式、优先级priority 为90(低于MASTER的优先级即可),根据上面的配置,我们建立一个check_nginx.sh脚本,用来查看nginx是否存活来切换nginx服务器。check_nginx.sh脚本内容如下:
#!/bin/bash
##auto check nginx process
##li 2013-03-16
killall -0 nginx
if
[ $? -ne 0 ];then
/etc/init.d/keepalived stop
fi
五、启动测试nginx+keepalived高可用
在105跟106上都执行以下步骤:
1.启动nginx执行: /usr/local/nginx/sbin/nginx
2.启动keepalived执行:/etc/rc.d/init.d/keepalived start
3.执行脚本: /data/sh/check_nginx.sh
在浏览器访问:192.168.1.200
因我们的105上设置的是MASTER模式,所以我们去查看105下的日志文件:
从上面可用观察出105为MASTER模式,现在我们宕掉105的nginx,观察在浏览器里面还能不能访问192.168.1.200,如果能访问且页面跟上面访问页面一样,则说明我们的nginx+keepalived搭建成功,当然我们也需要去观察105的日志文件:
以上说明:我们搭建的nginx+keepalived高可用web服务器完成。
如果你看到这,是对我最大的褒奖,希望你能给宝贵的建议。谢谢。
|