Heartbeat 项目是 Linux-HA 工程的一个组成部分,其与lvs在本质上无关。
Heartbeat提供了2个核心的功能正是lvs所需要的,心跳监测部分和资源接管,心跳监测可以通过网络链路和串口进行,而且支持冗余链路,安装了 Heartbeat 的两台机器会通过心跳检测互相检测对方的状态,当检测到对方失效的时候会调用资源接管来做接管服务器,保证高可靠性。
在一个高可靠的lvs集群中,负载调度IPVS部分一般由2台服务器组成,一台负责调度,一台负责备用,当负责调度的服务器出现问题的时候迅速切换到备用机器上,而heartbeat 就是负责检测,负载调度 IPVS 的可用性,并在出现问题的时候切换到备用 IPVS 上面。
ldirectord是专门为LVS监控而编写的,用来监控lvs架构中服务器池(server pool) 的服务器状态。
ldirectord 运行在 IPVS 节点上, ldirectord作为一个守护进程启动后会对服务器池中的每个真是服务器发送请求进行监控,如果 服务器没有响应 ldirectord 的请求,那么ldirectord 认为该服务器不可用, ldirectord 会运行 ipvsadm 对 IPVS表中该服务器进行删除,如果等下次再次检测有相应则通过ipvsadm 进行添加。
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现 。IPVS通常与keepalived配合使用,后者也是LVS项目的子项目之一,用于检测服务器的状态。
在lvs体系中,Keepalived主要有如下3个功能:
1 管理LVS负载均衡软件
2 实现对LVS集群节点的健康检查功能
3 作为系统网络服务的高可用功能
即 Keepalived 实现了 heartbeat + ldirectord 的功能。
第一种集群方式 : LVS+heartbeat+ldirectord实现集群负载
第二种集群方式:LVS+KeepAlived集群负载
本文主要介绍 第二种集群方式:LVS+KeepAlived集群负载
Server1:172.25.51.1
Server2:172.25.51.2
Server3:172.25.51.3
物理机:172.15.51.250
<1>添加yum源完整的安装包
[root@server1 ~]# vim /etc/yum.repos.d/rhel-source.repo
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.51.250/rhel6.5
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[LoadBalancer]
name=LoadBalancer
baseurl=http://172.25.51.250/rhel6.5/LoadBalancer
gpgcheck=0
[HighAvailability]
name=HighAvailability
baseurl=http://172.25.51.250/rhel6.5/HighAvailability
gpgcheck=0
[ResilientStorage]
name=ResilientStorage
baseurl=http://172.25.51.250/rhel6.5/ResilientStorage
gpgcheck=0
[ScalableFileSystem]
name=ScalableFileSystem
baseurl=http://172.25.51.250/rhel6.5/ScalableFileSystem
gpgcheck=0
示图1:6.5版本yum源完整的安装包
示图2:物理机查看版本6.5镜像yum源完整的安装包
<2>安装管理集群服务ipvsadm
[root@server1 ~]# yum install ipvsadm -y
<3>临时添加ip,并设置为lvs的vip
rr:表示论询算法,-t:TCP,-s:sheduler
[root@server1 ~]# ip addr add 172.25.51.100/24 dev eth0 ##临时添加ip
[root@server1 ~]# ipvsadm -A -t 172.25.51.100:80 -s rr
示图1:添加ip后
示图2:查询ipvsadm状态
<4>vip添加RS地址,并设置为DR模式
-r:RS地址,-g:DR模式
[root@server1 ~]# ipvsadm -a -t 172.25.51.100:80 -r 172.25.51.2:80 -g
[root@server1 ~]# ipvsadm -a -t 172.25.51.100:80 -r 172.25.51.3:80 -g
[root@server1 ~]# /etc/init.d/ipvsadm save
<1>修改Apache服务测试页内容,并开启httpd服务
[root@server2 ~]# vim /var/www/html/index.html
示图:本机测试
<2>Realserver同样添加虚拟ip地址,与调度器虚拟ip地址一致
[root@server2 ~]# ip addr add 172.25.51.100/32 dev eth0
<3>安装arp防火墙
arp防火墙用来屏蔽数据包,拦截ip冲突,因为虚拟机与real server的地址一致,所以进入real server时需要把虚拟ip DROP掉,即拦截real server的虚拟ip
[root@server2 ~]# yum install arptables_jf -y
<4>设置arp抑制
[root@server2 ~]# arptables -A IN -d 172.25.51.100 -j DROP ##拒绝客户端访问realserver的虚拟ip,只要输入进来直接DROP掉
[root@server2 ~]# arptables -A OUT -s 172.25.51.100 -j mangle --mangle-ip-s 172.25.51.2 ##由于tcp三次握手,出去时仍要以vip地址形式才会握手,而真正将数据传输给客户端的就是realserver,mangle参数就是这个功能
[root@server2 ~]# /etc/init.d/arptables_jf save
<1>修改Apache服务测试页内容,并开启httpd服务
[root@server3 ~]# vim /var/www/html/index.html
示图:本机测试
<2>Real server同样添加虚拟ip地址,与调度器虚拟ip地址一致
[root@server2 ~]# ip addr add 172.25.51.100/32 dev eth0
<3>安装arp防火墙
arp防火墙用来屏蔽数据包,拦截ip冲突,因为虚拟机与real server的地址一致,所以进入real server时需要把虚拟ip DROP掉,即拦截real server的虚拟ip
[root@server3 ~]# yum install arptables_jf -y
<4>设置arp抑制
[root@server3 ~]# arptables -A IN -d 172.25.51.100 -j DROP ##拒绝客户端访问realserver的虚拟ip,只要输入进来直接DROP掉
[root@server3 ~]# arptables -A OUT -s 172.25.51.100 -j mangle --mangle-ip-s 172.25.51.3 ##由于tcp三次握手,出去时仍要以vip地址形式才会握手,而真正将数据传输给客户端的就是realserver,mangle参数就是这个功能
[root@server3 ~]# /etc/init.d/arptables_jf save
示图1:物理机访问172.25.51.100
示图2:server1查看ipvsadm状态,server2、3负载均衡
示图:arp缓冲区中MAC地址与server1的MAC地址相同
说明lvs服务OK,浮动IP实现负载均衡 client -> vs -> rs -> client
<1>下载并安装监控软件
lftp 172.25.51.250:/pub> get ldirectord-3.9.5-3.1.x86_64.rpm
[root@server1 ~]# yum install ldirectord-3.9.5-3.1.x86_64.rpm
<2>修改配置文件
[root@server1 ~]# rpm -qpl ldirectord-3.9.5-3.1.x86_64.rpm ##查找配置文件
[root@server1 ~]# cd /etc/ha.d/
[root@server1 ha.d]# ls
resource.d shellfuncs
[root@server1 ha.d]# cp /usr/share/doc/ldirectord-3.9.5/ldirectord.cf .
[root@server1 ha.d]# ls
ldirectord.cf resource.d shellfuncs
[root@server1 ha.d]# vim ldirectord.cf ##修改配置文件
virtual=172.25.51.100:80
real=172.25.51.2:80 gate
real=172.25.51.3:80 gate
fallback=127.0.0.1:80 gate ##server2、3服务down后,访问本机80端口
service=http
scheduler=rr ##调度模式:轮询
#persistent=600
#netmask=255.255.255.255
protocol=tcp
checktype=negotiate
checkport=80 ##端口
request="index.html" ##文件
# receive="Test Page"
# virtualhost=www.x.y.z
[root@server1 ha.d]# /etc/init.d/ldirectord start 开启服务
[root@server1 ha.d]# vim /var/www/html/index.html
此网站正在维护中...
<4>监听端口80
[root@server1 html]# vim /etc/httpd/conf/httpd.conf
136 Listen 80
[root@server1 html]# /etc/init.d/httpd start
<5>Real Server主机关闭http服务,Virtual Server主机
示图1:测试本机
示图2:查看ipvsadm状态
[root@server2 ~]# /etc/init.d/httpd stop
[root@server3 ~]# /etc/init.d/httpd stop
测试:物理机
注意: 当安装 php 模块后,默认优先读取 index.php,应将index.php删除,才可读取Index.html.
<1>停止 ldirectord 服务
[root@server1 ~]# /etc/init.d/ldirectord stop
[root@server1 ~]# chkconfig ldirectord off
<2>下载并安装keepalived
lftp 172.25.51.250:/pub> get keepalived-1.4.3.tar.gz
[root@server1 ~]# tar zxf keepalived-1.4.3.tar.gz
[root@server1 ~]# cd keepalived-1.4.3
[root@server1 keepalived-1.4.3]# ./configure --prefix=/usr/local/keepalived --with-init=SYSV
[root@server1 keepalived-1.4.3]# make && make install
注意:compile报错时,解决依赖性, 安装 openssl-devel
[root@server1 ~]# yum install openssl-devel.x86_64 -y
<3>配置keepalived服务
[root@server1 ~]# cd /usr/local/keepalived/etc/rc.d/init.d/
[root@server1 init.d]# chmod +x keepalived
[root@server1 local]# ln -s /usr/local/keepalived/etc/keepalived/ /etc
[root@server1 local]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@server1 local]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@server1 local]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/
[root@server1 init.d]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
# vrrp_strict ##注释
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 12 ##避免冲突
priority 100 ##优先级100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.51.100
}
}
virtual_server 172.25.51.100 80 {
delay_loop 3
lb_algo rr
lb_kind DR
# persistence_timeout 50 ##注释
protocol TCP
real_server 172.25.51.2 80 {
weight 1
SSL_GET {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 172.25.51.3 80 {
weight 1
SSL_GET {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
<4>scp配置文件给server4
[root@server1 ~]# scp -r /usr/local/keepalived/ server4:/usr/local/
[root@server1 ~]# scp -r /etc/keepalived/keepalived.conf server4:/etc/keepalived/
注意:
server1和server4必须同时安装scp服务
[root@server1 ~]# yum provides */scp ##查找服务所在安装包
[root@server1 ~]# yum install openssh-clients-5.3p1-94.el6.x86_64 -y
[root@server1 ~]# /etc/init.d/keepalived start
[root@server1 ~]# /etc/init.d/keepalived reload
<1>配置完整的yum源
vim /etc/yum.repos.d/rhel-source.repo
<2>安装服务
[root@server4 ~]# yum install -y ipvsadm
[root@server4 ~]# yum install openssh-clients-5.3p1-94.el6.x86_64 -y
<3>配置keepalived服务
[root@server4 ~]# ln -s /usr/local/keepalived/etc/keepalived/ /etc/
[root@server4 ~]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@server4 ~]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@server4 ~]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/
[root@server4 ~]#cd /usr/local/keepalived/etc/rc.d/init.d
[root@server4 init.d]# chmod +x keepalived
[root@server4 init.d]# vim /etc/keepalived/keepalived.conf ##主要修改以下部分
vrrp_instance VI_1 {
state BACKUP ##备用
interface eth0
virtual_router_id 12
priority 50 ##优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.51.100
}
}
<4>重启服务
[root@server4 ~]# /etc/init.d/keepalived start
[root@server4 ~]# /etc/init.d/keepalived reload
[root@server2 ~]# /etc/init.d/httpd start
[root@server3 ~]# /etc/init.d/httpd start
实验时,可删除vip,停止keepalived服务,停止网络服务,刷掉内核
注意:手动删除vip时,keepalived服务失效
server1主机keepalived服务关闭时: (切换到server4主机,备用,显示server4的Mac地址)
[root@server1 ~]# echo c > /proc/sysrq-trigger #故意让系统崩溃