物理机:Dell 2950 环境: VIP 192.168.0.210 DS 192.168.0.211(master) 192.168.0.212(backup) RS 192.168.0.215 192.168.0.216 操作系统: vmware ESXi CentOS 4.6 x86_64 需要软件 libnet-1.1.2.1.tar.gz ipvsadm-1.24.tar.gz heartbeat-2.0.2.tar.gz LVS工作方式:DR(Direct Route)

架构图:

![](http://192.168.1.238/wp- content/uploads/2010/08/LVS.jpg)

DS配置:

tar xzvf libnet-1.1.2.1.tar.gz
cd libnet
./configure
make && make install
tar xzvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
ln -s /usr/src/kernels/2.6.9-67.EL-largesmp-x86_64/ /usr/src/linux
make && make install
groupadd -g 694 haclient
useradd -g haclient -d /dev/null -s /sbin/nologin -u 694 hacluster
tar xzvf heartbeat-2.0.2.tar.gz
cd heartbeat-2.0.2
./ConfigureMe configure
make && make install
cp doc/{authkeys,ha.cf,haresources} /etc/ha.d/
cp ldirectord/ldirectord.cf /etc/ha.d/
ln -s /usr/sbin/ldirectord /etc/ha.d/resource.d/
chmod 600 /etc/ha.d/authkeys
chkconfig --add heartbeat
chkconfig heartbeat on

编辑配置文件:

vim /etc/ha.d/ha.cf
logfile /var/log/ha-log
logfacility     local0
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport 694
ucast eth0 192.168.0.212 #配置另一方IP
auto_failback off
node    node1
node    node2
ping_group group1 192.168.0.211 192.168.0.212
respawn hacluster /usr/lib64/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
deadping 30

vim /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=yes
logfile="/var/log/ldirectord.log"
logfile="local0"
quiescent=no
virtual=192.168.0.210:80
        real=192.168.0.215:80 gate 2
        real=192.168.0.216:80 gate 2
        service=http
    virtualhost=www.linux.com
        scheduler=wrr
        #persistent=600
        #netmask=255.255.255.255
        protocol=tcp

vim /etc/ha.d/haresources
node1 \
LVSSyncDaemonSwap::master \
        IPaddr2::192.168.0.210/24/eth0/192.168.0.255 \
        ldirectord::ldirectord.cf

vim /etc/ha.d/authkeys
auth 3
3 md5 Hello!

Real Server 配置:

tar xzvf arptables-v0.0.3-3.tar.gz
cd arptables-v.0.0.3-3
make && make install



#!/bin/bash
# chkconfig: 2345 90 10
# description: Preparing for Load Balancer and Real Server switching
VIP=192.168.0.210
LOCAL_IP=192.168.0.215
BROADCAST=192.168.0.255  #vip's broadcast

. /etc/rc.d/init.d/functions
case "$1" in
    start)
     echo "reparing for Real Server"
       echo "1" > /proc/sys/net/ipv4/tcp_syncookies
       echo "0" > /proc/sys/net/ipv4/tcp_ecn
       echo "0" > /proc/sys/net/ipv4/conf/all/accept_redirects
       echo "0" > /proc/sys/net/ipv4/conf/all/accept_source_route
       echo "1" > /proc/sys/net/ipv4/tcp_synack_retries
       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
        echo "0" > /proc/sys/net/ipv4/ip_forward
        ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $BROADCAST up
        /sbin/route add -host $VIP dev lo:0
        /usr/local/sbin/arptables -F
        /usr/local/sbin/arptables -A INPUT -j DROP -d ${VIP}
        /usr/local/sbin/arptables -A OUTPUT -j mangle -o eth0 -s ${VIP} --mangle-ip-s ${LOCAL_IP}
        ;;
    stop)
        echo "reparing for Load Balancer"
        echo "1" > /proc/sys/net/ipv4/ip_forward
        ifconfig lo:0 down
        /usr/local/sbin/arptables -F
        ;;
    *)
        echo "Usage: lvs {start|stop}"
        exit 1
esac