发新话题
打印

[转载]redhatAS4.2 安装集群LVS

[转载]redhatAS4.2 安装集群LVS

信息来源: CU

环境描述:本文在配置LVS时使用三台linux,一台做Directorserver(192.168.0.25),两台做realserver(192.168.0.127192.168.0.128),在配置lvs+heartbeat时又添加了一台(192.168.0.126)做为备份主节点,虚拟VIP:192.168.0.100
软件列表:
ipvsadm-1.24.tar.gz
libnet.tar下载地址:http://www.packetfactory.net/libnet/稳定版本是:1.1.2.1
e2fsprogs可以用rpm安装光盘
heartbeat-2.0.2.tar.gz
2.6内核已经集成IPVS内核补订了,所以不再需要重新编译内核.



配置此集群分以下几种情况

一、配置基于DR模式Lvs集群
二、配置基于隧道模式Lvs集群
三、配置基于高可用Lvs+heartbeat
四、此种配置方式可以加强LVS的主节点的高安全性前提下(主节点简称DR,备份主节点DRbak),考虑充分利用资源可以将DRbak做为realserver




一、配置基于DR模式Lvs集群


1、下载ipvsadm管理程序
http://www.linuxvirtualserver.org/software/
注意对应自己的内核版本

ipvsadm-1.24.tar.gz

tarzxvfipvsadm-1.24.tar.gz
cdipvsadm-1.24
make&&makeinstall


注意在make时可能会出现很多错误的信息,请按照如下操作就可以心编译正常
ln-s/usr/src/kernels/2.6.9-22.EL-i686//usr/src/linux

cdipvsadm-1.24

make&&makeinstall


2、配置VIP脚本
[root@ns~]#more/etc/init.d/lvsDR
#!/bin/sh
#createin20060812byghb
#description:startLVSofDirectorserver
VIP=192.168.0.100
RIP1=192.168.0.127
RIP2=192.168.0.128
#RIPn=192.168.0.128~254
GW=192.168.0.1

./etc/rc.d/init.d/functions

case"$1"in
start)
echo"startLVSofDirectorServer"
#settheVirtualIPAddress
/sbin/ifconfigeth0:0$VIPbroadcast$VIPnetmask255.255.255.255up
/sbin/routeadd-host$VIPdeveth0:0
#ClearIPVStable
/sbin/ipvsadm-C
#setLVS
/sbin/ipvsadm-A-t$VIP:80-srr(如果需要session保持添加-p默认保持300秒)
/sbin/ipvsadm-a-t$VIP:80-r$RIP1:80-g
/sbin/ipvsadm-a-t$VIP:80-r$RIP2:80-g
#/sbin/ipvsadm-a-t$VIP:80-r$RIP3:80-g
#RunLVS
/sbin/ipvsadm
#end

;;
stop)
echo"closeLVSDirectorserver"
/sbin/ipvsadm-C
;;
*)
echo"Usage:$0{start|stop}"
exit1
esac



(-srr是使用了轮叫算法,可以自行选择相应的算法,更改rr就可以了,ipvsadm-h查看帮助。-g是使用lvs工作DR直接路由模式,也可自行修改)
如果有多个realserver直接添加就可以了,之后启动此脚本就可以了。

3、配置realserver脚本

#!/bin/bash
#description:startrealserver
#createin20060812byghb
VIP=192.168.0.100
/sbin/ifconfiglo:0$VIPbroadcast$VIPnetmask255.255.255.255up
/sbin/routeadd-host$VIPdevlo:0
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
sysctl-p
#end
此脚本使realserver不响应arp请求,将此脚本分别在realserver上执行就可以了。

测试:分别启动realserver上的httpd服务
在realserver1执行echo"Thisisrealserver1"/var/www/html/index.html
在realserver2执行echo"Thisisrealserver2"/var/www/html/index.html

打开IE浏览器输入http://192.168.0.100应该可以分别看到:Thisisrealserver1和Thisisrealserver1.


二、配置基于隧道模式Lvs集群

1、安装ipvsadmin方法和上面一样,在此略过

2、配置LVSdirectorserver脚本

[root@ns~]#more/etc/init.d/tunlvs
#!/bin/sh
#description:startLVSofDirectorserver
VIP=192.168.0.100
RIP1=192.168.0.127
RIP2=192.168.0.128
#RIPn=192.168.0.n
GW=192.168.0.1

