LVS--DR模式

[复制链接]
发表于 2026-2-11 16:13:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目次

1 DR模式原理

2 DR模式哀求复兴过程

3 实验情况

4 开始实验

4.1 设置实验情况

4.2 Router 设置路由转发

4.3  LVS 设置转发规则

4.4 办理vip相应题目

4.5 Web1设置

4.6 Web2设置

5 测试结果


1 DR模式原理



  • 当用户向负载平衡调理器(Director Server)发起哀求,调理器将哀求发往至内核空间
  • PREROUTING链起首会汲取到用户哀求,判定目标IP确定是本机IP,将数据包发往INPUT链
  • IPVS是工作在INPUT链上的,当用户哀求到达INPUT时,IPVS会将用户哀求和本身已界说好的集群服务举行比对,假如用户哀求的就是界说的集群服务,那么此时IPVS会强行修改数据包里的目标IP地点及端口,并将新的数据包发往POSTROUTING链
  • POSTROUTING链汲取数据包后发现目标IP地点刚好是本身的后端服务器,那么此时通过选路,将数据包终极发送给后端的服务器

2 DR模式哀求复兴过程



   1、客户端向目标VIP发送哀求:客户端发起HTTP/HTTPS等协议的哀求,目标地点为假造IP(VIP)。
  
  2、负载平衡器汲取并处置处罚哀求:负载平衡器汲取到哀求后,会根据预设的负载平衡计谋(如轮询、权重、最少毗连数等),选择一个符合的后端服务器举行转发。
  
  3、数据包转发至后端服务器:负载平衡器不会更改IP报文的内容,而是将数据包的目标MAC地点更改为所选后端服务器的MAC地点,然后将数据包发送到后端服务器地点的网络。
  
  4、后端服务器处置处罚哀求和返回相应:后端服务器汲取到数据包后,查抄其目标IP地点是否与本身绑定的VIP类似,假如是,则处置处罚该哀求。将相应报文通过lo接口传送给物理网卡然后向外发出。处置处罚完成后,后端服务器将相应数据包直接发回给客户端
  
  5、客户端将收到复兴报文。客户端以为得到正常的服务,而不会知道是哪一台服务器处置处罚的
  
3 实验情况

序号                        源                                目标1 192.168.239.200/24
mac:client
  172.25.254.200/24
mac:Router-eth0
2 192.168.239.200/24
mac:Router-eth1
  172.25.254.200/24
mac:LVS
3 192.168.239.200/24
mac:LVS
  172.25.254.200/24
mac:web1
4 172.25.254.200/24
mac:web1-eth0
  172.25.254.100/24
mac:Router-eth1
5 172.25.254.200/24
mac:Router-eth0
  172.25.254.100/24
mac:client

4 开始实验

