1. lvs-nat模式

1.1 nat的实现方式

  通过将请求报文的目标地址和目标端口修改为挑选出某RS的RIP和PORT来实现
  

1.2 nat实现的要点

(1) RIP和DIP应该使用私网地址,RS的网关应该指向DIP(在同一物理网络中);
(2) 请求和响应报文都要经由director转发;极高负载的场景中,Director可能会成为系统瓶颈;
(3) 支持端口映射;
(4) VS必须为Linux,RS可以是任意的OS;
(5) RS的RIP与Director的DIP必须在同一IP网络;

1.3 lvs-nat配置

Director:

  1. 本机的80端口不能对外提供服务
  2. VIP配置:
      172.18.253.120/16(对外提供服务的地址)
  3. DIP配置:
      IP/NETMASK:192.168.144.254/24(vmnet11)
      开启核心转发功能,指令中,参数值为1时启用ip转发,为0时禁止ip转发,在NAT模式下此操作是必须的
      方法一:echo "1" >/proc/sys/net/ipv4/ip_forward
      方法二:vim /etc/sysctl.conf,增加一行:net.ipv4.ip_forward = 1保存退出执行sysctl -p使配置生效。

RS1:
192.168.144.222/24(vmnet11) 安装httpd,并制作测试页,内容为RS1

RS2:
192.168.135.200/24(vmnet11) 安装httpd,并制作测试页,内容为RS2

1.4 拓扑图

lvs-nat与lvs-dr配置_第1张图片

1.5 各服务器具体配置:

Director的DIP网卡手动配置(vmnet11)
  IP:192.168.144.254
  NETMASK:255.255.255.0

RS1网卡手动配置为如下(vmnet11)
  IP:192.168.144.222
  NETMASK:255.255.255.0
  GATEWAY:192.168.144.254
RS2网卡手动配置为如下(vmnet11)
  IP:192.168.144.220
  NETMASK:255.255.255.0
  GATEWAY:192.168.144.254

网卡配置好之后,互ping测试网络是否通畅

1.6 在Director上配置LVS

#]~ ipvsadm -A -t 172.18.253.120:80 -s rr
#]~ ipvsadm -a -t 172.18.253.120:80 -r 192.168.144.222:80 -m    
#因nat模式支持端口映射,RS的端口可修改为其它端口
#]~ ipvsadm -a -t 172.18.253.120:80 -r 192.168.144.200:80 -m

1.7 查看当前LVS状况:

lvs-nat与lvs-dr配置_第2张图片

1.8 测试:

lvs-nat与lvs-dr配置_第3张图片
因LVS调试方法为round robin,故每次访问的RS不同。


2. lvs-dr模式

direct routing(直接路由) LVS默认类型

2.1 实现方式:

  通过为请求报文重新封装一个MAC首部进行转发,源MAC是DRP所在接口的MAC,目标MAC是挑选出的RS的RIP所在接口的MAC地址。;IP首部不会发生变化(源IP为CIP,目标IP始终为VIP);

2.2 lvs-dr实现要点

在各主机(Director,RS)均需配置VIP;因此,要解决地址的冲突问题,目标是让各RS上的VIP不可见,仅用于接收目标地址为VIP的报文,同时可作为响应报文的源地址。
为达到这一目标,有三种解决方案:
  (1)在前端的网关接口上静态绑定,需要有路由管理权限
  (2)在各RS上使用arptables,添加规则,拒绝自己的VIP向外通告以及响应arp地址解析的请求。
  (3)在各RS上修改内核参数,来限制arp响应和通告。

2.3 lvs-dr实现要点中的apr_ignore及apr_announce解释

限制响应级别:
  arp_ignore:控制系统在收到外部的arp请求时,是否要返回arp响应
  Define different modes for sending replies in response to received ARP requests that resolve local target IP addresses:
  0:(default): reply for any local target IP address, configured on any interface
