实验平台:virtualbox 4.12
操作系统:RHEL5.4
实验拓扑:
本实验是在上一篇配置HA高可用集群的基础上进行的,所以关于HA的配置过程不再详细说明。 首先来配置两台Realserver,分别安装httpd服务,并添加测试页下面是在两台HA上进行的配置: 在两台Directory上分别安装ipvsadm
- [root@R1 ~]#yum install httpd -y
- [root@R1 ~]#echo '<h1>Realserver1</h1> <body bgcolor=red</body>' > /var/www/html/index.html
- [root@R2 ~]#yum install httpd -y
- [root@R2 ~]#echo '<h1>Realserver2</h1> <body bgcolor=red</body>' > /var/www/html/index.html
由于使用两台Directory搭建lvs,关于lvs的Directory的设置就必须依赖于脚本来实现了。先来编辑Directory脚本,该脚本用于在资源流向自己时自动设置自己为lvs的Directory,并在资源不属于自己时取消关于lvs的各项设置 [root@node1 ~]#vim /etc/init.d/ipvs
- [root@node1 ~]# yum install ipvsadm -y
- [root@node2 ~]# yum install ipvsadm -y
- #!/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
将配置好的脚本拷贝至node2上 [root@node1 ~]# scp /etc/init.d/ipvs node2:/etc/init.d/ 这里需要对haresources文件进行一下修改,将刚才的脚本设置为随服务启动
- [root@node1 ~]#chmod a+x /etc/init.d/ipvsadm.sh
配置realservers的脚本(运行此脚本能够实现快速配置realservers)
- [root@node1 ~]# vim /etc/ha.d/haresources
- node1.a.org 192.168.56.20/24/eth0/192.168.56.255 ipvs [root@node1 ~]# scp /etc/ha.d/haresources node2:/etc/ha.d/
启动服务进行测试:
- #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/rs.sh
- [root@R1 ~]# scp /etc/init.d/rs.sh 192.168.56.12:/etc/init.d/
查看一下LVS信息 [root@node1 ~]# ipvsadm -L -n 说明服务已经正常启动,在浏览器上测试一下 这时停掉node1上的heartbeat,看服务是否正常 [root@node1 ~]# service heartbeat stop 刷新页面可以看到 依然可以正常访问,在node2上查看一下ipvsdam的信息 [root@node2 ~]# ipvsadm -L -n 服务器搭建完成。
- [root@node1 ~]# /etc/init.d/heartbeat start
- [root@node1 ~]# ssh node2 -- '/etc/init.d/heartbeat start'
- [root@node1 ~]# /etc/init.d/ipvs start
- [root@R1 ~]# /etc/init.d/rs.sh start
- [root@R2 ~]# /etc/init.d/rs.sh start
本文出自 “春夏秋冬又一春” 博客,请务必保留此出处http://zhangjianjian.blog.51cto.com/2944771/662962