当前位置:Linux教程 - Linux文化 - RedHat 8.0/9.0 LVS安裝手冊

RedHat 8.0/9.0 LVS安裝手冊


1、前言:

  RedHat在 9.0以后,就将ipvsadm这些套件去除,因此如果想使用LVS(Linux Virtual Server),就得自己重新编译核心(kernel),而编译核心是一件穷极无聊的事情,不管是make menuconfig、make xconfig、make config,触目所及都是一堆不是很懂的驱动程序,本文利用一些技巧,将compile kernel变得比较简单,但是如果你有一些不常见的硬设备,请自己想办法Compile,或者利用linux的load module功能自行将该硬件的驱动程序加载进kernel。

  Redhat 8.0选择完整安装时,预设已经会支持ipvs,但是他却不支持-NOARP这个网络卡设定选项,因为没有这个-NOARP选项,则LVS无法在同一个网段内设定LVS/DR与LVS/IP Tunnel,会遭遇ARP Problem,所以只好重新编译核心,顺便将kernel更新至比较新的版本,另外RedHat 8.0上比较新的核心,预设不支持ipvs。

  首先简单介绍一下LVS到底是什么东西,其实他是一种丛集(Cluster)的技术,透过支持IPVS的kernel,来达到LVS/Direct Routing (DR)、LVS/IP Tunnel、LVS/NAT的功能,详细内容请看LVS官方网站。而这些技术有什么用呢?其实就是拿来当成High Availability软件,Unix大厂SUN、HP、IBM、SCO等等都有提供这类的软件,但是都要很多钱,而我们没什么钱,只好用Open Source,而Open Source的好处,就是可以达到类似的功能,但不需要花钱。?

  以下安装内容主要参考下述网页,非常感谢这些提供基本知识的人,如果你不清楚LVS是什么,看看下面文章,可以有个头绪顺便练练基本的功夫,以免后面的内容你完全不晓得我再写什么。

http://linux.vbird.idv.tw/ 鸟哥的Linux私房菜(Linux基本常识) http://www.adj.idv.tw/server/linux_lvs.php 在RedHat 7.3上架设LVS http://www.linuxvirtualserver.org/ LVS的官方网站 http://www.linuxaid.com.cn/articles/2/0/202490941.shtml LinuxAid说明(上) http://www.linuxaid.com.cn/articles/8/0/807154206.shtml LinuxAid说明(下) http://mail.incredimail.com/howto/lvs/install/ Alex的网站,重要的patch都从这儿抓取

附带一提:

  如果你参考Linux Virtual Server的官方网站上的作法,我在REDHAT 8上编译了十多次的kernel也作不出来,后来发现是因为RedHat在kernel-2.4.20-19.8的source中将ipvs的程序代码拿掉,且CIPE版本也不对。

  目前最适合拿来当Linux Virtual Server的RedHat版本为7.3,其它的都不是很stable,有兴趣可以用google查查看。

  下列方法适用于2.4的kernel,主要在Redhat 8.0上使用,但是RedHat 9.0应该也差不多。

PART I :编译核心

1、首先安装RedHat 8.0

  选择全部安装。关闭一些不用的Service与并将vsftp激活。即着手进行kernel的重新编译,此时的kernel版本为2.4.18-18.4。

2、下载oprofile

  首先上google找寻oprofile版本要大于0.4的,因为在安装kernel 2.4.20-18.8.rpm的时候会需要这个rpm,如果找不到rpm,就搜寻oprofile-0.4-xx.src.rpm。

2.1、安装oprofile

# rpm -ivh oprofile-0.4-44.8.1.src.rpm # cd /usr/src/redhat/SPECS # vi oprofile.spec 将(BuildPreReq: kernel-source >=2.4.20)用# mark起来 # rpmbuild -bb oprofile.spec # cd /usr/src/redhat/RPMS/i386/ # rpm -ivh oprofile-0.4-44.8.1.i386.rpm(安装oprofile) 3、安装内核

  安装kernel 2.4.20-18.8的kernel,上http://ftp.nsysu.edu.tw/Linux/RedHat/updates/抓取kernel-2.4.20-18.8的rpm核心,因为我测试环境有三部,一部是我的notebook (P4 CPU、512MB RAM),一部是有4GB RAM与两颗CPU(PIII 700)的机器,一部是有1GB RAM与两颗有hyper threading P4 CPU的计算机。所以我总共抓了三个RPM,分别是

