Lvs+keepalived能实现的功能: 利用lvs控制器主备模式避免单点故障以及自动删除故障WEB服务器结点并当它恢复后再自动添加到群集中。
拓扑图:
IP规划:
1、在LVS SRV上安装lvs、keepalived软件(LVS-MASTER & LVS-BACKUP)
lvs install--------------------------------
[root@LVS-MASTER ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
[root@LVS-MASTER ~]# ln -s /usr/src/kernels/2.6.18-238.el5-i686/ /usr/src/linux/
[root@LVS-MASTER ~]# tar zxvf ipvsadm-1.24.tar.gz
[root@LVS-MASTER ~]# cd ipvsadm-1.24
[root@LVS-MASTER ipvsadm-1.24]# make && make install
Keepalived install------------------------------------
[root@LVS-MASTER ~]# wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
[root@LVS-MASTER ~]# tar zxvf keepalived-1.1.15.tar.gz
[root@LVS-MASTER ~]# cd keepalived-1.1.15
[root@LVS-MASTER keepalived-1.1.15]# ./configure && make && make install
######### 将keepalived做成启动服务,方便管理 ##########
[root@LVS-MASTER ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[root@LVS-MASTER ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@LVS-MASTER ~]# mkdir /etc/keepalived/
[root@LVS-MASTER ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@LVS-MASTER ~]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@LVS-MASTER ~]# service keepalived start | stop
2、keepalived的配置
============================================================================
################### MASTER ######################
============================================================================
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
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 {
172.16.201.188
}
}
virtual_server 172.16.201.188 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 172.16.201.211 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 172.16.201.212 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
============================================================================
######################### BACKUP ######################
============================================================================
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.201.188
}
}
virtual_server 172.16.201.188 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 172.16.201.211 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 172.16.201.212 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
3、在realserver SRV上执行realserver.sh脚本,为lo:0绑定VIP地址172.16.201.188、抑制ARP广播。
4、分别在LVS-MASTER、LVS-BACKUP SRV上执行service keepalived start启动keepalived就可实现负载均衡及高可用集群了;
[root@LVS-MASTER ~]# service keepalived start
日志信息:
�D�D�D�D�D�D�D�D�D�D�D�D�D�D高可用性测试�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D
(为了模拟故障,将LVS-MASTER上的keepalived服务停掉,然后观察LVS-BACKUP SRV上的日志,信息如下…)
(从日志中可知,主机出现故障后,备机立刻检测到,此时备机变为MASTER角色,并且接管了主机的虚拟IP资源,最后将虚拟IP绑定在etho设备上。)
将LVS-MASTER 上的keepalived服务开启后,LVS-BACKUP的日志状态。
(从日志可知,备机在检测到主机重新恢复正常后,释放了虚拟IP资源重新成为BACKUP角色)
�D�D�D�D�D�D�D�D�D�D�D�D�D故障切换测试�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D
----故障切换是测试当某个节点出现故障后,Keepalived监制模块是否能及时发现然后屏蔽故障节点,同时将服务器转移到正常节点来执行。
将REAL-SERVER2节点服务停掉,假设这个节点出现故障,然后主、备机日志信息如下…….
(从以上可以看出,Keepalived监控模块检测到172.16.201.212这台主机出现故障后,将些节点2从集群系统中剔除掉了。 此时访问http://172.16.201.188 只能看到“This is realserver1 !!!”了)
重新启动REAL-SERVER2节点的服务,日志信息如下:
(Keepalived监控模块检测到172.16.201.212这台主机恢复正常后,又将此节点加入集群系统中,再次访问就可以访问到“This is realserver2 !!!”页面了, 嘿嘿……..)