./etc/rc.d/init.d/functions

case"$1"in
start)
echo"startLVSofDirectorServer"
#settheVirtualIPAddress
/sbin/ifconfigtunl0$VIPbroadcast$VIPnetmask255.255.255.255up
/sbin/routeadd-host$VIPdevtunl0
#ClearIPVStable
/sbin/ipvsadm-C
#setLVS
/sbin/ipvsadm-A-t$VIP:80-srr
/sbin/ipvsadm-a-t$VIP:80-r$RIP1:80-i
/sbin/ipvsadm-a-t$VIP:80-r$RIP2:80-i
#/sbin/ipvsadm-a-t$VIP:80-r$RIP3:80-i
#RunLVS
/sbin/ipvsadm
#end

;;
stop)
echo"closeLVSDirectorserver"
ifconfigtunl0down
/sbin/ipvsadm-C
;;
*)
echo"Usage:$0{start|stop}"
exit1
esac

3、配置realserver

[root@localhost~]#more/etc/init.d/tunl
#!/bin/sh
#ghbin20060812
#description:Configrealservertunlportandapplyarppatch
VIP=192.168.0.100

./etc/rc.d/init.d/functions

case"$1"in
start)
echo"Tunlportstarting"
ifconfigtunl0$VIPnetmask255.255.255.255broadcast$VIPup
/sbin/routeadd-host$VIPdevtunl0
echo"1">/proc/sys/net/ipv4/conf/tunl0/arp_ignore
echo"2">/proc/sys/net/ipv4/conf/tunl0/arp_announce
echo"1">/proc/sys/net/ipv4/conf/all/arp_ignore
echo"2">/proc/sys/net/ipv4/conf/all/arp_announce
sysctl-p

;;
stop)
echo"Tunlportclosing"
ifconfigtunl0down
echo1>/proc/sys/net/ipv4/ip_forward
echo0>/proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo"Usage:$0{start|stop}"
exit1
esac

如果有多个VirutalIP,可以使用tunl0:0,tunl0:1...。
此脚本分别在realserver上执行,目的使realserver忽略arp响应,并设定vip.

测式同样按照上面的方法测试


三、配置基于高可用Lvs+heartbeat

1、确定LVS使用DR或/tun模式,请对照上面的配置。
本例使用tun模式
Directorserver端脚本文件
[root@ns~]#more/etc/init.d/tunlvs
#!/bin/sh
#description:startLVSofDirectorserver
VIP=192.168.0.100
RIP1=192.168.0.127
RIP2=192.168.0.128
#RIPn=192.168.0.n
GW=192.168.0.1

./etc/rc.d/init.d/functions

case"$1"in
start)
echo"startLVSofDirectorServer"
#settheVirtualIPAddress
/sbin/ifconfigtunl0$VIPbroadcast$VIPnetmask255.255.255.255up
/sbin/routeadd-host$VIPdevtunl0
#ClearIPVStable
/sbin/ipvsadm-C
#setLVS
/sbin/ipvsadm-A-t$VIP:80-srr
/sbin/ipvsadm-a-t$VIP:80-r$RIP1:80-i
/sbin/ipvsadm-a-t$VIP:80-r$RIP2:80-i
#/sbin/ipvsadm-a-t$VIP:80-r$RIP3:80-i
#RunLVS
/sbin/ipvsadm
#end

;;
stop)
echo"closeLVSDirectorserver"
ifconfigtunl0down
/sbin/ipvsadm-C
;;
*)
echo"Usage:$0{start|stop}"
exit1
esac

realserver端同样使用上面的配置文件就可以。

2、安装heartbeat
libnet.tar下载地址:http://www.packetfactory.net/libnet/稳定版本是:1.1.2.1
e2fsprogs可以用rpm安装光盘
heartbeat-2.0.2.tar.gz下载地址:http://www.linux-ha.org/download/


2.1安装

tar-zxvflibnet.tar.gz
cdlibnet
./configure
make
makeinstall

tarzxfheartbeat-1.99.4.tar.gz
cdheartbeat-1.99.4
./ConfigureMeconfigure--disable-swig--disable-snmp-subagent
make
makeinstall
cpdoc/ha.cfdoc/haresourcesdoc/authkeys/etc/ha.d/
cpldirectord/ldirectord.cf/etc/ha.d/


配置:

