LVS——DR模式下LVS的健康状况检查(ldirectord)
为什么要进行健康状况检查?
DR模式下的LVS调度器不会检查后端的服务器状态,导致如果后端服务器发生故障时,调度器依然正常进行调度,用户在访问的时候会看到故障,导致用户体验下降。
我们需要改进服务,保证用户在进行访问的时候,只能看到正确的输出;
主机环境:rhel6.5 selinux and iptables disabled
主机名 | ip | 服务 |
---|---|---|
lvs1 | 172.25.254.1 | ipvsadm |
lvs2 | 172.25.254.2 | apache |
lvs3 | 172.25.254.3 | apache |
- Load Balance: 172.25.254.1(lvs1)
- Virtual IP:172.25.254.100
- Real Server1:172.25.254.2 (lvs2)
- Real Server2:172.25.254.3(lvs3)
- 物理机内网 : 172.25.254.250
搭建步骤:
1.配置更高级的yum源(使其获得高可用的资源)
[[email protected] yum.repos.d]# vim rhel-source.repo
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.254.73/rhel6.5/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[LoadBalancer]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.254.73/rhel6.5/LoadBalancer
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[HighAvailability]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.254.73/rhel6.5/HighAvailability
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[[email protected] yum.repos.d]# yum clean all
[[email protected] yum.repos.d]# yum repolist
2.获得可以对后端服务器进行健康状态检查的软件包并安装,ldirectord-3.9.5-3.1.x86_64.rpm
[[email protected] ~]# ls
anaconda-ks.cfg install.log.syslog
install.log ldirectord-3.9.5-3.1.x86_64.rpm
[[email protected] ~]# yum install ldirectord-3.9.5-3.1.x86_64.rpm -y
3.查看配置文件,并将配置文件的模板文件进行拷贝
[[email protected] ~]# rpm -qpl ldirectord-3.9.5-3.1.x86_64.rpm
warning: ldirectord-3.9.5-3.1.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 7b709911: NOKEY
/etc/ha.d
/etc/ha.d/resource.d
/etc/ha.d/resource.d/ldirectord
/etc/init.d/ldirectord
/etc/logrotate.d/ldirectord
/usr/lib/ocf/resource.d/heartbeat/ldirectord
/usr/sbin/ldirectord
/usr/share/doc/ldirectord-3.9.5
/usr/share/doc/ldirectord-3.9.5/COPYING
/usr/share/doc/ldirectord-3.9.5/ldirectord.cf
/usr/share/man/man8/ldirectord.8.gz
[[email protected] ~]# cp /usr/share/doc/ldirectord-3.9.5/ldirectord.cf /etc/ha.d/
4.编辑ldirectord的配置文件
[[email protected] ~]# vim /etc/ha.d/ldirectord.cf
11 # Global Directives #“ 全局 ” 设置
12 checktimeout=3 #指定定real server出错的时间间隔
13 checkinterval=1 #指定ldirectord在两次检查之间的时间间隔
16 autoreload=yes #自动重载配置文件,选yes时,当配置文件发生变化,自动载入配置信息
17 #logfile="/var/log/ldirectord.log" #设定ldirectord日志输出文件路径
22 quiescent=no
#当一个节点在 checktimeout 设置的时间周期内没有响应时它是 “ 静止的 ” (它的权重为0),当你设置了这个选项后,ldirectord
将会从 IPVS 表中移除真实服务器而不是 “ 停止 ” 它,从 IPVS 表移除节点将中断现有的客户端连接,并使 LVS 丢掉所有的连接
跟踪记录和持续连接模板,如果 你不将这个选项设置为 no,当某个节点崩溃时,对某些客户端计算机而言可能会显示为集群关闭了,
因为在这个节点崩溃前这些客户端计算机被分配给它了,而连接跟踪记录和程序连接模板仍然保留在Director上。
25 virtual=172.25.254.100:80 #VIP 地址和端口号
26 real=172.25.254.2:80 gate #指定RealServer地址和端口,同时设定LVS工作模式,gate表示DR模式,ipip表示TUNL模式,masq表示NAT模式。
27 real=172.25.254.3:80 gate #当所有的real server节点不能工作时,web服务重定向的地址
28 fallback=127.0.0.1:80 gate #当所有的real server节点不能工作时,web服务重定向的地址
29 service=http #指定服务类型,对http服务做负载均衡
30 scheduler=rr #指定调度算法,这里是rr(轮叫)算法
31 #persistent=600
32 #netmask=255.255.255.255
33 protocol=tcp #指出该服务使用的协议:tcp、udp 或 fwm
34 checktype=negotiate #指定Ldirectord的检测类型,默认为negotiate
35 checkport=80 #健康检查使用的端口是 80
36 request="index.html"
37 #receive="Test Page" #指定请求和应答字串
38 #virtualhost=www.x.y.z #虚拟服务器的名称
5.清除手动编写的规则策略,开启ldirectord服务,让配置文件生效
[[email protected] ~]# ipvsadm -C
[[email protected] ~]# /etc/init.d/ldirectord start
[[email protected] ~]# ipvsadm -l
6.在调度器上也安装apache服务,编写默认发布页内容,方便查看测试效果
[[email protected] ~]# yum install httpd -y
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# vim index.html
[[email protected] html]# cat index.html
lvs1
[[email protected] html]# /etc/init.d/httpd start
测试:
后端所有服务器都正常时,无宕机情况
将lvs3的httpd服务关闭,模拟后端服务器lvs3宕机(此时只有lvs2被调度)
将lvs2、lvs3的httpd服务都关闭,模拟后端服务器全部宕机(此时lvs1自身参与调度)