利用keepalived+nginx实现官网web的高可用性
两台服务器一台为主,一台为备机,使两台机器公用一个虚拟IP,当主服务器宕机,这个IP会自动切换到备机上,前端机恢复之后再自动切换回主服务机。
主和辅都需要安装keepalived,安装方法相同
yum -y install kernel-devel
yum install openssl-* -y
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar xzf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/usr/local/keepalived
make && make install
ln -s /usr/src/kernels/2.6.32-431.23.3.el6.x86_64/ /usr/src/linux #注意系统位数
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir -pv /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
加入开机启动:chkconfig keepalived on
二、配置
主和辅配置的差别只在stateMASTER和priority 100
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
# notification_email { ####此处定义发生替换会邮件通知
# cuimingkun@yinwuweiye.com
# }
# notification_email_from houzaicunsky@qq.com
# smtp_server 127.0.0.1
# smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/root/shell/nginx_pid.sh" ##监控脚本位置
interval 2
weight 2
}
track_script {
chk_http_port
}
vrrp_instance VI_1 {
state MASTER #####备机为BACKUP
interface eth0
virtual_router_id 51
priority 100 #####备机要小于主
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.68 ####虚拟IP
}
}
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
nginx的 配置这里就不写了配置太多了!
如果是双网卡可以参考:http://blog.csdn.net/jiedushi/article/details/6097920
本文转自 houzaicunsky 51CTO博客,原文链接:http://blog.51cto.com/hzcsky/1542154