4.1 设置实验情况

  1. # Client
  2. [root@client ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
  3. [connection]
  4. id=eth0
  5. type=ethernet
  6. interface-name=eth0
  7. [ipv4]
  8. address1=192.168.239.200/24,192.168.239.100
  9. method=manual
  10. # Router
  11. [root@router ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
  12. [connection]
  13. id=eth0
  14. type=ethernet
  15. interface-name=eth0
  16. [ipv4]
  17. address1=192.168.239.100/24
  18. method=manual
  19. [root@router ~]# cat /etc/NetworkManager/system-connections/eth1.nmconnection
  20. [connection]
  21. id=eth1
  22. type=ethernet
  23. interface-name=eth1
  24. [ipv4]
  25. address1=172.25.254.100/24
  26. method=manual
  27. # LVS 配置
  28. [root@LVS ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
  29. [connection]
  30. id=eth0
  31. type=ethernet
  32. interface-name=eth0
  33. [ipv4]
  34. address1=172.25.254.50/24,172.25.254.100
  35. method=manual
  36. # web1
  37. [root@web1 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
  38. [connection]
  39. id=eth0
  40. type=ethernet
  41. interface-name=eth0
  42. [ipv4]
  43. address1=172.25.254.10/24,172.25.254.100
  44. method=manual
  45. # web2
  46. [root@web2 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
  47. [connection]
  48. id=eth0
  49. type=ethernet
  50. interface-name=eth0
  51. [ipv4]
  52. address1=172.25.254.20/24,172.25.254.100
  53. method=manual
复制代码

4.2 Router 设置路由转发

  1. # 虚拟机本身是不开启路由功能的,要想开启请在内核中开启路由转发功能
  2. [root@mysql-03 test]# sysctl -a | grep ip_forward
  3. net.ipv4.ip_forward = 0
  4. net.ipv4.ip_forward_update_priority = 1
  5. net.ipv4.ip_forward_use_pmtu = 0
  6. [root@mysql-03 test]# echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf
  7. [root@router ~]# sysctl -p
  8. net.ipv4.ip_forward = 1
复制代码
4.3  LVS 设置转发规则

  1. # 给环回接口填上
  2. [root@LVS test]# ip addr add 172.25.254.200/32 dev lo
  3. # 设置转发规则
  4. [root@LVS test]# ipvsadm -A -t 172.25.254.200:80 -s wrr
  5. [root@LVS test]# ipvsadm -a -t 172.25.254.200:80 -r 172.25.254.10 -w 1 -g
  6. [root@LVS test]# ipvsadm -a -t 172.25.254.200:80 -r 172.25.254.20 -w 1 -g
  7. # 查看规则
  8. [root@LVS ~]# ipvsadm -Ln
  9. IP Virtual Server version 1.2.1 (size=4096)
  10. Prot LocalAddress:Port Scheduler Flags
  11.   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  12. TCP  172.25.254.200:80 wrr
  13.   -> 172.25.254.10:80             Route   1      0          5         
  14.   -> 172.25.254.20:80             Route   1      0          6
复制代码

4.4 办理vip相应题目

   DR 模子中各主机上均必要设置 VIP ,办理地点辩论的方式有三种:  

  • (1)在前端网关做静态绑定
  • (2)在各RS利用arptables
  • (3)在各RS修改内核参数,来限定arp相应和告示的级别
限定相应级别 :arp_ignore

  • 0:默认值,表现可利用当地恣意接口上设置的恣意地点举行相应
  • 1:仅在哀求的目标IP设置在本田主机的汲取到哀求报文的接口上时,才给予相应
限定告示级别 :arp_announce

  • 0:默认值,把本机全部接口的全部信息向每个接口的网络举行告示
  • 1:只管制止将接口信息向非直接毗连网络举行告示
  • 2:必须制止将接口信息向非本网络举行告示

4.5 Web1设置

  1. # 增加VIP
  2. [root@web1 test]# ip addr add 172.25.254.200/32 dev lo
  3. # 查看网关是否指向路由器
  4. [root@web1 test]# route -n
  5. Kernel IP routing table
  6. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
  7. 0.0.0.0         172.25.254.100  0.0.0.0         UG    100    0        0 eth0
  8. 172.25.254.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
  9. [root@web1 test]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
  10. [root@web1 test]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
  11. [root@web1 test]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
  12. [root@web1 test]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
复制代码
4.6 Web2设置

  1. # 增加VIP
  2. [root@web2 test]# ip addr add 172.25.254.200/32 dev lo
  3. # 查看网关是否指向路由器
  4. [root@web2 test]# route -n
  5. Kernel IP routing table
  6. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
  7. 0.0.0.0         172.25.254.100  0.0.0.0         UG    100    0        0 eth0
  8. 172.25.254.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
  9. [root@web2 test]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
  10. [root@web2 test]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
  11. [root@web2 test]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
  12. [root@web2 test]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
复制代码

5 测试结果


  1. [root@client ~]# for i in {1..10}
  2. > do
  3. > curl 172.25.254.200
  4. > done
  5. this is web1
  6. this is web2
  7. this is web1
  8. this is web2
  9. this is web1
  10. this is web2
  11. this is web1
  12. this is web2
  13. this is web1
  14. this is web2
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表