kernel-2.4.20-18.8.i686.rpm 02-Jun-2003 23:34 13.3M kernel-bigmem-2.4.20-18.8.i686.rpm 02-Jun-2003 23:34 13.9M kernel-smp-2.4.20-18.8.i686.rpm 02-Jun-2003 23:34 13.9M

  然后在各部机器上先安装上述RPM:若不清楚倒底要用那个,可以用uname -r

2.4.20-18.8bigmem (就用bigmem的版本) #rpm -ivh kernel-2.4.20-18.8.i686.rpm

  检查grub(/boot/grub/grub.conf)或lilo(/etc/lilo.conf)的设定是不是正确?

  如果正确,就重新激活机器(shutdown -i 6 now),用2.4.20-18.8开机,检查有无错误。

4、安装内核源码

  然后上http://www.redhat.com 抓取kernel-source-2.4.20-19.8.i386.rpm这个kernel的核心。利用

# rpm -ivh kernel-source-2.4.20-19.8.i386.rpm

  安装,他会建立两个目录:

/usr/src/linux-2.4.20-19.8 (directory) /usr/src/linux-2.4->/usr/src/linux-2.4.20-19.8 (Symbolic Link)

  但是我们还要另外建立一个symbolic link

#ln -s ./linux-2.4.20-19.8 ./linux

  因为等会要上ipvs patch的时候,他一定会link到/usr/src/linux这个目录,所以要另外建立这个Soft Link。

5、因为我们先前有安装过2.4.20-18.8的rpm文件,所以在/boot目录下,可以找到

-rw-r--r-- 1 root root 44764 5月 29 19:49 config-2.4.20-18.8

  这个档案,将这个档案copy至/usr/src/linux/.config

# cp /boot/config-2.4.20-18.8 /usr/src/linux/.config

  因为等会我要利用make oldconfig去作…他default会去读取/usr/src/linux/.config这个file

  注意:这就是我所说的技巧,因为我先前用make config、make menuconfig选取那些modules,但是因为数量实在太多,看得眼花撩乱,而且选完不知道为什么总有一两地方有问题无法compile成功,无意间被我发现2.4.20-18.8的config档案与2.4.20-19.8的config档案一模一样,加上kernel的README中又有写 make oldconfig会去读取,.config这个档案,所以我才会想到用这个方法或许可行,因为2.4.20-18.8都可以正常激活机器了,不可能用相同的config 档案,2.4.20-19.8就无法激活。其它版本,应该也差不多,先安装一个差不多版本的kernel,再用他的config去重新compile支持 ipvs的kernel。

6、给kernel的source file上Patch:

6.1、使用LVS的内核补丁

  上Linux Virtual Server的网页抓取kernel-2.4.0-20的PATCH档案下来,我是抓下面这个档案:

