lvs+keepalived

时间:2020-11-28 15:32:26

一、简介

VS/NAT原理图:

lvs+keepalived

二、系统环境

实验拓扑:

lvs+keepalived

系统平台:CentOS 6.3

Kernel:2.6.32-279.el6.i686

LVS版本:ipvsadm-1.26

keepalived版本:keepalived-1.2.4

三、安装

0、安装LVS前系统需要安装popt-static,kernel-devel,make,gcc,openssl-devel,lftp,libnl*,popt*

1、在两台Director Server上分别配置LVS+Keepalived

LVS install -------------

[root@CentOS-LVS_MASTER ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz

[root@CentOS-LVS_MASTER ~]# ln -s /usr/src/kernels/2.6.32-279.el6.i686//usr/src/linux/

[root@CentOS-LVS_MASTER ~]# tar zxvf ipvsadm-1.26.tar.gz

[root@CentOS-LVS_MASTER ~]# cd ipvsadm-1.26

[root@CentOS-LVS_MASTER ipvsadm-1.26]# make && make install

Keepalived install -------------

[root@CentOS-LVS_MASTER ~]# wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz

[root@CentOS-LVS_MASTER ~]# tar zxvf keepalived-1.2.4.tar.gz

[root@CentOS-LVS_MASTER ~]# cd keepalived-1.2.4

[root@CentOS-LVS_MASTER keepalived-1.2.4]# ./configure && make && make install

######### 将keepalived做成启动服务,方便管理##########

[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/

[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

[root@CentOS-LVS_MASTER ~]# mkdir /etc/keepalived/

[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

[root@CentOS-LVS_MASTER ~]# cp /usr/local/sbin/keepalived /usr/sbin/

[root@CentOS-LVS_MASTER ~]# service keepalived start | stop

2、开启路由转发

[root@CentOS-LVS_MASTER ~]# vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1
[root@CentOS-LVS_MASTER ~]# sysctl -p

3、配置Keepalived

[root@CentOS-LVS_MASTER ~]# less /etc/keepalived/keepalived.conf


! Configuration File for keepalived
global_defs {
# notification_email {
# acassen@firewall.loc
# failover@firewall.loc
# sysadmin@firewall.loc
# }
# notification_email_from Alexandre.Cassen@firewall.loc
# smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id lvs2
}
vrrp_script check_arp {
script "/etc/keepalived/check_arp.sh"
interval 60
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
mcast_src_ip 10.46.109.163
garp_master_delay 5
nopreempt
virtual_router_id 179
priority 50
advert_int 3
authentication {
auth_type PASS
auth_pass ykt@179
}
track_script {
check_arp
}
virtual_ipaddress {
10.46.109.179/27 dev eth0
}
notify_master /etc/keepalived/to_master.sh
notify_backup /etc/keepalived/to_backup.sh
}
virtual_server 10.46.109.179 8080 {
delay_loop 30
lb_algo wlc
lb_kind NAT
persistence_timeout 30
protocol TCP
ha_suspend
real_server 10.46.109.164 8080 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 10.46.109.165 8080 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

BACKUP服务器同上配置,先安装lvs再安装keepalived,然后配置/etc/keepalived/keepalived.conf,只需将批注部分改一下即可。

4、分别在2台Real Server上面设置网关

把网关都设置成:192.168.10.10

5、在2台RealServer中分别配置HTTP

[root@WEB1 ~]# yum -y install httpd
[root@WEB1 ~]# cd /var/www/html/
[root@WEB1 html]# cat index.html 
<h1>WEB1/192.168.10.4</h1>
[root@WEB1 html]# /etc/init.d/httpd start
另一台机器配置一样,过程略。

6、分别在CentOS-LVS_MASTER、CentOS-LVS_BACKUP上执行service keepalived start启动keepalived就可实现负载均衡及高可用集群了;

[root@CentOS-LVS_MASTER keepalived]# service keepalived start

lvs+keepalived

lvs+keepalived

四、测试

####高可用性测试####

模拟故障,将CentOS-LVS_MASTER上的keepalived服务停掉,然后观察CentOS-LVS_BACKUP上的日志,信息如下

lvs+keepalived

从日志中可知,主机出现故障后,备机立刻检测到,此时备机变为MASTER角色,并且接管了主机的虚拟IP资源,最后将虚拟IP绑定在etho设备上。

将CentOS-LVS_MASTER 上的keepalived服务开启后,CentOS-LVS_BACKUP的日志状态。

lvs+keepalived

从日志可知,备机在检测到主机重新恢复正常后,释放了虚拟IP资源重新成为BACKUP角色

####故障切换测试####

故障切换是测试当某个节点出现故障后,Keepalived监制模块是否能及时发现然后屏蔽故障节点,同时将服务器转移到正常节点来执行。

将WEB2节点服务停掉,假设这个节点出现故障,然后主、备机日志信息如下

lvs+keepalived

lvs+keepalived

从以上可以看出,Keepalived监控模块检测到192.168.10.5这台主机出现故障后,将WEB2从集群系统中剔除掉了。 此时访问http://10.0.0.227只能看到WEB1了)

lvs+keepalived

重新启动WEB2节点的服务,日志信息如下:

lvs+keepalived

lvs+keepalived

Keepalived监控模块检测到192.168.10.5这台主机恢复正常后,又将此节点加入集群系统中,再次访问就可以访问到WEB2页面了)

lvs+keepalived

修改网关:

vi /etc/sysconfig/network-scripts/route-eth0