实验平台:virtualbox 4.12
操作系统:RHEL5.4
- heartbeat-2.1.4-9.el5.i386.rpm
- heartbeat-devel-2.1.4-9.el5.i386.rpm
- heartbeat-gui-2.1.4-9.el5.i386.rpm
- heartbeat-ldirectord-2.1.4-9.el5.i386.rpm
- heartbeat-pils-2.1.4-10.el5.i386.rpm
- heartbeat-stonith-2.1.4-10.el5.i386.rpm
- libnet-1.1.4-3.el5.i386.rpm
- perl-MailTools-1.77-1.el5.noarch.rpm
- [root@DIR ~]# ssh-keygen -t rsa
- [root@DIR ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node2
- [root@DIR2 ~]# ssh-keygen -t rsa
- [root@DIR2 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node1
- [root@node1 ~]# cp /usr/share/doc/heartbeat-2.1.4/{ha.cf,haresources,authkeys} /etc/ha.d/
- [root@node1 ~]#cp /usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf /etc/ha.d/
- logfile /var/log/ha-log //打开日志
- bcast eth1 //监测心跳的网卡
- node node1.a.org
- node node2.a.org //定义集群节点
- respawn hacluster /usr/local/lib64/heartbeat/ipfail
- apiauth ipfail gid=haclient uid=hacluster //定义随heartbeat一起启动的进程
- [root@node1 ~]# vim /etc/ha.d/ldirectord.cf
- quiescent=yes
- checktimeout=3 //检测存活的时间
- checkinterval=1 //查询间隔
- autoreload=yes //自动重读配置文件
- logfile="/var/log/ldirectord.log" //日志目录
- logfile="local0"
- virtual=192.168.56.20:80 //虚拟主机地址
- real=192.168.56.11:80 gate //Realserver地址
- real=192.168.56.12:80 gate
- fallback=127.0.0.1:80 gate //Realserver全部宕机时指向的地址
- service=http //服务
- request="test/index.html" //测试存活与否所用的页面
- receive="ok" //检测关键字
- scheduler=wlc //调度算法
- protocol=tcp
- checktype=negotiate //检测类型
- checkport=80
- [root@R1 ~]#yum install httpd -y
- [root@R1 ~]#echo '<h1>Realserver1</h1> <body bgcolor=red</body>' > /var/www/html/index.html
- [root@R1 ~]# echo 'ok' > /var/www/html/test.html
- [root@R2 ~]#yum install httpd -y
- [root@R2 ~]#echo '<h1>Realserver2</h1> <body bgcolor=red</body>' > /var/www/html/index.html
- [root@R2 ~]# echo 'ok' > /var/www/html/test.html
- [root@node1 ~]# yum install httpd -y
- [root@node1 ~]# echo '<h1>web service is under maintenance,please weat...</h1> <body bgcolor='red'> </body>' > /var/www/html/index.html
- [root@node2 ~]# yum install httpd -y
- [root@node2 ~]# echo '<h1>web service is under maintenance,please weat...</h1> <body bgcolor='green'> </body>' > /var/www/html/index.htm
- [root@node1 ~]# yum install ipvsadm -y
- [root@node2 ~]# yum install ipvsadm -y
- [root@node1 ~]#vim /etc/init.d/ipvs.sh
- #!/bin/bash
- . /etc/rc.d/init.d/functions
- VIP=192.168.56.20
- RIP1=192.168.56.11
- RIP2=192.168.56.12
- 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
- echo 1 > /proc/sys/net/ipv4/ip_forward //开启本机路由转发功能
- # 清除已有的iptables配置
- /sbin/iptables -F
- /sbin/iptables -Z
- # 清除已有的ipvsadm 规则
- /sbin/ipvsadm -C
- # 添加VIP
- /sbin/ipvsadm -A -t $VIP:80 -s wlc
- # 在Directory下添加Realserver
- /sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g -w 1
- /sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w 2
- /bin/touch /var/lock/subsys/ipvsadm &> /dev/null
- ;;
- stop)
- echo 0 > /proc/sys/net/ipv4/ip_forward
- /sbin/ipvsadm -C
- /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
- [root@node1 ~]#chmod a+x /etc/init.d/ipvs.sh
- #vim /etc/init.d/realserver.sh
- #!/bin/bash
- # Script to start LVS DR real server.
- # description: LVS DR real server
- . /etc/rc.d/init.d/functions
- VIP=192.168.56.20
- host=`/bin/hostname`
- case "$1" in
- start)
- # Start LVS-DR real server on this machine.
- /sbin/ifconfig lo down
- /sbin/ifconfig lo up
- echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
- echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
- echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
- echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
- /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
- /sbin/route add -host $VIP dev lo:0
- ;;
- stop)
- /sbin/ifconfig lo:0 down
- echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
- echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
- echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
- echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
- ;;
- status)
- # Status of LVS-DR real server.
- islothere=`/sbin/ifconfig lo:0 | grep $VIP`
- isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
- if [ ! "$islothere" -o ! "isrothere" ];then
- # Either the route or the lo:0 device
- # not found.
- echo "LVS-DR real server Stopped."
- else
- echo "LVS-DR real server Running."
- fi
- ;;
- *)
- # Invalid entry.
- echo "$0: Usage: $0 {start|status|stop}"
- exit 1
- ;;
- esac
- [root@R1 ~]# chmod a+x /etc/init.d/realserver.sh
- [root@node1 ~]# /etc/init.d/heartbeat start
- [root@node1 ~]# ssh node2 -- '/etc/init.d/heartbeat start'
- [root@node1 ~]# /etc/init.d/ldirectord start
- [root@node1 ~]# ssh node2 --'/etc/init.d/ldirectord start'
- [root@node1 ~]# /etc/init.d/ipvs start
- [root@R1 ~]# /etc/init.d/realserver.sh start
- [root@R2 ~]# /etc/init.d/realserver.sh start
把两台Realserver都停掉,刷新页面可以看到