The IPVS Netfilter module for kernel 2.4 - Version 1.0.9 - May 21, 2003 (http://www.linuxvirtualserver.or ... /ipvs-1.0.9.tar.gz)   取得ipvs-1.0.9.tar.gz 这个档案

  我试过抓linux-2.4.20-ipvs-1.0.9.patch.gz 这个档案,但是从来没有compile成功过,所以放弃。

6.2、将ipvs-1.09.tar.gz解开,放在/usr/src/source/ipvs-1.0.9这个目录下,然后在这个目录下,输入

# make patchkernel # make installsource

  将IPVS的Patch加载到kernel的source中

6.3、加载 "hidden"的patch

  可以到http://mail.incredimail.com/howto/lvs/install/src/ 这个目录中抓取(hidden-2.4.20pre10-1.diff)这个patch档案,放在/usr/src/linux目录下,利用:

# cat hidden-2.4.20pre10-1.diff | patch -p1

  对Kernel进行patch,非常感谢Alex提供上述Patch。

  注意:为什么这里要对kernel进行hidden的patch,因为在使用LVS中的DR与IP Tunnel的时候,会需要有一块网络卡要设定两个IP的情形,但是Linux在2.2.14之后,就将eth0:1的-NOARP这个FLAG关闭。也就是说在kernel 2.2.14以后,eth0:1就视为eth0的别名,任何对eth0:1的设定也同样作用在eth0,换句话说,我对eth0:1下-NOARP,同样也会对eth0有作用,这样会使得整张网络卡收不到封包。

  在上述两种模式下,因为我所有的机器都放在同一个网段,当该网段的 Router接收到客户端(Client)对虚拟IP(Virtual IP)的TCP connection要求时,会先在网段中利用Arp request询问谁有VIP的地址,而包含Director与RealServers上所有的interface(不管Primary还是 Subinterface),只要他有那个ip,都会发送arp reply回去,造成网段内所有拥有Virtual IP的interface都会reply给Router,最后结果就是看谁的速度快,Router就将该封包送给谁,如此会造成LVS的Server并无法发挥其效果,因此需要利用hidden这个pattch,将Subinterface上的Virtual IP给隐藏起来,如此他就不会对Arp Request进行Reply,如此就可以解决ARP的问题,而这个NOARP的问题,kernel发展小组认为不重要,所以以后都不会修改,要用请自行编译。事实上,解法不只一种,我这儿只使用比较简单的一种。资料来源:(http://www.linuxvirtualserver.org )。

7、接着安装cipe 1.5.4的这个版本(如果已经安装,记得先移除后,改用下面这个rpm),http://mail.incredimail.com/howto/lvs/install/package/ 这个目录中有。cipe 1.4.5无法使用,因为1.4.5有Bug,所以一定要确定cipe的版本是1.5.4,且一定要安装cipe否则等一下无法compile ipvs的东西。

  注意:先用rpm -qa |grep cipe查查看是不是已经有cipe存在,如果存在,请用rpm -e cipe 将之移除,再用rpm -ivh cipe-xxx.rpm 安装。否则编译kernel的模块的时候会失败。

  所有准备工作到此告一段落。接着正式进入compile kernel的重要阶段。

8、开始compile kernel,

# cd /usr/src/linux # make oldconfig # (会去读取.config的资料,并会问你有关ipvs的一些设定,全部用M回答),按下Enter。 # make dep # make modules # make bzImage

  这里我顺序有点颠倒…可是无所谓…?

# make modules_install # make install

9、至于最后的make install,如果你用lilo开机,他会给你一个错误讯息,但是必要的档案已经都有了,如果是grub就没有问题。

-rw-r--r-- 1 root root 129736 7月 28 15:51 initrd-2.4.20-19.8custom.img lrwxrwxrwx 1 root root 28 7月 28 15:58 System.map -> System.map-2.4.20-19.8custom -rw-r--r-- 1 root root 519383 7月 28 15:51 System.map-2.4.20-19.8custom -rw-r--r-- 1 root root 1118581 7月 28 15:51 vmlinuz-2.4.20-19.8custom

10、如果用lilo开机,修改/etc/lilo.conf如下面格式:

prompt timeout=350 boot=/dev/hda map=/boot/map install=/boot/boot.b message=/boot/message lba32 # optional

other=/dev/hda1 optional label=WinXP-Home

image=/boot/vmlinuz-2.4.20-18.8 label=linux_old append="root=LABEL=/" read-only optional initrd=/boot/initrd-2.4.20-18.8.img image=/boot/vmlinuz-2.4.20-19.8custom label=linux_new root=/dev/hda5 append="devfs=mount" read-only optional initrd=/boot/initrd-2.4.20-19.8custom.img

  因为我的/是mount在/dev/hda5下,所以你只要稍加修改即可。

  如果是用grub开机,他会自动设定好/boot/grub.conf

default=1 timeout=10 splashimage=(hd0,0)/boot/grub/splash.xpm.gz title Red Hat Linux (2.4.20-19.8custom) root (hd0,0) kernel /boot/vmlinuz-2.4.20-19.8custom ro root=LABEL=/ initrd /boot/initrd-2.4.20-19.8custom.img title Red Hat Linux (2.4.20-18.8bigmem) root (hd0,0) kernel /boot/vmlinuz-2.4.20-18.8bigmem ro root=LABEL=/ initrd /boot/initrd-2.4.20-18.8bigmem.img title Red Hat Linux-smp (2.4.18-14smp) root (hd0,0) kernel /boot/vmlinuz-2.4.18-14smp ro root=LABEL=/ initrd /boot/initrd-2.4.18-14smp.img title Red Hat Linux-up (2.4.18-14) root (hd0,0) kernel /boot/vmlinuz-2.4.18-14 ro root=LABEL=/ initrd /boot/initrd-2.4.18-14.img

11、重新开机(shutdown -i 6 now)

12、选择linux_new(lilo)或Red Hat Linux (2.4.20-19.8custom)开机,看看开机过程有没有错误发生。然后检查/proc/sys/net/ipv4/conf/all/hidden是不是存在?

13、如果都没有错误,安装下列rpm

rpm -ivh ipvsadm-1.21-4.i386.rpm

14、试验一下 输入:

[root@nbaa boot]# ipvsadm IP Virtual Server version 1.0.9 (size=65536) Prot LocalAddressort Scheduler Flags -> RemoteAddressort Forward Weight ActiveConn InActConn

  出现上面画面就是成功啦!!!

15、后续的动作就依照你的网络状态,去作规划了,后续进行中。

PART II:LVS设定

系统架构

一、简易设定:

1、IP Tunnel的设定:

Load Balance : 10.144.43.175(nb804 Real Server:10.144.43.142(linux142)、10.144.43.187(linux187) Virtual IP :10.144.43.185(VIP)

Nb8048上面的设定:

1、先建立一个Subinterface

# ifconfig eth0:0 10.144.43.185 netmask 255.255.255.255 broadcast || 10.144.43.185 up

2、建立IP Tunnel的Rule

2.1、先打开IP Forwarding

# echo 1 > /proc/sys/net/ipv4/ip_forward

2.2、建立IP Tunnel的Rule,先建立Service

# ipvsadm -A -t 10.144.43.185:23 -s wlc

2.3、加入两台Real Server的IP

# ipvsadm -a -t 10.144.43.185:23 -r 10.144.43.187 -i # ipvsadm -a -t 10.144.43.185:23 -r 10.144.43.142 -i

linux142与linux187上面的设定:

1、另外建立一个Tunnel0

# ifconfig tunl0 10.144.43.185 netmask 255.255.255.255 broadcast 10.144.43.185

2、将host加入routing table

# route add -host 10.144.43.185 dev tunl0

3、将接口的hidden属性打开:

# echo 1 > /proc/sys/net/ipv4/ip_forward # echo 1 > /proc/sys/net/ipv4/conf/all/hidden # echo 1 > /proc/sys/net/ipv4/conf/tunl0/hidden

2、Direct Routing的设定

Load Balance:10.144.43.175 Virtual IP: 10.144.43.185 RealServer1:10.144.43.175(nb804 RealServer2:10.144.43.142(linux142) RealServer3:10.144.43.187(linux187)

  网络架构图如上所示,其中DR与RealServer1、RealServer2在同一个网段,其IP Address配置如附图,基本设定如下,包含避免ARP Problem的设定:

DR(Direct Routing Server):

# ifconfig eth0 10.144.43.175 netmask 255.255.255.0 broadcast 10.144.43.255 # route add -net 10.144.43.0 dev eth0

# ifconfig eth0:0 10.144.43.185 netmask 255.255.255.255 broadcast 10.144.43.185 # echo 1 > /proc/sys/net/ipv4/ip_forward # ipvsadm -A -t 10.144.43.185:23 -s rr (用Round Robin方式) # ipvsadm -a -t 10.144.43.185:23 -r 10.144.43.175 -g # ipvsadm -a -t 10.144.43.185 :23 -r 10.144.43.142 -g # ipvsadm -a -t 10.144.43.185 :23 -r 10.144.43.187 -g

RealServer1 与RealServer2的设定:

# ifconfig eth0 10.144.43.142 netmask 255.255.255.0 broadcast 10.144.43.255 # route add -net 10.144.43.0 dev eth0 # 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 # 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

测试:

  从10.16.1.16机器上,对10.144.43.185作telnet 测试,三次联机三次都连到不同的Server,所以测试正常。

二、进阶的设定:

  看完前面的设定,如果你有测试成功的话,一定会发现万一Real Server死掉了怎么办?负责转送封包的DR并不晓得Real Server已经故障无法使用,还是会将封包往故障的Real Server处送,这样会影响用户的使用权益,为了解决这个问题在LVS正式网站上有几种解法,而我安装过Piranha(RedHat的方法)与「mon、heartbeat、fake、coda」方法,发现piranha-gui需要apache 1.3的rpm档,而且还需要php 模块安装在上面,但是RedHat 8.0预设只提供apache 2.0.40 (httpd-2.0.40),因此在安装piranha-gui的时候一直安装不起来,我也试过将apache 1.3安装上去,但是还另外需要安装一堆有的没的RPM,所以我就决定放弃不用piranha,采用「mon、heartbeat、fake、coda」(http://www.linuxvirtualserver.org/HighAvailability.html )的解法。

  那因为我只有三部机器,且只有一部要当Director,所以hearbeat、fake对我的情形来说并不需要,另外我的档案也不需要全部统一放置在同一个地方,所以coda对我而言并没有用,最后我只需要安装mon这个服务(Daemon)。

  Mon的官方网站,在http://www.kernel.org/software/mon/,有兴趣可以去看看。废话不多说,开始进行安装。

1、安装服务

  安装可以采用tarball或rpm安装,但是因为tarball安装需要花比较多时间,为了省时间,所以全部采用RPM安装,只有perl-Mon模块采用tarball安装。

2、下载源码

  首先上http://rpmfind.net 搜寻下列档案,其中Mon-0.11.tar.gz请从Mon的正式网站上抓取。

fping-2.2b1-2.i386.rpm ------------------------ 可以一次Ping多个ip perl-Convert-BER-1.31-3.i386.rpm---------- perl Convert::BER module perl-Net-Telnet-3.01-9.i386.rpm-------------- perl Net::Telnet module Mon-0.11.tar.gz---------------------------------- perl Mon module perl-Period-1.20-9.i386.rpm ------------------ perl Period module mon-0.99.2-8.i386.rpm------------------------- mon daemon rpm

  然后

# rpm -ivh fping-2.2b1-2.i386.rpm # rpm -ivh perl-Convert-BER-1.31-3.i386.rpm # rpm -ivh perl-Net-Telnet-3.01-9.i386.rpm # rpm -ivh perl-Period-1.20-9.i386.rpm # gzip -dc Mon-0.11.tar.gz|tar xvf - # cd Mon-0.11 # perl Makefile.PL && make && make test && make install # cd .. # rpm -ivh -nodeps mon-0.99.2-8.i386.rpm

  用chkconfig -list mon 检查是否有安装mon daemon,

3、就这样安装完成了,接着进行mon的设定。

3.1、配置MON

  Mon的设定档放在/etc/mon/mon.cf,而在我们的例子中需要针对LVS写一个(lvs.alert)程序。这个程序主要是用来新增移除 Director的Routing Rule,如同在基本配置里面(这次的Director改为10.144.43.142这部机器),我们的设定:

ipvsadm -A -t 10.144.43.185:23 -s rr ipvsadm -a -t 10.144.43.185:23 -r 10.144.43.175 -g ipvsadm -a -t 10.144.43.185 :23 -r 10.144.43.142 -g ipvsadm -a -t 10.144.43.185 :23 -r 10.144.43.187 -g

  现在假设10.144.43.175网络断线,或机器当机不能正常工作,这时候mon daemon就会自动呼叫lvs.alert,

ipvsadm -d -t 10.144.43.185 :23 -r 10.144.43.175

  将这笔拿掉,然后持续监控,直到10.144.43.175 又正常工作时,再呼叫lvs.alert将10.144.43.175加入Route中。

#!/usr/bin/perl # # lvs.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("s:g:h:t:l:V:R:W:F:u";  $ipvsadm = "/sbin/ipvsadm";  $protocol =  $opt_P;  $virtual_services =  $opt_V;  $remote =  $opt_R; if( $opt_u){  $weight =  $opt_W; if( $opt_F eq "nat"{  $forwarding = "-m"; }elsif( $opt_F eq "tun"{  $forwarding = "-i"; }else{  $forwarding = "-g"; }

if( $protocol eq "tcp"{ system( " $ipvsadm -a -t  $virtual_services -r  $remote -w  $weight  $forwarding"; } else { system( " $ipvsadm -a -u  $virtual_services -r  $remote -w  $weight  $forwarding"; } } else { if( $protocol eq "tcp"{ system(" $ipvsadm -d -t  $virtual_services -r  $remote"; } else { system(" $ipvsadm -d -u  $virtual_services -r  $remote"; } }

3.2、mon daemon的设定

  因为我是以telnet的服务为测试范例,我真正上线使用的系统将会是Web server的监测,因此下面的设定只针对telnet来作设定,需要其它Service的请自行更正,并请自己去看mon的一些设定说明,这儿只简单说明下列的意思。

# # Extremely basic mon.cf file # 除了lvs.alert外,事实上还可发送Email,只是我把他拿掉了, # 因为我的Server并没有激活Sendmail.. # 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) # 要监测的机器,一次可以一部也可以多部 # 为了减少麻烦,而且我设备也不多,所以我设定一个group只有一个Server hostgroup server1 10.144.43.175 hostgroup server2 10.144.43.187 # 监测10.144.43.175 watch server1 # service 后面的telnet名称同/etc/services里面的名称,不是自己乱给的 service telnet # 间隔多久测试一次,这儿设定20秒,也可以设定10m、1h。 interval 20s # 用哪一个monitor程序去作,基本上telnet.monitor是用perl写的程。 # 所以如果有自己的monitor程序,也可以比照办理。 monitor telnet.monitor # 监测的时间 period wd {Mon-Fri} hr {7am-10pm} # alert:当有联机失败时会呼叫这个Event,执行后面lvs.alert程序 alert lvs.alert -P tcp -V 10.144.43.185:23 -R 10.144.43.175 -W 5 -F dr # uplaert:当当联机失败后,又发现对方活过来时候,会呼叫这个event。 upalert lvs.alert -P tcp -V 10.144.43.185:23 -R 10.144.43.175 -W 5 -F dr -u 1

watch server2 service telnet interval 20s monitor telnet.monitor period wd {Mon-Fri} hr {7am-10pm} alert lvs.alert -P tcp -V 10.144.43.185:23 -R 10.144.43.187 -W 5 -F dr upalert lvs.alert -P tcp -V 10.144.43.185:23 -R 10.144.43.187 -W 5 -F dr -u 1 # See /usr/doc for the original example...

3.3、修改完成后,就可以用service mon start,激活mon daemon,进行监测。

4、可以自己作一些测试,例如拔掉网络线,关掉Realserver上的服务,测试看看哪儿有问题。(呼!总算可以对老板交代了。^_^)

三、做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.  $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 LocalAddressort Scheduler Flags -> RemoteAddressort 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在作用。其中或许会有些参数不大对,但是可以自己慢慢修正。