Nginx高性能、稳定性成为IT人士青睐的http和反向代理服务器。今天我们来一起来温习一下Nginx+Keepalived高可用配置,鉴于互联网类似的优秀文章很多,这里仅供参考和交流。
一、实验环境:
系统环境:CentOS 6.0 x86_64
Nginx版本:nginx v1.2.4
Keepalived版本:keepalived v1.2.1
Nginx-1:192.9.117.160 (Master)
Nginx-2:192.9.117.161 (Backup)
二、Nginx安装配置:
分别在两台服务器安装Nginx、keepalived,如下:
yum install -y pcre-devel 安装perl 兼容的正规表达式库
tar -xzf nginx-1.2.4.tar.gz && cd nginx-1.2.4 ; sed -i -e 's/1.2.2//g' -e 's/nginx//TDTWS/g' -e 's/"NGINX"/"TDTWS"/g' src/core/nginx.h &&./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
三、Keepalived安装配置:
tar -xzvf keepalived-1.2.1.tar.gz &&cd keepalived-1.2.1 && ./configure && make && make install
if
[ $? -eq 0 ];then
echo "Install keepalived success,please waiting configure keepalived ..............."
else
echo "Install keepalived failed ,please check install version !"
exit 0
fi
DIR=/usr/local/ ;cp $DIR/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ ; cp $DIR/etc/sysconfig/keepalived /etc/sysconfig/ ; mkdir -p /etc/keepalived ; cp $DIR/sbin/keepalived /usr/sbin/
Nginx、Keepalived软件安装完毕,接下来进行详细配置。
四、配置Keepalived:
两台服务器端keepalived.conf内容都为如下,都设置为backup,不抢占,注意修改优先级不同:
! 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 LVS_DEVEL
}
vrrp_script chk_nginx {
script "/data/sh/check_nginx.sh"
interval 2
weight 2
}
# VIP1
vrrp_instance VI_1 {
state BACKUP
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.9.117.162
}
track_script {
chk_nginx
}
}
如上配置,我们需要自己建立check_nginx脚本,以方便检查本地Nginx是否存活更好的切换。Check_nginx.sh脚本内容如下:
#!/bin/bash
#auto check nginx process
#2012-10-16 wugk
killall -0 nginx
if
[[ $? -ne 0 ]];then
/etc/init.d/keepalived stop
fi
五、Nginx 配置:
在两台Nginx服务器分别新建index.html测试页面,然后启动Nginx服务测试。
访问VIP http://192.9.117.162 如下:
Down掉192.9.117.160的Nginx服务,分别查看keepalived后台日志和ping VIP日志如下:
Oct 16 16:47:48 localhost Keepalived: Stopping Keepalived v1.2.1 (10/16,2012)
Oct 16 16:47:49 localhost Keepalived_vrrp: Terminating VRRP child process on signal
至此,Nginx+Keepalived高可用WEB架构搭建成功!目前此套系统已经在线上使用,后期会观察使用情况并分享给大家,欢迎多交流!
声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。