RedHat 8.0/9.0 LVS安裝手冊(5)

类别:编程语言 点击:0 评论:0 推荐:

三、做DR router的Load Balance:

  那如果今天我有一堆机器,而且在上面执行的服务都相当的重要,所以只有一部Director对这些重要工作来说,是非常危险的,万一这部Director死掉了,那我的服务不就毁了?因此在这儿要介绍「mon+heartbeat+fake+coda」的heartbeat与fake的安装与设定。

1、下载安装文件

  从http://www.linux-ha.org/download/ 抓取下列RPM档案:

heartbeat-1.0.3-1.rh.8.0.1.i386.rpm heartbeat-1.0.3-1.rh.8.0.1.src.rpm heartbeat-ldirectord-1.0.3-1.rh.8.0.1.i386.rpm heartbeat-pils-1.0.3-1.rh.8.0.1.i386.rpm heartbeat-stonith-1.0.3-1.rh.8.0.1.i386.rpm

  从http://rpmfind.net 抓取下列RPM档案:

libnet-1.1.0-1.rh.8.0.um.1.i386.rpm perl-Authen-SASL-2.03-1.rh.8.0.um.1.noarch.rpm perl-Convert-ASN1-0.16-2.rh.8.0.um.1.noarch.rpm perl-IO-Socket-SSL-0.92-1.rh.8.0.um.1.noarch.rpm perl-Mail-IMAPClient-2.2.7-1.rh.8.0.um.1.noarch.rpm perl-Net-SSLeay-1.22-1.rh.8.0.um.1.i386.rpm perl-Parse-RecDescent-1.80-8.noarch.rpm perl-XML-NamespaceSupport-1.08-2.noarch.rpm perl-XML-SAX-0.12-1.rh.8.0.um.1.noarch.rpm

  从http://search.cpan.org抓取下列perl-ldap档案,目前最新版本是0.27版本,其实都无所谓,可以用就好了。

perl-ldap-0.25.tar.gz (我这儿用以前我抓的tar.gz檔)

2、开始依照下列顺序安装:

# rpm -ivh perl-Convert-ASNI-0.16.2.rh.8.0.um.1.onarch.rpm # perl -ivh perl-Authen-SASL-2.03-1.rh.8.0.um.1.noarch.rpm # rpm -ivh perl-Net-SSLeay-1.22-1.rh.8.0.um.1.i386.rpm # rpm -ivh perl-IO-Socket-SSL-0.92-1.rh.8.0.um.1.noarch.rpm # rpm -ivh perl-Mail-IMAPClient-2.2.7-1.rh.8.0.um.1.noarch.rpm # rpm -ivh perl-Parse-RecDescent-1.80-8.noarch.rpm # rpm -ivh perl- NamespaceSupport-1.08-2.noarch.rpm # rpm -ivh perl- XML-SAX-0.12-1.rh.8.0.um.1.noarch.rpm # rpm -ivh libnet-1.1.0-1.rh.8.0.um.1.i386.rpm # gzip -dc perl-ldap-0.25.tar.gz |tar xvf - # cd perl-ldap-0.25 # perl Makefile # make && make test && make install # cd ..

  需要的Perl module安装完毕,开始安装heartbeat的modules。

# rpm -ivh heartbeat-pils-1.0.3-1.rh.8.0.1.i386.rpm # rpm -ivh heartbeat-stonith-1.0.3-1.rh.8.0.1.i386.rpm # rpm -ivh heartbeat-1.0.3-1.rh.8.0.1.i386.rpm

  下面这个module是一个整合所有功能的RPM,heartbeat官方网站有介绍。

# rpm -ivh --nodeps heartbeat-ldirectord-1.0.3-1.rh.8.0.1.i386.rpm

  从http://www.linux-ha.org/这个网站上可以知道fake已经整合进heartbeat的功能里面,所以我们就不需要安装fake相关的程序了。

  目前Linux-HA可以应用的方面如下:

