基于ldirectord的高可用性群集实现http服务
Ldirectord是个服务用于实现后方真实的服务器的健康状态探测,其配置文件中定义虚拟服务规则、探测页面、哪种服务,当后方的服务器失效,在节点上的虚拟服务规则将会失效。
实验的ip地址规划:
案例ip地址规划
虚拟ip地址为192.168.20.100
节点一的eth0的ip地址为192.168.20.10,eth1ip地址为192.168.30.10
节点二的eth0的ip地址为192.168.20.20,eth1ip地址为192.168.30.20
Realserver1的ip地址为192.168.20.30
Realserver2的ip地址为192.168.20.40
实验的拓扑图:实现http服务
在节点上的相关配置
打开配置文件Ldirectord.cf
checkinterval=1
#fallback=127.0.0.1:80
autoreload=yes
#logfile="/var/log/ldirectord.log"
#logfile="local0"
#emailalert="[email protected]"
#emailalertfreq=3600
#emailalertstatus=all
quiescent=no当检测到后方的服务器失效时,节点上虚拟规则将会失效
#Sampleforanhttpvirtualservice
virtual=192.168.20.100:80
real=192.168.20.30:80gate后方真正的服务器
real=192.168.20.40:80gate
service=http
request=".test.html"#ldirectord发送http请求请求后方服务器的测试页面的内容,.test.html是测试页面
receive="ok"#请求返回的内容
scheduler=rr#虚拟服务的算法为轮训机制
#persistent=600
#netmask=255.255.255.255
protocol=tcp#基于tcp的四层协议
checktype=negotiate
checkport=80#检查端口
打开ha.cf这个文件,ha.cf是高可用性群集的配置文件
bcasteth1#Linux广播地址为eth:0这个端口,当主节点失效时通过这个端口把资源(vip、heartbeat服务控制的其他服务、其他服务的配置文件)传到备份节点上。
#bcasteth1eth2#Linux
#nodekathy
nodenode1.a.com
nodenode2.a.com
打开resource文件定义资源(虚拟服务ip端口服务配置文件)
#node-nameresource1resource2...resourceN
node1.a.com192.168.20.100/24/eth0ldirectord::ldirectord.cf
打开authkeys文件
authkeys
auth3#节点之间传递资源时要进行验证,MD5身份验证验证内容是redhat
#1crc
#2sha1HI!
3md5redha
[[email protected]]#serviceheartbeatstart
logdisalreadyrunning
StartingHigh-Availabilityservices:
2013/10/04_18:08:24INFO:RunningOK
2013/10/04_18:08:24CRITICAL:Resource192.168.20.100/24/eth0isactive,andshouldnotbe!
2013/10/04_18:08:24CRITICAL:Non-idleresourcescanaffectdataintegrity!
[[email protected]]#ifconfig
eth0Linkencap:EthernetHWaddr00:0C:29:FA:2D:49
inetaddr:192.168.20.10Bcast:192.168.20.255Mask:255.255.255.0
inet6addr:fe80::20c:29ff:fefa:2d49/64Scope:Link
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
RXpackets:10176errors:0dropped:0overruns:0frame:0
TXpackets:7179errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:1000
RXbytes:3123678(2.9MiB)TXbytes:962204(939.6KiB)
Interrupt:19Baseaddress:0x2000
eth0:0Linkencap:EthernetHWaddr00:0C:29:FA:2D:49
inetaddr:192.168.20.100Bcast:192.168.20.255Mask:255.255.255.0
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
Interrupt:19Baseaddress:0x2000
当节点二的heartbeat服务起来之后将抢夺资源
[[email protected]]#ifconfig
eth0Linkencap:EthernetHWaddr00:0C:29:D2:75:D0
inetaddr:192.168.20.20Bcast:192.168.20.255Mask:255.255.255.0
inet6addr:fe80::20c:29ff:fed2:75d0/64Scope:Link
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
RXpackets:6379errors:0dropped:0overruns:0frame:0
TXpackets:4892errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:1000
RXbytes:3005372(2.8MiB)TXbytes:712083(695.3KiB)
Interrupt:19Baseaddress:0x2000
eth0:0Linkencap:EthernetHWaddr00:0C:29:D2:75:D0
inetaddr:192.168.20.100Bcast:192.168.20.255Mask:255.255.255.0
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
Interrupt:19Baseaddress:0x2000
节点一执行hb_takeover抢夺资源
[root@node1heartbeat]#ifconfig
eth0Linkencap:EthernetHWaddr00:0C:29:FA:2D:49
inetaddr:192.168.20.10Bcast:192.168.20.255Mask:255.255.255.0
inet6addr:fe80::20c:29ff:fefa:2d49/64Scope:Link
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
RXpackets:10759errors:0dropped:0overruns:0frame:0
TXpackets:7779errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:1000
RXbytes:3175621(3.0MiB)TXbytes:1028476(1004.3KiB)
Interrupt:19Baseaddress:0x2000
eth0:0Linkencap:EthernetHWaddr00:0C:29:FA:2D:49
inetaddr:192.168.20.100Bcast:192.168.20.255Mask:255.255.255.0
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
Interrupt:19Baseaddress:0x2000
在节点一上查看虚拟服务规则表
[root@node1heartbeat]#ipvsadm-l
IPVirtualServerversion1.2.1(size=4096)
ProtLocalAddress:PortSchedulerFlags
->RemoteAddress:PortForwardWeightActiveConnInActConn
TCP192.168.20.100:httprr
->192.168.20.30:httpRoute100
->192.168.20.40:httpRoute100
节点二上没有虚拟ip
[email protected]]#ifconfig
eth0Linkencap:EthernetHWaddr00:0C:29:D2:75:D0
inetaddr:192.168.20.20Bcast:192.168.20.255Mask:255.255.255.0
inet6addr:fe80::20c:29ff:fed2:75d0/64Scope:Link
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
RXpackets:6417errors:0dropped:0overruns:0frame:0
TXpackets:4923errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:1000
RXbytes:3009061(2.8MiB)TXbytes:716759(699.9KiB)
Interrupt:19Baseaddress:0x2000
eth1Linkencap:EthernetHWaddr00:0C:29:CA:A4:3C
inetaddr:192.168.30.20Bcast:192.168.30.255Mask:255.255.255.0
inet6addr:fe80::20c:29ff:feca:a43c/64Scope:Link
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
RXpackets:4112errors:0dropped:0overruns:0frame:0
TXpackets:3482errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:1000
RXbytes:1018506(994.6KiB)TXbytes:863519(843.2KiB)
Interrupt:19Baseaddress:0x2080
[[email protected]]#ipvsadm-l
IPVirtualServerversion1.2.1(size=4096)
ProtLocalAddress:PortSchedulerFlags
->RemoteAddress:PortForwardWeightActiveConnInActConn
在客户端上访问http://192.168.20.100/
得到的页面是后方真实的web服务器1上的内容Server1
刷新页面
得到的页面是后方真实的web服务器2上的内容Server2
server2
在节点一上查看虚拟服务规则表
[root@node1heartbeat]#ipvsadm-l
IPVirtualServerversion1.2.1(size=4096)
ProtLocalAddress:PortSchedulerFlags
->RemoteAddress:PortForwardWeightActiveConnInActConn
TCP192.168.20.100:httprr
->192.168.20.30:httpRoute102
->192.168.20.40:httpRoute102
当server1失效时
查看虚拟规则表
[root@node1heartbeat]#ipvsadm-l
IPVirtualServerversion1.2.1(size=4096)
ProtLocalAddress:PortSchedulerFlags
->RemoteAddress:PortForwardWeightActiveConnInActConn
TCP192.168.20.100:httprr
->192.168.20.40:httpRoute101
再次访问http://192.168.20.100/
得到的页面是后方真实的web服务器2上的内容
Server2
刷新还是server2