RIP2=172.16.30.5 // 定义RealSever2的ip地址 PORT=80
#
case "$1" in
start)
/sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:1
# Since this is the Director we must be able to forward packets
echo 1 > /proc/sys/net/ipv4/ip_forward
# Clear all iptables rules.
/sbin/iptables -F
# Reset iptables counters.
/sbin/iptables -Z
# Clear all ipvsadm rules/services.
/sbin/ipvsadm -C
# Add an IP virtual service for VIP 172.16.30.1 port 80
# In this recipe, we will use the round-robin scheduling method.
# In production, however, you should use a weighted, dynamic scheduling method.
/sbin/ipvsadm -A -t $VIP:80 -s wlc
# Now direct packets for this VIP to
# the real server IP (RIP) inside the cluster
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g -w 1
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w 1
/bin/touch /var/lock/subsys/ipvsadm &> /dev/null
;;
stop)
# Stop forwarding packets
echo 0 > /proc/sys/net/ipv4/ip_forward
# Reset ipvsadm
/sbin/ipvsadm -C
# Bring down the VIP interface
/sbin/ifconfig eth0:1 down
/sbin/route del $VIP
/bin/rm -f /var/lock/subsys/ipvsadm
echo "ipvs is stopped..."
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ]; then
echo "ipvsadm is stopped ..."
else
echo "ipvs is running ..."
ipvsadm -L -n
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
;;
Esac
这个脚本在写完之后,要赋予执行的权限,并且把他放到/etc/ha.c/resource.d/下
# chmod +x ipvsd
# cp ipvsd /etc/ha.d/resource.d/
同步到dir2上:
# scp /etc/ha.d/resource.d/ipvsd dir2:/etc/ ha.d/resource.d/
现在都配置好了,接下来就是启动HA的Heartbeat服务了:
# /etc/init.d/heartbeat start
# sh dir2 -- '/etc/init.d/heartbeat start'
测试:
过一会儿之后,在dir1上:
# ifconfig
查看eth0:1网卡是否已经自动启动
在dir1和dir2上分别测试,dir1有配置而dir2上没有:
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.30.1:80 wlc
-> 172.16.30.5:80 Route 1 0 0
-> 172.16.30.4:80 Route 1 0 0
然后,我们把dir1设置为standby节点,即模拟故障:
# cd /usr/lib/heartbeat
# ./hb_standby
再次执行如下命令,dir2上就有配置了,说明资源已经自动从dir1转移到了dir2上了:
# ipvsadm –Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.30.1:80 wlc
-> 172.16.30.5:80 Route 1 0 0
-> 172.16.30.4:80 Route 1 0 0
然后测试网页的访问:
file:///C:/Users/Administrator/AppData/Local/youdao/ynote/images/9353985954FD4441B550EA46418AEF51/120709090695321.jpeg
|