要让路由知道哪台主机是Director,进行选择:

1、VIP:MAC(Director VIP) —— MAC绑定,但未必可行,例如路由是运营商的。

2、arptables

3、kernel parameter(常用此配置):

    arp_ignore:定义接收到ARP请求时的响应级别;

        0:默认级别,只要本机配置有相应的地址,就给予响应;

        1:只有当请求目标地址是请求到达接口上配置的地址,才给予响应;

    arp_announce:定义将自己的地址向外通告时的通告级别;

        0:默认级别,将本机任何接口上的任何地址向外通告;

        1:试图仅向目标网络通告与其网络匹配的地址;

        2:仅向与本地接口上的地址匹配的网络进行通告;


# 注意:各节点间的时间偏差不应该超出1秒钟。

# Director配置NTP服务器:

Director:

yum install ntp

/etc/init.d/ntpd start


Client:

/usr/sbin/ntpdate Director_IP


VS/DR拓扑示例:

            192.168.101.253

                10.10.10.1

                    Client

                        |

                        |

                  (router)

            192.168.101.254

                        |

                        |          VIP=192.168.101.168

                        | ————— Director

                        |              DIP=10.10.10.10

                        |

            —————————

            |                                |

            |                                |

RIP=10.10.10.11        RIP=10.10.10.12

          RS1                            RS2

VIP=192.168.101.168    VIP=192.168.101.168


        在如上图的VS/DR或VS/TUN应用的一种模型中(所有机器都在同一个物理网络),所有机器(包括Director和RealServer)都使用了一个额外的IP地址VIP。当一个客户端向VIP发出一个连接请求时,此请求必须要连接至Director的VIP,而不能是RealServer的。因为,LVS的主要目标就是要Director负责调度这些连接请求至RealServer的。

        因此,在Client发出至VIP的连接请求后,只能由Director将其MAC地址响应给客户端(也可能是直接与Director连接的路由设备),而Director则会相应的更新其ipvsadm table以追踪此连接,然后将其转发至后端的RealServer之一。

        如果Client在请求建立至VIP的连接时由某RealServer响应了其请求,则Client会在其MAC table中建立起一个VIP至RealServer的对应关系,并以至进行后面的通信。此时,在Client看来只有一个RealServer而无法意识到其它服务器的存在。

        为了解决此问题,可以通过在路由器上设置其转发规则来实现。当然,如果没有权限访问路由器并做出相应的设置,则只能通过传统的本地方式来解决此问题。这些方法包括:

1、禁止RealServer响应对VIP的ARP请求;

2、在RealServer上隐藏VIP,使得它们无法获知网络上的ARP请求;

3、基于"透明代理(Transparent Proxy)"或者"fwmark (firewall mark)";

4、禁止ARP请求发往RealServer。


        传统认为,解决ARP问题可以基于网络接口,也可以基于主机来实现。Linux采用了基于主机的方式,因为其可以在大多场景中良好的工作,但LVS却并不属于这些场景之一,因此,过去实现此功能相当麻烦。现在可以通过设置arp_ignore和arp_announce,这变得相对简单的多。

        Linux 2.2和2.4(2.4.26之前的版本)的内核解决"ARP问题"的方法各不相同,且比较麻烦。幸运的是,2.4.26和2.6的内核中引入了两个新的调整ARP栈的标志(device flags):arp_announce和arp_ignore。基于此,在DR/TUN的环境中,所有IPVS相关的设定均可使用arp_announce=2和arp_ignore=1/2/3来解决"ARP问题”。


VS/DR配置示例:

Director:

    eth0, DIP:10.10.10.10/24

    eth1, VIP:192.168.101.168/24    gw:192.168.101.254

RS1:

    eth0, RIP:10.10.10.11/24        gw:10.10.10.1

    lo:0, VIP:192.168.101.168/32

RS2:

    eth0, RIP:10.10.10.12/24        gw:10.10.10.1

    lo:0, VIP:192.168.101.168/32

Client:

    IP1:192.168.101.253/24

    IP2:10.10.10.1/24


# 注意:这里物理机(Client)VM8网卡配置两个IP地址,一个192.168.101.253,一个10.10.10.1;RS配置网关指向物理机的10.10.10.1。


1、Director安装ipvsadm

# yum install ipvsadm


2、RS1安装httpd

# yum install httpd

# echo "rs1.redhat.com" > /var/www/html/index.html

# service httpd start


3、RS2安装httpd

# yum install httpd

# echo "rs2.redhat.com" > /var/www/html/index.html

# service httpd start


4、解决ARP问题