主配置文件(/etc/ha.d/ha.cf)
#debugfile/var/log/ha-debug
logfile/var/log/ha-log#指名heartbeat的日志存放位置
#crmyes#支持ClusterResourceManager(集群资源管理)功能
#bcasteth1#指明心跳方式使用以太广播方式,并且是在eth1接口上进行广播。
logfacilitylocal0
keepalive2#指明心跳时间为2秒(即每两秒钟在eth1上发送一次广播)。
deadtime30#指定在30秒内没有心跳信号,则立即切换服务。
warntime10#指明心跳延迟的时间为十秒。当10秒钟内备份机不能联系上主机(当前活动的服务器,即无心跳信号),就会往日志中写入一个警告日志,但此时不会切换服务。


initdead120#Withsomeconfigurations,thenetworktakessometimetostartworkingafterareboot.Thisisaseparate"deadtime"tohandlethatcase.Itshouldbeatleasttwicethenormaldeadtime.
udpport694#Useportnumber694forbcastorucastcommunication.Thisisthedefault,andtheofficialIANAregisteredportnumber.

baud19200
serial/dev/ttyS0
mcasteth0225.0.0.169410

#当主节点恢复后,是否自动切回
auto_failbackon

#stonith用来保证共享存储环境中的数据完整性
#stonithbaytech/etc/ha.d/conf/stonith.baytech

#watchdog能让系统在出现故障1分钟后重启该机器。这个功能可以帮助服务器在确实停止心跳后能够重新恢复心跳。
#如果使用该特性,则在内核中装入"softdog"内核模块,用来生成实际的设备文件,输入"insmodsoftdog"加载模块。
#输入"grepmisc/proc/devices"(应为10),输入"cat/proc/misc|grepwatchdog"(应为130)。
#生成设备文件:"mknod/dev/watchdogc10130"。
#watchdog/dev/watchdog

nodens.ghb.com#Mandatory.Hostnameofmachineinclusterasdescribedbyuname-n.
nodensbak.ghb.com

#默认heartbeat并不检测除本身之外的其他任何服务,也不检测网络状况。
#所以当网络中断时,并不会进行LoadBalancer和Backup之间的切换。
#可以通过ipfail插件,设置'pingnodes'来解决这一问题。详细说明参考hearbeat文档。
#ping192.168.136.1172.16.0.1
ping_groupgroup1192.168.136.1192.168.136.2
respawnroot/usr/lib/heartbeat/ipfail
apiauthipfailgid=rootuid=root

#其他一些插件可以在/usr/lib/heartbeat下找到
#apiauthipfailuid=hacluster
#apiauthccmuid=hacluster
#apiauthcmsuid=hacluster
#apiauthpinggid=haclientuid=alanr,root
#apiauthdefaultgid=haclient

资源文件(/etc/ha.d/haresources):
ns.aaa.comIPaddr::192.168.0.127/24/eth0httpd
#设置ns.aaa.com为主节点,集群服务器的ip地址为192.168.0.127netmask为255.255.255.240集群的服务有httpd

认证文件(/etc/ha.d/authkeys),选取一种认证方式,这个文件的权限必须是600

auth1
1crc
#2sha1sha1_any_password
#3md5md5_any_password

使用同样的方法配置节点2
备份节点192.168.0.126上的heartbeat和apache的配置与节点1要完全相同,lvs配置也要相同。


2.2
完装完毕进行测试,关闭主节点机器,另一台自动接管,主节点恢复后自动接管回服务。如果以上测试没有问题,那么开始和lvs整合。

配置Ldirectord
Ldirectord的作用是监测RealServer,当RealServer失效时,把它从LoadBalancer列表中删除,恢复时重新添加,在安装heartbeat时已经安装了Ldirectord。

配置(/etc/ha.d/ldirectord.cf):
#GlobalDirectives
checktimeout=3
checkinterval=1
fallback=127.0.0.1:80
autoreload=yes
logfile="/var/log/ldirectord.log"
quiescent=yes

#Asamplevirualwithafallbackthatwilloverridethegobalsetting
virtual=192.168.0.100:80
real=192.168.0.127:80gate
real=192.168.0.128:80gate
fallback=127.0.0.1:80gate
service=http
request="test.html"
receive="TestPage"
virtualhost=www.funbsd.net
scheduler=rr
#persistent=600
#netmask=255.255.255.255
protocol=tcp
在每个RealServer的中添加监控页:
echo"TestPage">>/var/www/html/test.html


