随着Nginx在国内的发展潮流,越来越多的互联网公司都在使用Nginx。
Nginx高性能、稳定性成为IT人士青睐的http和反向代理服务器,今天我们来实战构建Nginx+Keepalived高可用架构配置,如下为网络架构图:
一、环境准备:
系统环境:CentOS 6.5 x86_64
Nginx版本:nginx v1.6.2
Keepalived版本:keepalived v1.2.1
Nginx-1:192.168.33.8 (Master)
Nginx-2:192.168.33.10 (Backup)
二、Nginx安装配置:
分别在两台服务器安装Nginx、keepalived,如下:
yum install -y pcre-devel 安装perl 兼容的正规表达式库
tar -xzf nginx-1.6.2.tar.gz && cd nginx-1.6.2 ; sed -i -e ‘s/1.6.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
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.168.33.188
}
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.168.33.188能出现界面即可。