当前位置:   article > 正文

LVS--DR模式

LVS--DR模式

目录

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模式原理

  1. 当用户向负载均衡调度器(Director Server)发起请求,调度器将请求发往至内核空间

  2. PREROUTING链首先会接收到用户请求,判断目标IP确定是本机IP,将数据包发往INPUT链

  3. IPVS是工作在INPUT链上的,当用户请求到达INPUT时,IPVS会将用户请求和自己已定义好的集群服务进行比对,如果用户请求的就是定义的集群服务,那么此时IPVS会强行修改数据包里的目标IP地址及端口,并将新的数据包发往POSTROUTING链

  4. 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/965118
推荐阅读
相关标签
  

闽ICP备14008679号