实验平台:virtualbox 4.12
操作系统:RHEL5.4
实验拓扑:两台Directory都有两块网卡,都设置为静态地址,其中eth0用于网络间通信,eth1用于两台Directory监测彼此心跳信息,所以使用独立的网络进行连接,在实际应用中可以使用串行线进行连接,这里由于使用虚拟机就采用intnet的内部网予以实现。 本实验用到的软件包有:一.首先安装软件包 [root@DIR ~]# yum localinstall *.rpm --nogpgcheck -y 按照拓扑图设置两台主机的IP地址和主机名 分别修改两主机/etc/hosts文件,添加下面的内容 192.168.56.10 node1.a.org node1 192.168.56.30 node2.a.org node2 为两主机设置免密码ssh连接 首先在node1上:
- 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
再在node2上:
- [root@DIR ~]# ssh-keygen -t rsa
- [root@DIR ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node2
这样就可以免输密码自由连接了。 可以将node1上的软件包直接拷贝至node2上进行安装: [root@node1 ~]# scp *.rpm node2:/root/ 然后同样的安装步骤: [root@DIR2 ~]# yum localinstall -y *.rpm –nogpgcheck 准备工作基本结束,下面开始进行配置二.配置Heartbeat 1.将heartbeat的配置文件拷贝到/etc/ha.d目录下
- [root@DIR2 ~]# ssh-keygen -t rsa
- [root@DIR2 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node1
2.首先编辑ha.cf [root@node1 ha.d]# cd /etc/ha.d/ [root@node1 ha.d]# vim ha.cf 修改下面几项:
- [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/
3.然后编辑authkeys
- 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一起启动的进程
5.拷贝配置文件至node2 由于两台主机的配置文件必须相同,这里直接将node1上编辑好的配置文件拷贝至node2 [root@node1 ha.d]# scp ha.cf haresources authkeys ldirectord.cf node2:/etc/ha.d/ 首先来配置两台Directory和两台Realserver,分别安装httpd服务,并添加测试页
- [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
下面是在两台HA上进行的配置: 在两台Directory上分别安装ipvsadm
- [root@R1 ~]#yum install httpd -y
- [root@R1 ~]#echo '<h1>Realserver1h1> <body bgcolor=redbody>' > /var/www/html/index.html
- [root@R1 ~]# echo 'ok' > /var/www/html/test.html
- [root@R2 ~]#yum install httpd -y
- [root@R2 ~]#echo '<h1>Realserver2h1> <body bgcolor=redbody>' > /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
由于使用两台Directory搭建lvs,关于lvs的Directory的设置就必须依赖于脚本来实现了。先来编辑Directory脚本,该脚本用于在资源流向自己时自动设置自己为lvs的Directory,并在资源不属于自己时取消关于lvs的各项设置
- [root@node1 ~]# yum install ipvsadm -y
- [root@node2 ~]# yum install ipvsadm -y
将配置好的脚本拷贝至node2上[root@node1 ~]# scp /etc/init.d/ipvs node2:/etc/init.d/这里需要对haresources文件进行一下修改,将刚才的脚本设置为随服务启动配置realservers的脚本(运行此脚本能够实现快速配置realservers)
- [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
[root@R1 ~]# scp /etc/init.d/rs.sh 192.168.56.12:/etc/init.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/realserver.sh
查看一下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/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都停掉,刷新页面可以看到
查看一下ipvs转发信息[root@node1 ~]# ipvsadm -L -n 停掉主节点node1,刷新页面 [root@node2 ~]# ipvsadm -L -n至此,配置完成。
本文出自 “春夏秋冬又一春” 博客,请务必保留此出处http://zhangjianjian.blog.51cto.com/2944771/662981