赞
踩
目录
当用户向负载均衡调度器(Director Server)发起请求,调度器将请求发往至内核空间
PREROUTING链首先会接收到用户请求,判断目标IP确定是本机IP,将数据包发往INPUT链
IPVS是工作在INPUT链上的,当用户请求到达INPUT时,IPVS会将用户请求和自己已定义好的集群服务进行比对,如果用户请求的就是定义的集群服务,那么此时IPVS会强行修改数据包里的目标IP地址及端口,并将新的数据包发往POSTROUTING链
POSTROUTING链接收数据包后发现目标IP地址刚好是自己的后端服务器,那么此时通过选路,将数据包最终发送给后端的服务器
1、客户端向目标VIP发送请求:客户端发起HTTP/HTTPS等协议的请求,目标地址为虚拟IP(VIP)。
2、负载均衡器接收并处理请求:负载均衡器接收到请求后,会根据预设的负载均衡策略(如轮询、权重、最少连接数等),选择一个合适的后端服务器进行转发。
3、数据包转发至后端服务器:负载均衡器不会更改IP报文的内容,而是将数据包的目的MAC地址更改为所选后端服务器的MAC地址,然后将数据包发送到后端服务器所在的网络。
4、后端服务器处理请求和返回响应:后端服务器接收到数据包后,检查其目的IP地址是否与自己绑定的VIP相同,如果是,则处理该请求。将响应报文通过lo接口传送给物理网卡然后向外发出。处理完成后,后端服务器将响应数据包直接发回给客户端
5、客户端将收到回复报文。客户端认为得到正常的服务,而不会知道是哪一台服务器处理的
序号 | 源 | 目的 |
---|---|---|
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 |
- # Client
- [root@client ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
- [connection]
- id=eth0
- type=ethernet
- interface-name=eth0
-
- [ipv4]
- address1=192.168.239.200/24,192.168.239.100
- method=manual
-
-
- # Router
- [root@router ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
- [connection]
- id=eth0
- type=ethernet
- interface-name=eth0
-
- [ipv4]
- address1=192.168.239.100/24
- method=manual
- [root@router ~]# cat /etc/NetworkManager/system-connections/eth1.nmconnection
- [connection]
- id=eth1
- type=ethernet
- interface-name=eth1
-
- [ipv4]
- address1=172.25.254.100/24
- method=manual
-
-
- # LVS 配置
- [root@LVS ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
- [connection]
- id=eth0
- type=ethernet
- interface-name=eth0
-
- [ipv4]
- address1=172.25.254.50/24,172.25.254.100
- method=manual
-
-
- # web1
- [root@web1 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
- [connection]
- id=eth0
- type=ethernet
- interface-name=eth0
-
- [ipv4]
- address1=172.25.254.10/24,172.25.254.100
- method=manual
-
-
- # web2
- [root@web2 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
- [connection]
- id=eth0
- type=ethernet
- interface-name=eth0
-
- [ipv4]
- address1=172.25.254.20/24,172.25.254.100
- method=manual

- # 虚拟机本身是不开启路由功能的,要想开启请在内核中开启路由转发功能
-
- [root@mysql-03 test]# sysctl -a | grep ip_forward
- net.ipv4.ip_forward = 0
- net.ipv4.ip_forward_update_priority = 1
- net.ipv4.ip_forward_use_pmtu = 0
-
- [root@mysql-03 test]# echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf
- [root@router ~]# sysctl -p
- net.ipv4.ip_forward = 1
- # 给环回接口填上
- [root@LVS test]# ip addr add 172.25.254.200/32 dev lo
-
- # 设置转发规则
- [root@LVS test]# ipvsadm -A -t 172.25.254.200:80 -s wrr
- [root@LVS test]# ipvsadm -a -t 172.25.254.200:80 -r 172.25.254.10 -w 1 -g
- [root@LVS test]# ipvsadm -a -t 172.25.254.200:80 -r 172.25.254.20 -w 1 -g
-
- # 查看规则
- [root@LVS ~]# ipvsadm -Ln
- IP Virtual Server version 1.2.1 (size=4096)
- Prot LocalAddress:Port Scheduler Flags
- -> RemoteAddress:Port Forward Weight ActiveConn InActConn
- TCP 172.25.254.200:80 wrr
- -> 172.25.254.10:80 Route 1 0 5
- -> 172.25.254.20:80 Route 1 0 6

- # 增加VIP
- [root@web1 test]# ip addr add 172.25.254.200/32 dev lo
-
- # 查看网关是否指向路由器
- [root@web1 test]# route -n
- Kernel IP routing table
- Destination Gateway Genmask Flags Metric Ref Use Iface
- 0.0.0.0 172.25.254.100 0.0.0.0 UG 100 0 0 eth0
- 172.25.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
-
-
-
- [root@web1 test]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
- [root@web1 test]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
- [root@web1 test]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
- [root@web1 test]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

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

- [root@client ~]# for i in {1..10}
- > do
- > curl 172.25.254.200
- > done
- this is web1
- this is web2
- this is web1
- this is web2
- this is web1
- this is web2
- this is web1
- this is web2
- this is web1
- this is web2
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。