http://freehat.blog.51cto.com/1239536/410299

 第一,系统规划                        Heartbeat +LVS+Ldirectord
1 需要的源码包
     ipvsadm-1.24.tar.gz  heartbeat-2.0.7.tar.gz libnet-1.1.2.1.tar.gz
      ipvsadm-1.24 需要linux 内核为2.6.18 ,同时也需要编译内核。
2.规划网络
   VIP 192.168.10.100
   HA1 192.168.10.10
        192.168.20.10
   HA2   192.168.10.20
         192.168.20.20
   RealServer1 192.168.10.1
   RealServer2 192.168.10.2
3 在heartbeat节点上修改主机名
 #vim /etc/sysconfig/network
HOSTNAME= ha1 并保存,只有重启或者注销时时才生效
 #hostname ha1 #立即生效
 # vim /etcsysconfig/network
HOSTNAME=ha2
 # hostname ha2
4 在heartbeat 节点上hosts 文件中添加如下
   192.168.10.10   ha1
   192.168.10.20 ha2
   192.168.20.10 ha010
   192.168.20.20 ha020
 
 
第二, 编译内核并让 linux-2.6.18.tar.gz
1 解压内核linux-2.6.18.tar.gz
 # tar xvf linux-2.6.18.tar.gz 
 #mv   linux-2.6.18 /usr/src/
2 因为linux系统默下是在寻找linux 目录所以创建连接
   #ln -sv linux-2.6.18   linux
3,拷贝/boot/config-2.6.18-164.el5 linux 目录下面
   #cp /boot/config-2.6.18-164.el5 /usr/src/linux/.config
4 编译内核
    如果以前编译过内核,并且想删除编译过的内核
    # make mrproper
5 编译内核的命为
      #make menuconfig 或者make xconfig  
6 选择内核支持IPvsadm
      Code maturity level options --->
[*] Prompt for development and/or incomplete code/drivers
Networking options --->
[*] Network packet filtering (replaces ipchains)
[ ] Network packet filtering debugging
...
IP: Netfilter Configuration --->
IP: Virtual Server Configuration --->
<M> virtual server support (EXPERIMENTAL)
[*] IP virtual server debugging
(12) IPVS connection table size (the Nth power of 2)
     --- IPVS scheduler
<M> round-robin scheduling
<M> weighted round-robin scheduling
<M> least-connection scheduling scheduling
<M> weighted least-connection scheduling
<M> locality-based least-connection scheduling
<M> locality-based least-connection with replication scheduling
<M> destination hashing scheduling
<M> source hashing scheduling
--- IPVS application helper
<M> FTP protocol helper
7 安装内核
     make bzImagemake modules make modules_install make install
8 编译完成后重启服务器,并选择启动编译后的内核,并查看编译后的内核是不是支持ipvsadm
#grep   ip_vs_init  /boot/System.map
第三.安装ipvsadm heartbeat
      1 安装ipvsadm
       # tar -vfx    ipvsadm-1.24.tar.gz
       #cd   ipvsadm-1.24
       #./configure && make &&make install
      2 安装libnet
        #tar xvf libnet-1.1.2.1.tar.gz 
       #cd libnet
       #./configure && make && make install
     3 安装heartbeat
        #tar –xvf heartbeat-2.0.7.tar.gz
        #cd heartbeat-2.0.7.
        #./configure && make && make install
        # cd doc/
        #cp authkeys   haresources  ha.cf /etc/ha.d/
        #cd ../ldirectord/
        #cp ldirectord.cf /etc/ha.d
      4 修改 authkeys ,并修改权限
         #vim authkeys
            auth 1
1 crc 
          #chmod 0600 authkeys
      5 配置heartbeat主配置文件
         #vim ha.cf
          debugfile /var/log/ha-debug # heartbeat的调试信息
logfile /var/log/ha-log    #hearbeat 的日志信息
logfacility local7
keepalive 2  #指明心态时间为2秒,即是每个两秒在eth1 放松一下广播
deadtime 30  #指定在三十秒内没有心跳信息,就立即切换服务
warntime 10  # 指明心跳延迟的时间为十秒,当十秒钟内备份机联系不上,即当前活动的服务器无心跳信号,就会在日志中写一个警告日志,但不会切换服务
nitdead 120   #网络启动的时间
udpport 694   #广播/单波通讯使用的udp端口
baud 19200    #使用串口的波特率,和serial以前使用
#mcast eth1 192.168.20.10 如果采用组播通讯,该处设置组播通讯所使用的接口
ucast eth1 10.90.20.10 #master lvs ip   如果采用的是单波,该处设置其网络接口集群使用的IP地址  注意在该处的IP 地址为 Master 主配置文件中为Back eth1 的IP地址 ,Back 在该处的地址为Master eth1 的地址
auto_failback on    # 在主节点回复正常情况下,主动接管资源
node ha10 #master lvs hostname  主节点 该处的节点名必须和uname –n 相匹配
node ha20 #slave lvs hostname   辅助接点
respawn root /usr/lib/heartbeat/ipfail   #have to give a permission
apiauth ipfail gid=root uid=root
 
6 配置heartbeat的资源文件
     #vim haresources 在其尾端添加
          Ha10 IPaddr:: 192.168.10.100 lvs ldirectord