# 注意:RS需先配置关闭arp请求响应arp_ignore或arp_announce,然后再配置VIP

# RS1、RS2配置相同:

# 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


5、RS1配置IP及路由

# ifconfig lo:0 192.168.101.168 broadcast 192.168.101.168 netmask 255.255.255.255 up

# route add -host 192.168.101.168 dev lo:0


6、RS2配置IP及路由

# ifconfig lo:0 192.168.101.168 broadcast 192.168.101.168 netmask 255.255.255.255 up

# route add -host 192.168.101.168 dev lo:0


7、Director配置VS/DR(调度算法:WLC)

# ifconfig eth1 192.168.101.168 broadcast 192.168.101.255 netmask 255.255.255.0 up

# route add default gw 192.168.101.254

# ipvsadm -A -t 192.168.101.168:80 -s wlc

# ipvsadm -a -t 192.168.101.168:80 -r 10.10.10.11 -g -w 2

# ipvsadm -a -t 192.168.101.168:80 -r 10.10.10.12 -g -w 1


8、浏览器访问:http://192.168.101.168


VS/DR Script:

### Director ###

#!/bin/bash

#

# chkconfig: - 90 10

# description: LVS Script for VS/DR.

#


VIP=192.168.101.168

RIP1=10.10.10.11

RIP2=10.10.10.12

GW=192.168.101.254


CSTATUS1() {

    [ -e /var/lock/subsys/ipvsadm.lock ] && echo -e "\033[32mipvsadm is running...\033[0m" && exit 1

}


CSTATUS2() {

    [ ! -e /var/lock/subsys/ipvsadm.lock ] && echo -e "\033[32mipvsadm is not running...\033[0m" && exit 2

}


case "$1" in

  start)

    CSTATUS1

    echo -e "\033[32mStart VS/DR of Director Server...\033[0m"

    # Set the Virtual IP address.

    /sbin/ifconfig eth1 $VIP broadcast $VIP netmask 255.255.255.0 up

    /sbin/route add -host $VIP dev eth1

    /sbin/route add default gw $GW

    # Director must open packet forwarding.

    #echo 1 > /proc/sys/net/ipv4/ip_forward

    # Clean all iptables rules.

    /sbin/iptables -F

    # Reset iptables counters.

    /sbin/iptables -Z

    # Clean all ipvsadm rules/services.

    /sbin/ipvsadm -C

    # Set VS/DR, Scheduling is Weighted Least Connection.

    /sbin/ipvsadm -A -t $VIP:80 -s wlc

    /sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g -w 2

    /sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w 1

    # Show VS/DR status.

    /sbin/ipvsadm -L -n

    /bin/touch /var/lock/subsys/ipvsadm.lock

    ;;

  stop)

    CSTATUS2

    echo -e "\033[32mStop VS/DR of Director Server...\033[0m"

    # Reset ipvsadm.

    /sbin/ipvsadm -C

    # Close VIP interface.

    /sbin/ifconfig eth1 down &> /dev/null

    # Close packet forwarding.

    #echo 0 > /proc/sys/net/ipv4/ip_forward

    /bin/rm -f /var/lock/subsys/ipvsadm.lock

    ;;

  status)

    [ -e /var/lock/subsys/ipvsadm.lock ] && echo -e "\033[32mipvsadm is running...\033[0m" || echo -e "\033[32mipvsadm is not running...\033[0m"

    ;;

  *)

    echo -e "\033[32mUsage: $0 {start|stop|status}\033[0m"

    ;;

esac


### RealServer ###

#!/bin/bash

#

# description: VS/DR Real Server.

#


VIP=192.168.101.168

GW=10.10.10.1


case "$1" in

  start)

    echo -e "\033[32mStart VS/DR of Real Server...\033[0m"

    /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

    /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up

    /sbin/route add -host $VIP dev lo:0

    #/sbin/route add default gw $GW

    ;;

 stop)

    echo -e "\033[32mStop VS/DR of Real Server...\033[0m"

    /sbin/ifconfig lo:0 down &> /dev/null

    #/sbin/route del default gw $GW

    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

    ;;

  status)

    CHECKLO=`/sbin/ifconfig lo:0 | grep $VIP`

    CHECKRO=`netstat -nr | grep lo | grep $VIP`

    if [ ! "$CHECKLO" -o ! "$CHECKRO" ];then

        echo -e "\033[32mReal Server VS/DR is not running...\033[0m"

    else

        echo -e "\033[32mReal Server VS/DR is Running...\033[0m"

    fi

    ;;

  *)

    echo -e "\033[32mUsage: $0 {start|stop|status}\033[0m"

    ;;

esac