LVS负载均衡DR模型配置。

LVS的DR模型:简称直连路由模式:

此模式是通过改写请求报文的目标MAC地址,将请求发给真实服务器,而真实服务器将响应后的数据直接回应给请求的客户端,此模式要求LB与真实服务器需要有一块物理网卡都是连接在同一网段上

DR模型机制。
在这里插入图片描述
LVS的DR模型详解:

1:客户端发出请求数据包(源CIP 10.10.1.1:80 目的VIP1.1.1.1:802:LB接收到数据包后,不转换地址及端口,也不重新封装,只是将数据帧中的目的地址的MAC地址改写算法调度的真实机的MAC地址,将数据包转发出去

3:真实机收到数据后,但是发现数据包中目的地址不是自己(所以事先也得在LO上绑定一个VIP地址,同时配置抑制ARP),这样真实机做出响应直接回复客户端

4:客户端收到回复的数据包(源VIP 1.1.1.1:80 目的CIP 10.10.1.1:80,完成整个访问过程

注:LB只能改写目的MAC地址,因此它不能改变请求报文的目的端口端口,LVS只支持unix和linux,但集群节点服务器可以是win系统,此种模式配置相当麻烦

LVS的DR模式配置。

LB主机realserver主机
centos6centos7
10.5.100.94(物理ip)10.5.100.208(realserver1物理IP),10.5.100.207(realserver2)
10.5.100.10(vip)10.5.100.10(vip)

Centos6负载均衡上配置。

[root@centos6 ~]# ifconfig eth0:0 10.5.100.10/32 broadcast 10.5.100.10 up   配置vip地址到eth0接口的别名上面。
[root@centos6 ~]#route add -host 10.5.100.10 dev eth0:0   配置一条路由指定访问10.5.100.10的通过eth0的别名出去。
[root@centos6 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:C3:9E:67  
          inet addr:10.5.100.94  Bcast:10.5.100.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fec3:9e67/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:25512823 errors:0 dropped:0 overruns:0 frame:0
          TX packets:39575 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2320122981 (2.1 GiB)  TX bytes:4688278 (4.4 MiB)

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:C3:9E:67  
          inet addr:10.5.100.10  Bcast:10.5.100.10  Mask:0.0.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          
[root@centos6 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.5.100.10     0.0.0.0         255.255.255.255 UH    0      0        0 eth0
10.5.100.0      0.0.0.0         255.255.255.0   U     1      0        0 eth0
0.0.0.0         10.5.100.254    0.0.0.0         UG    0      0        0 eth0

RealServer1配置,arp_ignore和arp_announce是内核的两个参数,能够实现DR模型 ,realserver以httpd服务为列。
1:表示不接受任何广播相应,就算有地址也不响应。
2:表示禁止向外通告自己本机的地址信息。

解释:在realserver中配置vip的原因:

因为LVS的DR模型是通过修改目标MAC地址来进行数据转发的,
流程:客户端访问时经过路由器,经过交换机,这时交换机发出ARP广播,因为realserver配置的vip,所以通过内核参数来抑制realserver直接响应,交给负载均衡转发,然后负载均衡器广播,解析realserver的ARP解析,这时源MAC是DIPMAC,目标MAC是其中一台realserverMAC,但是目标地址不是realserver的地址。这时realserver通过物理网卡收到报文,然后再交给本机lo网卡,因为出去的报文必须经过物理网卡,但是这有一个问题,请求时没有请求rip啊,他不能响应啊,所以在realserver中配置一条路由,表示从哪进来了,就从那个网卡别名中出去,所以源地址为lo接口地址,转发给rip通过rip转发出去。

 [root@node2 ~]# echo " <h1>this is node2 real server</h1>" /var/www/html/index.html 
 [root@node2 ~]# systemctl restart httpd
 [root@node2 ~]# ss -tnl
 State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
 LISTEN      0      128                              *:22                                           *:*                  
 LISTEN      0      100                      127.0.0.1:25                                           *:*                  
 LISTEN      0      128                             :::80                                          :::*                  
 LISTEN      0      128                             :::22                                          :::*                  
 LISTEN      0      100                            ::1:25                                          :::*                  
 [root@node2 ~]# curl http://10.5.100.208
  <h1>this is node2 real server</h1>
 [root@node2 ~]# 
 [root@node2 ~]#echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
 [root@node2 ~]#echo 1 > /proc/sys/net/ipv4/conf/ens33/arp_ignore
 [root@node2 ~]#echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
 [root@node2 ~]#echo 2 > /proc/sys/net/ipv4/conf/ens33/arp_announce
 [root@node2 ~]#ifconfig eth0:0 10.5.100.10/32 broadcast 10.5.100.10 up 
 [root@node2 ~]#route add -host 10.5.100.10 dev lo:0 
 [root@node2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.5.100.208  netmask 255.255.255.0  broadcast 10.5.100.255
        inet6 fe80::a425:d3b4:3c87:c428  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ad:af:e0  txqueuelen 1000  (Ethernet)
        RX packets 25910498  bytes 2387879590 (2.2 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 35355  bytes 7121215 (6.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 48  bytes 4180 (4.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 48  bytes 4180 (4.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 10.5.100.10  netmask 0.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        
[root@node2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.5.100.254    0.0.0.0         UG    100    0        0 ens33
0.0.0.0         192.168.20.1    0.0.0.0         UG    101    0        0 ens36
10.5.100.0      0.0.0.0         255.255.255.0   U     100    0        0 ens33
10.5.100.10     0.0.0.0         255.255.255.255 UH    0      0        0 lo
192.168.20.0    0.0.0.0         255.255.255.0   U     101    0        0 ens36
[root@node2 ~]# 

Realserver2配置:与realserver1同理。

 [root@node1 ~]# echo " <h1>this is node1</h1>" /var/www/html/index.html 
 [root@node1 ~]# systemctl restart httpd
 [root@node1 ~]# ss -tnl
 State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
 LISTEN      0      128                              *:22                                           *:*                  
 LISTEN      0      100                      127.0.0.1:25                                           *:*                  
 LISTEN      0      128                             :::80                                          :::*                  
 LISTEN      0      128                             :::22                                          :::*                  
 LISTEN      0      100                            ::1:25                                          :::*                  
 [root@node1 ~]# curl http://10.5.100.207
  <h1>this is node1</h1>
 [root@node1 ~]#echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
 [root@node1 ~]#echo 1 > /proc/sys/net/ipv4/conf/eno6777736/arp_ignore
 [root@node1 ~]#echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
 [root@node1 ~]#echo 2 > /proc/sys/net/ipv4/conf/eno677736/arp_announce
 [root@node1 ~]#ifconfig eth0:0 10.5.100.10/32 broadcast 10.5.100.10 up 
 [root@node1 ~]#route add -host 10.5.100.10 dev lo:0 
 [root@node1 ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.5.100.207  netmask 255.255.255.0  broadcast 10.5.100.255
        inet6 fe80::20c:29ff:fed0:83ae  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d0:83:ae  txqueuelen 1000  (Ethernet)
        RX packets 26135462  bytes 2374192857 (2.2 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33240  bytes 12513985 (11.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno33554984: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.20.8  netmask 255.255.255.0  broadcast 192.168.20.255
        inet6 fe80::20c:29ff:fed0:83b8  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d0:83:b8  txqueuelen 1000  (Ethernet)
        RX packets 3482  bytes 382368 (373.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7062  bytes 549948 (537.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 1392  bytes 136218 (133.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1392  bytes 136218 (133.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 10.5.100.10  netmask 0.0.0.0
        loop  txqueuelen 0  (Local Loopback)
        
[root@node1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.5.100.254    0.0.0.0         UG    100    0        0 eno16777736
0.0.0.0         192.168.20.1    0.0.0.0         UG    101    0        0 eno33554984
10.5.11.2       10.5.100.254    255.255.255.255 UGH   100    0        0 eno16777736
10.5.100.0      0.0.0.0         255.255.255.0   U     0      0        0 eno16777736
10.5.100.0      0.0.0.0         255.255.255.0   U     100    0        0 eno16777736
10.5.100.10     0.0.0.0         255.255.255.255 UH    0      0        0 lo
192.168.20.0    0.0.0.0         255.255.255.0   U     100    0        0 eno33554984
[root@node1 ~]# 

配置负载均衡器。

[root@centos6 ~]# yum install ipvsadm -y
[root@centos6 ~]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@centos6 ~]# 
[root@centos6 ~]# ipvsadm -A -t 10.5.100.10:80 -s rr
[root@centos6 ~]# ipvsadm -a -t 10.5.100.10:80 -r 10.5.100.208 -g 
[root@centos6 ~]# ipvsadm -a -t 10.5.100.10:80 -r 10.5.100.207 -g 
[root@centos6 ~]# ipvsadm -s > /etc/sysconfig/ipvsadm
[root@centos6 ~]# service ipvsadm restart
ipvsadm: Clearing the current IPVS table:                  [  OK  ]
ipvsadm: Unloading modules:                                [  OK  ]
ipvsadm: Clearing the current IPVS table:                  [  OK  ]
ipvsadm: Applying IPVS configuration:                      [  OK  ]
[root@centos6 ~]# 

浏览器访问进行测试:负载均衡效果不是很好。
在这里插入图片描述
在这里插入图片描述
总结:LVS-dr:direct routing (gateway)
(1)保证前端路由器将目标ip为vip的请求报文发送给director;
解决方案:静态绑定 arptables 修改RS主机内核的参数
(2)RS的RIP可以使用私有地址:但也可以使用公网地址
(3)RS跟director必须在同一物理网络中
(4)请求报文经过由Director调度单响应报文一定不能经过Director;
(5)不支持端口映射
(6)RS可以大多数OS
(7)RS的网关不能指向DIP;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值