o Web servers o LVS director servers o Mail servers o Database servers o Firewalls o File servers o DNS servers o DHCP servers o Proxy Caching servers o etc.

  我们这里的重点是要做LVS Director Servers的Failover机制,也就是要做HA(High availability)架构。所以对于heartbeat-ldirectord这个套件不作介绍,有兴趣的人请自行研究(http://www.linuxvirtualserver.org/HighAvailability.html)一文。

  Linux-HA实验例子:

  系统架构如同基本设置范例图形所示,只是这儿设置Primary Director为10.144.43.187 (linux187),而Secondary Director为10.144.43.142(linux142),而10.144.43.175(就是唯一的RealServer,但是事实上10.144.43.142与10.144.43.187上面还是有提供服务。简单的说就是Director可以互相备援,且Realserver失败的时候,会自动从Director上面将该笔资料自LVS的Routing Rule中消除。

1、在Linux142与Linux187上都将Mon与Heartbeat套件安装完成。且在/etc/rc.local中加入一个设定subinterface的shellscript(/root/conf/data)。

  /root/conf/data 内容如下:

#!/bin/sh # 开机时读取,设定LVS相关的设定值,此时扮演LVS中的RealServer的角色 PATH=/bin:/sbin:/usr/bin:/usr/sbin export PATH ifconfig lo:0 10.144.43.185 netmask 255.255.255.255 broadcast 10.144.43.185 echo 1 >/proc/sys/net/ipv4/ip_forward echo 1 >/proc/sys/net/ipv4/conf/all/hidden echo 1 >/proc/sys/net/ipv4/conf/lo/hidden route add -host 10.144.43.185 dev lo:0

  /etc/rc.local内容如下:

#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local # add subinterface /root/conf/data

  作这个动作的目的在于让Linux142、Linux187在开机的时候将lo:0这个subinterface加入在网络卡中,同时激活hidden避免ARP问题。记得chmod 755 data。

2、在Linux142与linux187中,接着设定Mon基本设定,修改/etc/mon/mon.cf,使其对nb8048、Linux187、linux142作监测。因为提供的服务是Port 8080,在/etc/services中的名称为(webcache)

# # Extremely basic mon.cf file # # # global options # cfbasedir = /etc/mon pidfile = /var/run/mon.pid statedir = /var/run/mon/state.d logdir = /var/run/mon/log.d dtlogfile = /var/run/mon/log.d/downtime.log alertdir = /usr/lib/mon/alert.d mondir = /usr/lib/mon/mon.d maxprocs = 20 histlength = 100 randstart = 60s authtype = userfile userfile = /etc/mon/userfile # # group definitions (hostnames or IP addresses) # hostgroup server1 10.144.43.175 hostgroup server2 10.144.43.142 hostgroup server3 10.144.43.187 watch server1 service webcache interval 10s monitor http.monitor -p 8080 -t 10 allow_empty_group period wd {Sun-Sat} alert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.175 -W 5 -F dr alertevery 1h upalert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.175 -W 5 -F dr -u 1 watch server2 service webcache interval 10s monitor http.monitor -p 8080 -t 10 period wd {Sun-Sat} alert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.142 -W 5 -F dr alertevery 1h upalert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.142 -W 5 -F dr -u 1 watch server3 service webcache interval 10s monitor http.monitor -p 8080 -t 10 period wd {Sun-Sat} alert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.187 -W 5 -F dr alertevery 1h upalert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.187 -W 5 -F dr -u 1

3、编辑lvs.alert同前面提过的lvs.alert档案,放在/usr/lib/mon/alert.d/中。记得

chmod 755 lvs.alert

4、设定Heartbeat套件(High Availability)。

4.1、利用rpm -q heartbeat -d 将Heartbeat三个重要的设定档案(ha.cf、authkeys、haresources),copy到/etc/ha.d目录下。

[root@linux142 conf]# rpm -q heartbeat -d /usr/share/doc/heartbeat-1.0.3/authkeys /usr/share/doc/heartbeat-1.0.3/ha.cf /usr/share/doc/heartbeat-1.0.3/haresources [root@linux142 conf]#

4.2 修改/etc/hosts内容如下:

# Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 localhost.localdomain localhost 10.144.43.142 linux142 10.144.43.187 Linux187 10.144.43.175 nb8048

4.3、修改/etc/ha.d/authkeys内容如下:

auth 1 1 sha1 doss123   其中doss123为重要的key。然后

# chmod 600 authkeys

4.4、修改/etc/ha.d/ha.cf内容如下:

logfacility local0 # 使用syslogd keepalive 2 # 每隔几秒送一次heartbeat讯号 deadtime 40 # 多长时间就认定Primary Server Dead initdead 240 # hopfudge maximum hop count minus number of nodes in config nice_failback on # 如果Service转移到Secondary,就不要再转移回Primary hopfudge 1 # ??? udpport 1001 # 使用哪一个udp port ucast eth0 10.144.43.142 # Primary用unicast送给Secondary,如果在 Secondary则送给Primary node Linux187 # 给定cluster中的节点 node linux142 # 给定cluster中的节点

4.5、修改/etc/ha.d/haresources内容如下:

Linux187 10.144.43.185 lvs mon

  这里设定Linux187在/etc/hosts里面要有设定,否则会找不到。且名称必须等于「uname -n」的内容。上面的意思是说,当heartbeat两边激活时,Linux187与linux142都激活时,会有linux187取得LVS VIP的控制权。而他会激活后面两个Service,lvs(我们皆下来要写的激活Daemon 的shell script),而mon则是先前我们已经设定的好的mon daemon。

4.6、编写/etc/init.d/lvs(激活lvs daemon)内容如下:

#!/bin/sh # /etc/init.d/lvs # 当LVS Server激活时,也就是透过High Availability激活LVS程序时, # 1.将Lo的hidden属性取消 # 2.将lo:0 down下来,将lo:0删除 # 3.设定相关LVS设定档案 PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH IPVSADM=/sbin/ipvsadm case "$1" in start) if [ -x $IPVSADM ] then # 因为我们有设定lo:0的loopbback 的subinterface所以要先将相关设定移除 echo 1 > /proc/sys/net/ipv4/ip_forward echo 0 > /proc/sys/net/ipv4/conf/all/hidden echo 0 > /proc/sys/net/ipv4/conf/lo/hidden route del -host 10.144.43.185 dev lo:0 ifconfig lo:0 down # 建立Subinterface eth0:0 ifconfig eth0:0 10.144.43.185 netmask 255.255.255.255 broadcast 10.144.43.185 # 设定LVS/DR相关设定,建立8080 Port的Service $IPVSADM -A -t 10.144.43.185:8080 -s rr $IPVSADM -a -t 10.144.43.185:8080 -r 10.144.43.175 -w 5 -g $IPVSADM -a -t 10.144.43.185:8080 -r 10.144.43.187 -w 5 -g $IPVSADM -a -t 10.144.43.185:8080 -r 10.144.43.142 -w 5 -g fi ;; stop) if [ -x $IPVSADM ] then #---- 停止LVS Server,但是依然要回复RealServer的身份 $IPVSADM -C echo 1 >/proc/sys/net/ipv4/conf/all/hidden echo 1>/proc/sys/net/ipv4/conf/lo/hidden ifconfig lo:0 10.144.43.185 netmask 255.255.255.255 broadcast 10.144.43.185 route add -host 10.144.43.185 dev lo:0 fi ;; *) echo "Usage: lvs {start|stop}" exit 1 esac exit 0

4.7、修改/etc/ha.d/resource.d/IPaddr 这个shellscript:

  在add_interface() subrouteing 中新加入下列三行:

add_interface () { ipaddr="$1" ifinfo="$2" iface="$3" # # On Linux the Alias is named ethx:y # This will remove the "extra" interface Data # leaving us with just ethx # case $SYSTYPE in *BSD) IFEXTRA="" ;; *) IFEXTRA=`echo "$ifinfo" | cut -f2-` ;; esac case $SYSTYPE in SunOS) case `uname -r` in 5.8) $IFCONFIG $iface plumb CMD="$IFCONFIG $iface inet $ipaddr $IFEXTRA up" ;; *) CMD="$IFCONFIG $iface inet $ipaddr $IFEXTRA up" ;; esac ;; *BSD) CMD="$IFCONFIG $iface inet $ipaddr netmask 255.255.255.255 alias" ;; *) CMD="$IFCONFIG $iface $ipaddr $IFEXTRA" ;; esac ha_log "info: $CMD" $CMD # 将subinterface的hidden属性打开,如此可避免ARP问题 echo 1 >/proc/sys/net/ipv4/conf/all/hidden echo 1 >/proc/sys/net/ipv4/conf/lo/hidden # 加入预设到VIP的route route add -host $ipaddr dev $iface rc=$? case $rc in 0) ;; *) echo "ERROR: $CMD failed." ;; esac return $rc }

4.8、至此有关Heartbeat的设定已经完成,记得上述档案在Linux187与linux142上都要一模一样,然后

# setup

  将heartbeat的服务设定为激活,之后将两台机器重新开机。

5、在linux187上利用ipvsadm -l检查LVS的Routing Rule,是否如下所示:

[root@Linux187 root]# ipvsadm -l IP Virtual Server version 1.0.9 (size=65536) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.144.43.185:webcache rr -> Linux187:webcache Local 5 0 0 -> linux142:webcache Route 5 0 0 -> nb8048:webcache Route 5 0 0

  然后作些测试,将187的eth0 利用ifconfig eth0 down,看看有没有切换到142去。

6、设定至此看起来都没问题,但是每次Primary Diretcor设备如果网络突然断掉,然后Primary网络又突然恢复的时候,会造成Primary与Secondary Director上的LVS Server都同时激活。有兴趣的人可以在primary Diretcor上用

# ifconfig eth0 down 将Primary的网络卡down下来,等一会约一分钟,再用 #ifconfig eth0 up #route add -net 0.0.0.0 gw 10.144.43.254

  然后,你再连上linux142与linux187上,输入ipvsadm -l,就会发现两边的LVS/Direct Routing都激活了,这显然不是我们所希望的结果。

  要解决这个问题,需要利用mon这个dameon,来做到。

  想法如下:

(1)、每隔一段时间,去ping 10.144.43.254(ping gateway),若发现GATEWAY在连续六次都没有反应后,就要将lvs的服务关闭(/etc/init.d/lvs stop),此时认定是自己网络卡已经故障,无法Ping出去。因为即便是Gateway死掉,此时整个网络对外边已经没有作用,激活lvs也没用,故需要关闭他。

(2)、若发现Gateway突然又Ping的到了,此时要将heartbeat service给restart(取得主动权)(/sbin/service heartbeat start),如此经过一段时间,Primary的Director将会取得LVS Server的位置,而Slave Director会回复到RealServer与Backup Director的位置。

7、为了解决LVS server同时激活的困扰,我们需要在mon服务中再加入一个hostgroup,/etc/mon/mon.cf内容如下:

# # Extremely basic mon.cf file # # # global options # cfbasedir = /etc/mon pidfile = /var/run/mon.pid statedir = /var/run/mon/state.d logdir = /var/run/mon/log.d dtlogfile = /var/run/mon/log.d/downtime.log alertdir = /usr/lib/mon/alert.d mondir = /usr/lib/mon/mon.d maxprocs = 20 histlength = 100 randstart = 60s authtype = userfile userfile = /etc/mon/userfile # # group definitions (hostnames or IP addresses) # hostgroup server1 10.144.43.175 hostgroup server2 10.144.43.142 hostgroup server3 10.144.43.187 # network gateway hostgroup server4 10.144.43.254 watch server1 service webcache interval 5s monitor http.monitor -p 8080 -t 10 allow_empty_group period wd {Sun-Sat} alert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.175 -W 5 -F dr alertevery 1h alertafter 6 upalert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.175 -W 5 -F dr -u 1 watch server2 service webcache interval 5s monitor http.monitor -p 8080 -t 10 period wd {Sun-Sat} alert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.142 -W 5 -F dr alertafter 6 alertevery 1h upalert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.142 -W 5 -F dr -u 1 watch server3 service webcache interval 5s monitor http.monitor -p 8080 -t 10 period wd {Sun-Sat} alert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.187 -W 5 -F dr alertafter 6 alertevery 1h numalerts 24 upalert lvs.alert -P tcp -V 10.144.43.185:8080 -R 10.144.43.187 -W 5 -F dr -u 1 watch server4 service ping interval 10s # 使用哪一个monitor去作测试 monitor ping.monitor 10.144.43.254 period wd {Sun-Sat} # 每个小时丢一个alert alertevery 1h # 连续测试六次失败才丢出第一个alert alertafter 6 # 最多丢12个alert numalerts 12 # alert时呼叫heartbeat.alert alert heartbeat.alert # upalert时呼叫heartbeat.alert -u upalert heartbeat.alert -u # See /usr/doc for the original example...

8、从上面/etc/mon/mon.cf中,可以发现我们要自己写一个alert发生时被呼叫的script,这里我用perl 写了一个简单的Script放在(/usr/lib/mon/alert.d/heartbeat.alert)。

#!/usr/bin/perl # heartbeat.alert - Linux Virtual Server alert for mon # # It can be activated by mon to remove a real server when the # service is down , or add the server when the service is up # use Getopt::Std; getopts("u"); $service ="/sbin/service"; $u = $opt_u; if($opt_u){ # 重新激活heartbeat服务 system("$service heartbeat restart"); }else{ # 停止lvs server system("/etc/init.d/lvs stop"); }

9、测试系统

  确认linux187与linux 142的设定与档案文件相同,然后两边同时重新激活heartbeat service,至此Linux-HA系统正式完成,可以作些测试。例如:拔掉Director的网络线一段时间,看看Secondary有没有take over,然后再插回去,看看Primary有没有回复原本Director的身份,可试着将将Primary reboot,看看Secondary会不会take over,然后master起来的时候,应该还是Secondary Diretcorr在作用。其中或许会有些参数不大对,但是可以自己慢慢修正。

本文地址:http://com.8s8s.com/it/it23429.htm