24132eqw 发表于 2016-2-2 09:03:16

lvs中dr模式配置脚本

1 dr模式介绍
1.1 lvs的安装

安装详解:http://www.iyunv.com/thread-172647-1-1.html


1.2 lvs模式

lvs有三种模式dr,nat,tun。这里先介绍dr模式。

1.3 dr特点
1)Load Balancer和所有的Real Server在同一个局域网段上。
2)Real Server将请求处理后,直接返回给用户,不需要在通过调度器返回。

1.4 处理过程
    请求过程:客户 => vip => RealServer。
    响应过程:RealServer =>客户。

2 配置环境
2.1 LoadBalancer
DIP:192.168.142.133(eth0)。
VIP:192.168.142.211(eth0:1)。

2.2 RealServer1
DIP:192.168.142.130(eth0)。

2.3 RealServer2
DIP:192.168.142.131(eth0)。

3 配置脚本
3.1 配置LoadBalancer
# mkdir -p /usr/local/lvs
# cd /usr/local/lvs
# vi lvs-dr.sh


view plain copy

    #!/bin/sh
      
    # lvs-dr.sh
    #
    # lvs的dr模式LVS server脚本
    #
    #   
    # vip和rip必须在同一个网段。VS/DR通过改写请求报文的MAC地址,将请求的包发送到realserver上,
    # 不改变包的源和目标的IP地址,然后realserver直接回复客户端,不再经过LVS调度器,这样大大的减轻了LVS的负担。
      
    # set the vip and port
    VIP=192.168.142.211
    VPORT1=80
      
    # set the rip and port
    # web
    RIP1=192.168.142.130
    RIP2=192.168.142.131
    # port
    RPORT1=80
      
    Usage (){
      echo "Usage:`basename $0` (start|stop|status) "
      exit 1
    }
      
    if [ $# -ne 1 ];then
      Usage
    fi
      
    case $1 in
      start)
            echo "start LVS of DirectorServer"
            echo 1 > /proc/sys/net/ipv4/ip_forward
            
            # set the vip
            /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
            /sbin/route add -host $VIP dev eth0:1
            
            # clear ipvs table
            /sbin/ipvsadm -C
            
            # add lvs vip and port
            /sbin/ipvsadm -A -t $VIP:$VPORT1 -s rr
            
            # add rip and port
            /sbin/ipvsadm -a -t $VIP:$VPORT1 -r $RIP1:$RPORT1 -g -w 1
            /sbin/ipvsadm -a -t $VIP:$VPORT1 -r $RIP2:$RPORT1 -g -w 1
                     
            /sbin/ipvsadm -L -n
            ;;
            
      stop)
            echo "close LVS DirectorServer"
            /sbin/ipvsadm -C
            /sbin/ifconfig eth0:1 down
            ;;
      
         
      status)
            /sbin/ipvsadm -L -n
            ;;
               
         
      *)
            Usage
    esac


启动
#sh lvs-dr.sh start

3.2 配置RealServer1
# mkdir -p /usr/local/lvs

# cd /usr/local/lvs

# vi realServer.sh

view plain copy

    #!/bin/sh
      
    # realServer.sh
    #
    # lvs的dr模式RIP server脚本
    #   
      
    VIP=192.168.142.211
      
    #vip's broadcast
    BROADCAST=192.168.142.255   
      
    Usage ()
    {
      echo "Usage:`basename $0` (start|stop)"
      exit 1
    }
      
    if [ $# -ne 1 ];then
      Usage
    fi
      
    case $1 in
      start)
            echo "reparing for Real Server"
            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/eth0/arp_ignore
            echo "2" >/proc/sys/net/ipv4/conf/eth0/arp_announce
            /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $BROADCAST up
            /sbin/route add -host $VIP dev lo:0
            ;;
            
      stop)
            /sbin/ifconfig lo:0 down
            echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
            echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
            echo "0" >/proc/sys/net/ipv4/conf/eth0/arp_ignore
            echo "0" >/proc/sys/net/ipv4/conf/eth0/arp_announce
            echo "stop Real Server"
            ;;
         
      *)
            Usage
    esac


启动
#sh realServer.sh start


3.3 配置RealServer2
配置同理RealServer1。

4 建立web服务器测试

1)RealServer1与RealServer2安装与配置Nginx(详解:http://www.iyunv.com/thread-172648-1-1.html)


2)在RealServer1的web服务器根目录建立index.html

    # vi index.html
    if you see this page,then you know this ip is 192.168.142.130 and this page is from nginx!


3)在RealServer2的web服务器根目录建立index.html

    # vi index.html
    if you see this page,then you know this ip is 192.168.142.131 and this page is from nginx!

4)配置host或者dns。将my.domain.com指向VIP:192.168.142.211。

5)刷新页面将会看到不同的内同。




页: [1]
查看完整版本: lvs中dr模式配置脚本