如果后端真实服务器出现问题,那么在测试端测试的时候,会返回给我们一个错误的页面,那么我们需要对后端服务器做健康检查,保证只返回正确的页面提供给用户。
实验环境: rhel6.5 selinux and iptables disabled
主机环境:
主机名IPhetoto1172.25.12.1hetoto2172.25.12.2hetoto3172.25.12.3 Load Balance: 172.25.12.1(hetoto1)Virtual IP:172.25.12.100RealRerver1:172.25.12.2 (hetoto2)RealRerver2:172.25.12.3(hetoto3)物理机内网 : 172.25.12.250配置hetoto2 1.安装apache
[root@hetoto2 ~]# yum install httpd -y2.编写前端文件
[root@hetoto2 ~]# cd /var/www/html [root@hetoto2 html]# vim index.html <h1>www.westos.org - hetoto2</h1>配置hetoto3:同上
[root@hetoto3 ~]# cd /var/www/html [root@hetoto3 html]# vim index.html <h1>www.westos.org - hetoto3</h1>1.安装软件ldirectord-3.9.5-3.1.x86_64.rpm用来对后端服务器做健康检查:
[root@hetoto1 ~]# yum install -y ldirectord-3.9.5-3.1.x86_64.rpm 查看配置文件 [root@hetoto1 ~]# 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.gz2.配置文件的母板拷到/etc/ha.d/
[root@hetoto1 ~]# cd /etc/ha.d/ [root@hetoto1 ha.d]# cp /usr/share/doc/ldirectord-3.9.5/ldirectord.cf . [root@hetoto1 ha.d]# ls ldirectord.cf resource.d shellfuncs3.编辑 ldirectord的配置文件
[root@server1 ha.d]# vim ldirectord.cf # Global Directives #“ 全局 ” 设置 checktimeout=3 #指定定real server出错的时间间隔 checkinterval=1 #指定ldirectord在两次检查之间的时间间隔 autoreload=yes #自动重载配置文件,选yes时,当配置文件发生变化,自动载入配置信息 quiescent=no #当一个节点在 checktimeout 设置的时间周期内没有响应是它是 “ 静止的 ” (它的权重为0),当你设置了这个选项后,ldirectord 将 会从 IPVS 表中移除真实服务器而不是 “ 停止 ” 它,从 IPVS 表移除节点将中断现有的客户端连接,并使 LVS 丢掉所有的连接跟踪记录和持续连接模板,如果 你不将这个选项设置为 no,当某个节点崩溃时,对某些客户端计算机而言可能会显示为集群关闭了,因为在这个节点崩溃前这些客户端计算机被分配给它了,而连接跟踪记录和程序连接模板仍然保留在Director上。 logfile="/var/log/ldirectord.log" #设定ldirectord日志输出文件路径 # Sample for an http virtual service virtual=172.25.12.100:80 #VIP 地址和端口号 real=172.25.12.2:80 gate #指定RealServer地址和端口,同时设定LVS工作模式,gate表示DR模式,ipip表示TUNL模式,masq表示NAT模式。 real=172.25.12.3:80 gate #当所有的real server节点不能工作时,web服务重定向的地址 fallback=127.0.0.1:80 gate #当所有的real server节点不能工作时,web服务重定向的地址 service=http #指定服务类型,对http服务做负载均衡 scheduler=rr #指定调度算法,这里是rr(轮叫)算法 #persistent=600 #netmask=255.255.255.255 protocol=tcp #指出该服务使用的协议:tcp、udp 或 fwm checktype=negotiate #指定Ldirectord的检测类型,默认为negotiate checkport=80 #健康检查使用的端口是 80 request="index.html" #receive="Test Page" #指定请求和应答字串 #virtualhost=www.x.y.z #虚拟服务器的名称 开启服务 [root@hetoto1 ha.d]# /etc/init.d/ldirectord start Starting ldirectord... success4.物理机 测试:
后端真实服务器都正常时(RealServer 无宕机 ):实行轮叫 将hetoto3关掉(RealServer 有一台宕机) 则显示只调度hetoto2服务器RealServer 全部宕机
1.将hetoto2,和hetoto3的httpd服务都关掉
[root@hetoto2 html]# /etc/init.d/httpd stop [root@hetoto3 html]# /etc/init.d/httpd stop2.在调度器hetoto1上编写前端文件
[root@hetoto1 ~]# yum install httpd -y [root@hetoto1 html]# cd /var/www/html [root@hetoto1 html]# vim index.html <h1>网站维护中..</h1>3.hetoto1的httpd服务端口改成80
[root@hetoto1 html]# vim /etc/httpd/conf/httpd.conf Listen 80 [root@hetoto1 html]# /etc/init.d/httpd restart4.重启ldirectord 服务
[root@hetoto1 ha.d]# /etc/init.d/ldirectord restart Restarting ldirectord... success5.物理机测试