对应的分别是:主节点   IPaddr:: 192.168.10.100 是虚拟Ip地址 lvs 是用来启动lvs 的脚本,ldirecord 是监听主节点和这是服务器的资源配置文件
 
7 配置ldirectord.cf
    checktimeout=20  #ldirectord 等待健康检查执行完毕的等待时间,如果在设置时间内没有完成检查,ldirectord 则会从ipvs 表中删除真实服务器
   checkinterval=60  #ldirectord 在两个检查之间的时间间隔
fallback=127.0.0.1:80  #当所有的真实服务器宕机时,所有的访问都放松的本主机上的apache服务上
autoreload=yes   #自动读取配置文件
logfile="/var/log/ldirectord.log" #ldirectord 的日志文件
logfile="local0"
quiescent=no  #当一个节点在checktimeout设置的时间周期内没有响应是它是静止的(它的权重为0),当你设置了这个选项后,ldirectord将会从IPVS表中移除真实服务器而不是停止它,从IPVS表移除节点将中断现有的客户端连接,并使LVS丢掉所有的连接跟踪记录和持续连接模板,如果你不将这个选项设置为no,当某个节点崩溃时,对某些客户端计算机而言可能会显示为集群关闭了,因为在这个节点崩溃前这些客户端计算机被分配给它了,而连接跟踪记录和程序连接模板仍然保留在Director上。
# A sample virual with a fallback that will override the gobal setting
virtual=192.168.10.100:80  #虚拟Ip 即使对外提供服务的ip地址
real=192.168.10.1.:80 gate   #真实服务器 当然gate 后面还可以指明权重
real=192.168.10.2:80 gate
# real=192.168.6.6:80 gate
fallback=127.0.0.1:80 gate
service=http  #服务类型
request=".ldirectord.html " #must be created on real server too  这是ldirectord 检测真实服务器是的服务器是否存活的文件
receive="OK" #this is .ldirectord.html content  #监测的文件中必须包含OK 这样的字符,ldirectord 才能识别该服务正常,否则为所失败
# virtualhost=some.domain.com.au
scheduler=wrr  # 此处要和配置的lvs 的调度算法一致
#persistent=600
#netmask=255.255.255.255
protocol=tcp
checktype=negotiate  # ldirectord 使用什么的方法检测这不是服务器,如果真实服务在设定的时间内没有返回,自己指定的信息,则该节点就被认为已经死掉了
 
8 配置友好页面返回信息,即使当所有的RealServer 出现问题的时,并能为用户提供一些友好的服务信息的页面
# cd /etc/ha.d/resource.d
 ln -vs /etc/init.d/httpd   ./
9 lvs 的启动脚本,并把该脚本拷贝到/etc/init.d/lvs ,且修改权限
注:该脚本是在调度器上执行的,即是heartbeat节点上执行的
   #vim lvs
   !/bin/bash
#chkconfig: 354 20 80
#description: start lvs
VIP=192.168.10.100
RIP1=192.168.10.1
RIP2=192.168.10.2
/etc/rc.d/init.d/functions
case "$1" in
                start)
                        echo "start lvs"
                        #set VIP
                        /sbin/ifconfig eth0:0 $VIP netmask 255.255.255.255
                        /sbin/route add -host $VIP dev eth0:0
                        #clean IPVS table
                        /sbin/ipvsadm -C
                        #set lvs
                                /sbin/ipvsadm -A -t $VIP:80 -s rr
                                /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
                                /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
                        #run lvs
                        /sbin/ipvsadm
                        #end
                        ;;
                stop)
                        echo "stop lvs"
                        /sbin/ipvsadm -C
                        ;;
                *)
                        echo "Usage:$0 {start|stop}"
                       ;;
                exit 1
esac
 #chmod 755 /etc/init.d/lvs
 
第四,配置RealServer ,即使真实服务器,并把下面的脚本拷贝到/etc/init.d/RealServer ,并修改权限
 注:该脚本是在真实服务器上启动的
     #vim RealServer
VIP=192.168.2.173
host=`/bin/hostname`
case "$1" in
                start)
                        #start lvs
                        /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
                        sysctl -p > /dev/null 2>&1
                        /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 up
                        /sbin/route add -host $VIP dev lo:0
                        ;;
                stop)
                        #stop lvs
                        /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
                        sysctl -p > /dev/null 2>&1
                        ;;
                status)
                        #status of lvs
                        islothere=`/sbin/ifconfig lo:0 |grep $VIP`
                        isrothere=`/netstat -rn |grep "lo" |grep "$VIP"`
                        if [ ! "islothere" -o ! "isrothere" ];then
                                        echo "the realserver is stopped"
                                else
                                        echo "realserver is running"
                        fi
                        ;;
                *)
                        echo "Usage:$0{start|stop|status}"
                  ;;
                exit 1
esac
 
# chmod 755 /etc/init.d/RealServer
并启动该脚本
 #/etc/init.d/lvs start
第五,
1.在各个真实服务器上的apache的家目录中创建文件
     #vim .ldirectord.html
          OK
   并启动apache
 2 .启动真实服务器上的RealServer 脚本
     #/etc/init.d/RealServer start
 3 启动heartbeat
   #/etc/init.d/lvs start
   #service heartbeat start
 
 
本文出自 “Gone with the wind” 博客,请务必保留此出处http://freehat.blog.51cto.com/1239536/410299