Nginx+keepalive主从 双机热备 + 自动切换解决方案
环境采集cenots 6.3 64位迷你安装,因为安装前,你需要做一些工作
yum install -y make wget
如果你愿意可以更新下系统,更换下yum源.
1.安装keepalive
官方最新版 keepalived-1.2.7
tar zxvf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
在此之前。请安装一下一些简单的工具
yum install -y gcc openssl-devel popt-devel
不然会编译不成功的。
然后
./configure
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived
chkconfig keepalived on
mkdir /etc/keepalived
ln -s /usr/local/sbin/keepalived /usr/sbin/
2.安装nginx
nginx-1.2.5.tar.gz
tar zxvf nginx-1.2.5.tar.gz
cd nginx-1.2.5
安装一下相关组件.
yum install -y pcre-devel
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
make && make install
3.配置keepalive
两台服务器端keepalived.conf内容都为如下,都设置为backup,不抢占,注意修改优先级不同,更详细的keepalived配置文件说明可以执行man keepalived.conf查看:
! Configuration File for keepalived
global_defs {
notification_email {
admin@lvtao.net
}
notification_email_from admin@lvtao.net
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#监控服务.NGINX mysql等
vrrp_script chk_nginx {
script "/home/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP #主从设置 MASTER
interface eth2 #网卡名
virtual_router_id 51
mcast_src_ip 10.0.1.133 #本机IP
priority 50 #从机小于主机
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
10.0.1.2 #VIP 的IP
}
track_script {
chk_nginx #检测脚本
}
}
virtual_server 10.0.1.2 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.0.1.132 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.0.1.133 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
启动相关服务。我在这儿使用的是nginx ,每个上面开了一个站点,通过IP可以直接访问的。启动keepalive后,就可以通过VIP的虚拟IP 10.0.1.2来访问站点了,测试方法就是 停止任何其中一个站点,看它是否能自动切换到从服务器上。
上面代码中 nginx的检测脚本如下 :
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/usr/local/nginx/sbin/nginx
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall keepalived
fi
fi
在两台Web Server上执行realserver.sh脚本,为lo:0绑定VIP地址10.0.1.2、抑制ARP广播。
#!/bin/bash
#description: Config realserver
VIP=10.0.1.2
/etc/rc.d/init.d/functions
case "$1" in
start)
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
分别在主从机上执行 sh realserver.sh start 就可实现负载均衡及高可用集群了;