使用本地所有接口上配置的地址进行响应
  1:reply only if the target IP address is local address configured on the incoming interface
    仅在请求的目标IP在本地主机的接入报文的接口上时,才给予响应。
  2:reply only if the target IP address is local address configured on the incoming interface and both with the sender's IP address are part from same subnet on this interface
    只响应目的IP地址为接收网卡上的本地地址的arp请求,并且arp请求的源IP必须和接收网卡同网段。
  3:do not reply for local addresses configured with scope host, only resolutions for global and link addresses are replied
    如果ARP请求数据包所请求的IP地址对应的本地地址其作用域(scope)为主机(host),则不回应ARP响应数据包,如果作用域为全局(global)或链路(link),则回应ARP响应数据包。

  4-7 – reserved
  保留位
  8:do not reply for all local addresses .The max value from conf/{all,interface}/arp_ignore is used when ARP request is received on the {interface}
    所有网络都不响应
限制通告级别:arp_announce
  0:(default) Use any local address, configured on any interface
  默认,把本地所有接口信息向所有接口公告

  1:Try to avoid local addresses that are not in the target's subnet for this interface. This mode is useful when target hosts reachable via this interface require the source IP address in ARP requests to be part of their logical network configured on the receiving interface. When we generate the request we will check all our subnets that include the target IP and will preserve the source address if it is from such subnet. If there is no such subnet we select source address according to the rules for level 2.
  尽量避免向非本网络通告

  2:Always use the best local address for this target. In this mode we ignore the source address in the IP packet and try to select local address that we prefer for talks with the target host. Such local address is selected by looking for primary IP addresses on all our subnets on the outgoing interface that include the target IP address. If no suitable local address is found we select the first local address we have on the outgoing interface or on all other interfaces, with the hope we will receive reply for our request and even sometimes no matter the source IP address we announce. The max value from conf/{all,interface}/arp_announce is used.
  忽略IP数据包的源IP地址,选择该发送网卡上最合适的本地地址作为arp请求的源IP地址。

2.4 设计要点

1、各主机有一个接口即可,但需要在同一物理网络中
2、RIP的网关不能指向DIP,RIP和DIP通常应该在同一网络,但此二者未必会与VIP在同一网络。
3、如果基于内核参数来设定,各RS需要先设置内核参数,再设置VIP及路由。

2.5 拓扑结构

lvs-nat与lvs-dr配置_第4张图片

2.6 dr模式中各服务器配置:

RS1与RS2上设置内核参数脚本:

#!/bin/bash
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
stop)
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
esac

Director地址配置:
ens33: 172.18.253.120/16 broadcast :172.18.253.120
ens33:0 172.18.253.119 netmask 255.255.255.255 broadcast 172.18.253.119
配置网卡命令:

~]# ifconfig ens33:0 172.18.253.119 netmask 255.255.255.255 broadcast 172.18.253.119

RS1地址配置
ens33:172.18.253.121/16

~]# lo:0:172.18.253.119 netmask 255.255.255.255 broadcast 172.18.253.119

添加路由表:

~]# route add -host 172.18.253.119 dev lo:0

开启httpd服务,编辑文件/var/www/html/index.html,内容为RS1
RS2地址配置
ens33:172.18.253.122/16
lo:0:172.18.253.119 netmask 255.255.255.255 broadcast 172.18.253.119
添加路由表:

~]# route add -host 172.18.253.119 dev lo:0

开启httpd服务,编辑文件/var/www/html/index.html,内容为RS2

2.7 定义LVS

~]# ipvsadm -A -t 172.18.253.119:80 -s rr
~]# ipvsadm -a -t 172.18.253.119:80 -r 172.18.253.121:80 -g
~]# ipvsadm -a -t 172.18.253.119:80 -r 172.18.253.122:80 -g

2.6.3 注:

RS上的设置也可直接通过脚本全部实现:

#!/bin/bash
vip=172.18.253.119
case $1 in
        start)
                echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
                echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
                echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
                echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
                /sbin/ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $vip
                /sbin/route add -host $vip dev lo:0
                echo "real server start OK"
                ;;
        stop)
                echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
                echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
                echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
                echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
                echo "real server stop ok"
                ;;
esac