RedHat下隧道模式的负载均衡

首先:隧道模式的负载均衡的工作原理:客户机访问director机,director机将客户端的请求发送到真实服务器,然后真实服务器将请求应回应的结果直接回应给客户机。真实服务器间可以是不通网段--即各服务器之间可以夸网。    这种方式不仅减轻director机的压力,效率高,还能在空间上得到运用。但在中国似乎暂时行不通(因为很少有企业用过...)。一般适用于大中型企业。

实验环境: 两台主机服务器:192.168.0.11   和  192.168.0.12
                     中转机: 192.168.0.13  和  192.168.0.19
                     客户机:192.168.0.10
            (主机名要跟IP保存一致。iptables要清空--iptables -F 。selinux 关闭)

实验步骤:
A.服务器:
    1。192.168.0.11
        # yum install -y httpd
        # service httpd start
        # echo "192.168.0.11" >/var/www/html/index.html
        # route add default gw 192.168.0.13
        # vim /etc/init.d/shutarp
            -------------------------------
            #!/bin/sh
            VIP=192.168.0.19

            . /etc/rc.d/init.d/functions

            case "$1" in
            start)
                echo "Tunl port starting"
                ifconfig tunl0 $VIP netmask 255.255.255.255 broadcast $VIP up
                /sbin/route add -host $VIP dev tunl0
                echo "1" >/proc/sys/net/ipv4/conf/tunl0/arp_ignore
                echo "2" >/proc/sys/net/ipv4/conf/tunl0/arp_announce
                echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
                echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
                sysctl -p

                ;;
            stop)
                echo "Tunl port closing"
                ifconfig tunl0 down
                echo 1 > /proc/sys/net/ipv4/ip_forward
                echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
                   ;;
                *)
            echo "Usage: $0 {start|stop}"
            exit 1
        esac
            -------------------------------
        # service shutarp start
    2。192.168.0.12
        # yum install -y httpd
        # service httpd start
        # echo "192.168.0.12" >/var/www/html/index.html
        # route add default gw 192.168.0.13
        # vim /etc/init.d/shutarp
            -------------------------------
            #!/bin/sh
            VIP=192.168.0.19

            . /etc/rc.d/init.d/functions

            case "$1" in
                start)
                    echo "Tunl port starting"
                    ifconfig tunl0 $VIP netmask 255.255.255.255 broadcast $VIP up
                    /sbin/route add -host $VIP dev tunl0
                    echo "1" >/proc/sys/net/ipv4/conf/tunl0/arp_ignore
                    echo "2" >/proc/sys/net/ipv4/conf/tunl0/arp_announce
                    echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
                    echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
                    sysctl -p

                    ;;
                stop)
                    echo "Tunl port closing"
                    ifconfig tunl0 down
                    echo 1 > /proc/sys/net/ipv4/ip_forward
                    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
                       ;;
                    *)
                echo "Usage: $0 {start|stop}"
                exit 1
            esac
        -------------------------------
        # service shutarp start
B.中转机:
    1。安装 ipvs服务:
        # yum clean all
        # yum install -y ipvsadm
    2。打开ip转发功能,关闭其他服务,设置中转
        # vim /etc/init.d/lvsDR
            ---------------------------------
            #!/bin/sh
            VIP=192.168.0.19
        RIP1=192.168.0.11
        RIP2=192.168.0.12
        GW=192.168.0.1

        . /etc/rc.d/init.d/functions

        case "$1" in
            start)
                echo " start LVS  of DirectorServer"
               /sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255 up
               /sbin/route add -host $VIP dev tunl0
               /sbin/ipvsadm -C
              /sbin/ipvsadm -A -t $VIP:80 -s rr
              /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -i
              /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -i
              /sbin/ipvsadm

                    ;;
            stop)
                echo "close LVS Directorserver"
                ifconfig tunl0 down
                /sbin/ipvsadm -C
                    ;;
                *)
            echo "Usage: $0 {start|stop}"
            exit 1
        esac

        ---------------------------------
        # service lvsDR start   (执行脚本)

        # vim /bin/shutarp
            -------------------------------
            #!/bin/bash
            VIP=192.168.0.19
            /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
            /sbin/route add -host $VIP dev lo:0
            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
            -------------------------------
        # shutarp

C。客户端:
    在浏览器中打开: http://192.168.0.19/   (重复刷新,看显示的页面:如果是不同页面说明实验成功!)