随着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
三、安装配置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 退出)
! Configuration File for keepalived
global_defs {
notification_email {
lifenlun163@163.com
}
notification_email_from lifenlun163@163.com
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
}
}