Web+Keepalived的高可用
在LVS+keepalived中,我们通过检测网卡的存活状态来判断KVS是否可用,也就是说,keepalived认为,网卡活着,则LVS活着(能够正常工作),因为LVS是集成在Linux的内核中的,只有Linux系统崩了,LVS才会挂掉,同时网卡也就不能正常使用了,所以说,LVS中探测网卡是完全没有问题的。但是在Web服务的高可用中则不然,因为Web服务远远没有Linux系统稳定,所以检测网卡的话是不能进行Web服务的存活状态进行有效探测的,因此在生产中,往往在eepalived中使用脚本来探测Web服务是否还活着,如果挂掉,就会启动备用节点。
环境准备
这里使用的是Apache
192.168.4.116(master)
192.168.4.115(backup)
安装Apache
两台安装完全一样
yum install -y httpd
service httpd start
echo "192.168.4.115" > /var/www.html/index.html #方便区分两台机器
安装Keepalived
master节点和backup节点一样
1.安装依赖
yum -y install kernel-devel openssl-devel popt-devel gcc*
2.安装keepalived
tar xf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-642.el6.x86_64/
make && make install
修改配置文件
master节点
vim /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.55
}
}
#下边的都删除,因为那个是LVS探测真实服务器的存活状态的区域,这里不需要
检测master节点httpd的脚本
/usr/local/script/http_check.sh
#!/bin/bash
URL="http://192.168.4.116/index.html"
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`
if [ $HTTP_CODE != 200 ]
then
p
/etc/init.d/keepalived stop
fi
添加到定时任务
当检测不到Apache还活着是,关闭keepalived服务,则VIP就漂移到了backup节点上
* * * * * /bin/sh /usr/local/script/http_check.sh #将检测Apache的存活状态
backup节点
vim /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.55
}
}