本文共 6333 字,大约阅读时间需要 21 分钟。
通过将请求报文的目标地址和目标端口修改为挑选出某RS的RIP和PORT来实现
(1) RIP和DIP应该使用私网地址,RS的网关应该指向DIP(在同一物理网络中);
(2) 请求和响应报文都要经由director转发;极高负载的场景中,Director可能会成为系统瓶颈;(3) 支持端口映射;(4) VS必须为Linux,RS可以是任意的OS;(5) RS的RIP与Director的DIP必须在同一IP网络;Director:
RS1:
192.168.144.222/24(vmnet11) 安装httpd,并制作测试页,内容为RS1RS2:
192.168.135.200/24(vmnet11) 安装httpd,并制作测试页,内容为RS2Director的DIP网卡手动配置(vmnet11)
IP:192.168.144.254 NETMASK:255.255.255.0RS1网卡手动配置为如下(vmnet11)
IP:192.168.144.222 NETMASK:255.255.255.0 GATEWAY:192.168.144.254RS2网卡手动配置为如下(vmnet11) IP:192.168.144.220 NETMASK:255.255.255.0 GATEWAY:192.168.144.254网卡配置好之后,互ping测试网络是否通畅
#]~ 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
direct routing(直接路由) LVS默认类型
通过为请求报文重新封装一个MAC首部进行转发,源MAC是DRP所在接口的MAC,目标MAC是挑选出的RS的RIP所在接口的MAC地址。;IP首部不会发生变化(源IP为CIP,目标IP始终为VIP);
在各主机(Director,RS)均需配置VIP;因此,要解决地址的冲突问题,目标是让各RS上的VIP不可见,仅用于接收目标地址为VIP的报文,同时可作为响应报文的源地址。
为达到这一目标,有三种解决方案: (1)在前端的网关接口上静态绑定,需要有路由管理权限 (2)在各RS上使用arptables,添加规则,拒绝自己的VIP向外通告以及响应arp地址解析的请求。 (3)在各RS上修改内核参数,来限制arp响应和通告。限制响应级别:
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地址。1、各主机有一个接口即可,但需要在同一物理网络中
2、RIP的网关不能指向DIP,RIP和DIP通常应该在同一网络,但此二者未必会与VIP在同一网络。3、如果基于内核参数来设定,各RS需要先设置内核参数,再设置VIP及路由。RS1与RS2上设置内核参数脚本:
#!/bin/bashcase $1 instart)echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignoreecho 1 > /proc/sys/net/ipv4/conf/lo/arp_ignoreecho 2 > /proc/sys/net/ipv4/conf/all/arp_announceecho 2 > /proc/sys/net/ipv4/conf/lo/arp_announce;;stop)echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignoreecho 0 > /proc/sys/net/ipv4/conf/lo/arp_ignoreecho 0 > /proc/sys/net/ipv4/conf/all/arp_announceecho 0 > /proc/sys/net/ipv4/conf/lo/arp_announceesac
Director地址配置:
ens33: 172.18.253.120/16 broadcast :172.18.253.120ens33: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/16lo: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
~]# 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
RS上的设置也可直接通过脚本全部实现:
#!/bin/bashvip=172.18.253.119case $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
转载于:https://blog.51cto.com/11975865/2106368