LVS是一个关于服务器负载均衡的开源项目
下面是我的实施步骤:

没有采用NAT,应为不方便效率也比较低,我采用DR+TUN的方式,因为有台HP的机器(hppa)在另外一个网段,所以它用TUN,其他都是DR

所有机器就一块网卡eth0,括号是IP地址,VIP是10.60.65.222

   1. DIRECTOR-Master
      db3 (10.60.65.105)
      C2.4G/128M/Gentoo/2.6.19-gentoo-r5
   2. DIRECTOR-Slave
      druggo (10.60.65.68)
      AMD2500+/256M/Gentoo/2.6.20-gentoo-r4

   3. RS1
      magic (10.60.65.37)
      P2.4G/256M/MagicLinux/2.6.9
   4. RS2
      hppa (192.168.17.114)
      PA8500/2046M/Gentoo/2.6.20.1-hppa

DIRECTOR配置

安装带ldirectord的heartbeat以及ipvsadm,然后配置

1. /etc/ha.d/ha.cf

    logfacility     local0
    bcast   eth0
    auto_failback on
    node    db3 druggo

2. /etc/ha.d/haresources

db3     10.60.65.222/32/eth0/10.60.65.222       ldirectord

3. /etc/ha.d/authkeys

    auth 1
    1 crc

4. /etc/ha.d/conf/ldirectord.cf

   logfile = "/var/log/ldirectord.log"
    virtual = 10.60.65.222:80
            real=hppa:80 ipip 2
            #real=druggo:80 gate 2 (本来是RS,为了试试心跳所以……)
            real=magic:80 gate 3
            scheduler=wlc
            protocol = tcp
    virtual = 10.60.65.222:23
            real=hppa:23 ipip 1
            scheduler=wlc
            protocol = tcp

DIRECTOR-Master配置结束,Slave的配置直接拷贝Master的一模一样。

REALSERVER配置 magic 用DR方式,跑脚本 uplo

#!/bin/sh
ifconfig lo:0 10.60.65.222 broadcast 10.60.65.222 netmask 255.255.255.255 up
route add -host 10.60.65.222 dev lo:0
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/rp_filter

hppa 用TUN方式,需要编译ip-tunneling支持,然后modprobe ipip,这样就有tunl0了 跑脚本 uptunl

#!/bin/sh
ifconfig tunl0 10.60.65.222 broadcast 10.60.65.222 netmask 255.255.255.255 up
route add -host 10.60.65.222 dev tunl0
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/tunl0/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/tunl0/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/tunl0/rp_filter

至此配置完毕,可以开机了,先把magic和hppa上的apache打开,然后把db3和druggo上的心跳起动,一切就绪,在db3上运行

#ipvsadm

IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP 10.60.65.222:http wlc

-> magic:http                   Route   3      0          0
-> hppa:http                    Tunnel  2      0          0

TCP 10.60.65.222:telnet wlc

-> hppa:telnet                  Tunnel  1      0          0

访问VIP,正常工作,AB测试,按权分配连接。telnet服务也正常工作。

测试心跳,拔掉db3的网线,VIP不可访问,大约1分钟后,VIP正常,发现druggo已经工作。 插上db3的网线,VIP不可访问,大约1分钟后VIP正常,发现druggo停止,db3重新起动并工作。哈哈……