修改heartbeat的资源文件/etc/ha.d/haresources
ns.ghb.com192.168.0.100tunlvsldirectordhttpd



现在可以在主节点192.168.0.25上启动heartbeat

/etc/init.d/heartbeatstart

在备份节点也启动heartbeat/etc/init.d/heartbeatstart

测试:关闭主节点,备份节点将自动接管directorserver服务。(主节点正常时用ifconfig是可以看tunl接可口的,而备份节点用ifconfig命令是看不到的,只有在接管主节点服务后才是可以见的)

至此第三部分配置完毕。


四、考虑充份使用资源,将备份节点也做为realserver.

在主备directorserver上都要做如下配置
1.directorserver需要在脚本文件中将添加realserver,我们预先设置所有机器为realserver。

#!/bin/sh
#description:startLVSofDirectorserver
VIP=192.168.0.100
RIP1=192.168.0.127
RIP2=192.168.0.128
RIP3=192.168.0.25
RIP4=192.168.0.126
GW=192.168.0.1

./etc/rc.d/init.d/functions

case"$1"in
start)
echo"startLVSofDirectorServer"
#settheVirtualIPAddress
/sbin/ifconfigtunl0$VIPbroadcast$VIPnetmask255.255.255.255up
/sbin/routeadd-host$VIPdevtunl0
#ClearIPVStable
/sbin/ipvsadm-C
#setLVS
/sbin/ipvsadm-A-t$VIP:80-srr
/sbin/ipvsadm-a-t$VIP:80-r$RIP1:80-i
/sbin/ipvsadm-a-t$VIP:80-r$RIP2:80-i
/sbin/ipvsadm-a-t$VIP:80-r$RIP3:80-i
/sbin/ipvsadm-a-t$VIP:80-r$RIP3:80-i
#RunLVS
/sbin/ipvsadm
#end

;;
stop)
echo"closeLVSDirectorserver"
ifconfigtunl0down
/sbin/ipvsadm-C
;;
*)
echo"Usage:$0{start|stop}"
exit1
esac

2.修改/etc/ha.d/ldirectord.cf
#GlobalDirectives
checktimeout=3
checkinterval=1
fallback=127.0.0.1:80
autoreload=yes
logfile="/var/log/ldirectord.log"
quiescent=yes

#Asamplevirualwithafallbackthatwilloverridethegobalsetting
virtual=192.168.0.100:80
real=192.168.0.126:80gate
real=192.168.0.127:80gate
real=192.168.0.128:80gate
real=192.168.0.25:80gate
fallback=127.0.0.1:80gate
service=http
request="test.html"
receive="TestPage"
virtualhost=www.funbsd.net
scheduler=rr
#persistent=600
#netmask=255.255.255.255
protocol=tcp

3、将realserver的启动脚本加入到主节点,和备份节点中,并且这个脚本的启动级必须先于heartbeat,关闭级必须后于heartbeat

chkconfigtunlon添加到系统启动

4、创建closetunl启动脚本,为启动directorserver做准备
more/etc/init.d/closetunl
#!/bin/sh
#createin200608ghb
#description:closetunl0andarp_ignore
VIP=192.168.136.100

./etc/rc.d/init.d/functions

case"$1"in
start)
echo"startdirectorserverandclosetunl"
ifconfigtunl0down
echo1>/proc/sys/net/ipv4/ip_forward
echo0>/proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
echo"startRealServer"
ifconfigtunl0$VIPnetmask255.255.255.255broadcast$VIPup
/sbin/routeadd-host$VIPdevtunl0
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
sysctl-p

;;
*)
echo"Usage:lvs{start|stop}"
exit1
esac

chmod755/etc/init.d/closetunl
5、修改/etc/ha.d/haresources
ns.wdxc.comclosetunl192.168.0.100tunlvsldirectordhttpd



6、测试
http://192.168.0.100应该可以轮到四台机器上

配置完毕


总结,上面就是本人在本配置LVS+heartbeat过程,本过程还没涉及到共享存储这块,我觉得集群整套方案,共享存储是相当重要的部分,简易实现通过NFS也可以实现共享存储,但是在要求更大,更快时,可以要考虑其的存储方式如SAN等。还有就是后端数据库之间的负载,同步问题等,本人将在下一步解决关于共享存储,数据库负载方面的问题。也希望大家可以给出一些方案或是想法,大家共同讨论一下,共同学习,共同进步。

TOP

发新话题