目录
一、LVS-DR数据包流向分析
为方便进行原理分析,将client与群集机器放在同一网络中,数据包流经的路线为1-2-3-4
Client向目标VIP发出请求,Director(负载均衡器)接受
IP包头及数据帧头信息
Director根据负载均衡算法选择Real Server_1,不修改也不封装IP报文,而是将数据帧的MAC地址改为Real Server_1的MAC地址,然后在局域网上发送
IP包头及数据帧头信息
RealServer_1收到这个帧,解封装后发现目标IP与本机匹配(RealServer事先绑定了VIP),于是处理这个报文。随后重新封装报文,将相应报文通过lo接口传送给物理网卡然后向外发出
IP包头及数据帧头信息
Client将收到回复报文。Client认为得到正常的服务,而不会知道是那一台服务器处理的
注意:如果跨网段,则报文通过路由器经由internet返回给客户
-
数据包流向分析
- 客户端发送请求到 Director Server(负载均衡器),请求的数据报文(源 IP 是 CIP,目标 IP 是 VIP)到达内核空间
- Director Server 和 Real Server 在同一个网络中,数据通过二层数据链路层来传输
- 内核空间判断数据包的目标IP是本机VIP,此时IPVS(IP虚拟服务器)比对数据包请求的服务是否是集群服务,是集群服务就重新封装数据包。修改源 MAC 地址为 Director Server 的 MAC地址,修改目标 MAC 地址为 Real Server 的 MAC 地址,源 IP 地址与目标 IP 地址没有改变,然后将数据包发送给 Real Server
- 到达 Real Server 的请求报文的 MAC 地址是自身的 MAC 地址,就接收此报文。数据包重新封装报文(源 IP 地址为 VIP,目标 IP 为 CIP),将响应报文通过 lo 接口传送给物理网卡然后向外发出
- Real Server 直接将响应报文传送到客户端
-
DR 模式的特点
- Director Server 和 Real Server 必须在同一个物理网络中
- Real Server 可以使用私有地址,也可以使用公网地址。如果使用公网地址,可以通过互联网对 RIP 进行直接访问
- Director Server作为群集的访问入口,但不作为网关使用
- 所有的请求报文经由 Director Server,但回复响应报文不能经过 Director Server
- Real Server 的网关不允许指向 Director Server IP,即Real Server发送的数据包不允许经过 Director Server
- Real Server 上的 lo 接口配置 VIP 的 IP 地址
二、LVS-DR中的ARP问题
- 在LVS-DR负载均衡集群中负载均衡器与节点服务器都要配置相同的VIP地址
- 在局域网中具有相同的IP地址,势必会造成个服务器ARP通信的紊乱
当ARP广播发送到LVS-DR集群时,因为负载均衡器和节点服务器都是连接到相同的网络上,他们都会接收到ARP广播
只有前端的负载均衡器进行相应,其他节点服务器不应该相应ARP广播
- 对节点服务器进行处理,使其不响应针对VIP的ARP请求
使用虚接口lo:0承载VIP地址
设置内核参数arp_ignore=1:系统只响应目的IP为本地IP的ARP请求
- RealServer返回报文(源IP是VIP)经路由器转发,重新封装报文时,需要先获取路由器的MAC地址
- 发送ARP请求时,Linux默认使用IP包的源IP地址(即VIP)作为ARP请求包中的源IP地址,而不使用发送接口的IP地址
-
问题
路由器根据ARP表项,会将新来的请求报文转发给RealServer,导致Director的VIP失效
对节点服务器进行处理,设置你和参数arp_announce=2:系统不使用IP包的源地址来设置ARP请求的源地址,而选择放松接口的IP地址
三、解决ARP的两个问题的设置方法
- 修改/etc/sysctl.conf文件
-
net.ipv4.conf.lo.arp_ignore=1 #节点服务器只响应的IP为物理网卡接口IP的ARP请求 net.ipv4.conf.lo.arp_announce=2 #节点服务器不使用IP包的源IP而采用发送接口的IP来作为ARP请求报文的源IP net.ipv4.conf.all.arp_ignore=1 net.ipv4.conf.all.arp_announce=2
四、案例:构建LVS-DR集群
准备四台服务器,所有服务器进行初始化操作
20.0.0.168:做LVS调度器
20.0.0.147 20.0.0.132:做nginx服务器
20.0.0.40:做NFS共享服务器
(1)服务器初始化
systemctl disable --now firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
(2)配置NFS服务器(20.0.0.111)
1)安装软件包
yum install -y nfs-utils rpcbind
2)创建共享目录和测试文件
mkdir /share/{xy101,xy102} -p
ls /share/
echo '<h1>this is xy101 test web page!</h1>' > /share/xy101/test.html
echo '<h1>this is xy102 test web page!</h1>' > /share/xy102/test.html
3)共享目录
vim /etc/exports
/share/xy101 20.0.0.0/24(ro)
/share/xy102 20.0.0.0/24(ro)
systemctl enable --now rpcbind nfs
showmount -e
(2)配置节点服务器(20.0.0.147/132)
1)两台节点服务器域yum安装nginx
cd /etc/yum.repos.d/
yum -y install nginx
2)两台节点分别挂载共享目录
20.0.0.147
systemctl start rpcbind
showmount -e 20.0.0.111
mount 20.0.0.40:/share/xy101 /usr/share/nginx/html/
df
20.0.0.132
mount 20.0.0.111:/share/xy102 /usr/share/nginx/html/
3)配置虚拟IP地址、修改内核参数和添加路由
20.0.0.147
配置虚拟IP
[root@jd ~]# cd /etc/sysconfig/network-scripts/
[root@jd network-scripts]# ls
ifcfg-ens33 ifdown-ipv6 ifdown-TeamPort ifup-ippp ifup-routes network-functions
ifcfg-lo ifdown-isdn ifdown-tunnel ifup-ipv6 ifup-sit network-functions-
ifdown ifdown-post ifup ifup-isdn ifup-Team
ifdown-bnep ifdown-ppp ifup-aliases ifup-plip ifup-TeamPort
ifdown-eth ifdown-routes ifup-bnep ifup-plusb ifup-tunnel
ifdown-ib ifdown-sit ifup-eth ifup-post ifup-wireless
ifdown-ippp ifdown-Team ifup-ib ifup-ppp init.ipv6-global
[root@jd network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@jd network-scripts]# vim ifcfg-lo:0
[root@jd network-scripts]# systemctl restart network
[root@jd ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.147 netmask 255.255.255.0 broadcast 20.0.0.255
inet6 fe80::e56a:befc:f559:2ae0 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fa:2d:5d txqueuelen 1000 (Ethernet)
RX packets 11441 bytes 4350447 (4.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8845 bytes 828205 (808.7 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 1000 (Local Loopback)
RX packets 1760 bytes 131376 (128.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1760 bytes 131376 (128.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 20.0.0.100 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 52:54:00:35:a5:84 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=20.0.0.100
NETMASK=255.255.255.255
ONBOOT=yes
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
# BROADCAST=127.255.255.255
修改内核参数
[root@jd ~]# vim /etc/sysctl.conf
[root@jd ~]# sysctl -p
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@jd network-scripts]# vim /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
#
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
添加路由
[root@jd ~]# route add -host 20.0.0.100 dev lo:0
[root@jd ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 20.0.0.2 0.0.0.0 UG 100 0 0 ens33
20.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
20.0.0.100 0.0.0.0 255.255.255.255 UH 0 0 0 lo
20.0.0.132
上面步骤重复一次
(3)配置LVS调度器服务器(20.0.0.168)
1)yum安装ipvsadm
yum install -y ipvsadm
2)开启ipvsadm
[root@lvs ~]# touch /etc/sysconfig/ipvsadm
[root@lvs ~]# systemctl start ipvsadm
[root@lvs ~]# systemctl enable ipvsadm
Created symlink from /etc/systemd/system/multi-user.target.wants/ipvsadm.service to /usr/lib/systemd/system/ipvsadm.service.
[root@lvs ~]# systemctl status ipvsadm
● ipvsadm.service - Initialise the Linux Virtual Server
Loaded: loaded (/usr/lib/systemd/system/ipvsadm.service; enabled; vendor preset: disabled)
Active: active (exited) since 二 2024-06-11 16:49:38 CST; 22s ago
Main PID: 71701 (code=exited, status=0/SUCCESS)
6月 11 16:49:38 lvs systemd[1]: Starting Initialise the Linux Virtual Server...
6月 11 16:49:38 lvs systemd[1]: Started Initialise the Linux Virtual Server.
Hint: Some lines were ellipsized, use -l to show in full.
3)添加模块
[root@lvs ~]# modprobe ip_vs
[root@lvs ~]# lsmod | grep ip_vs
ip_vs_rr 12600 1
ip_vs 145458 3 ip_vs_rr
nf_conntrack 139264 1 ip_vs
libcrc32c 12644 3 xfs,ip_vs,nf_conntrack
4)添加虚拟IP
[root@lvs ~]# cd /etc/sysconfig/network-scripts/
[root@lvs network-scripts]# ls
ifcfg-ens33 ifdown-ppp ifup-ib ifup-Team
ifcfg-lo ifdown-routes ifup-ippp ifup-TeamPort
ifdown ifdown-sit ifup-ipv6 ifup-tunnel
ifdown-bnep ifdown-Team ifup-isdn ifup-wireless
ifdown-eth ifdown-TeamPort ifup-plip init.ipv6-global
ifdown-ib ifdown-tunnel ifup-plusb network-functions
ifdown-ippp ifup ifup-post network-functions-ipv6
ifdown-ipv6 ifup-aliases ifup-ppp
ifdown-isdn ifup-bnep ifup-routes
ifdown-post ifup-eth ifup-sit
[root@lvs network-scripts]# cp ifcfg-lo ifcfg-ens33:0
[root@lvs network-scripts]# vim ifcfg-ens33:0
DEVICE=ens33:0
IPADDR=20.0.0.100
NETMASK=255.255.255.255
ONBOOT=yes
[root@lvs network-scripts]# systemctl restart network
[root@lvs network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.10 netmask 255.255.255.0 broadcast 20.0.0.255
inet6 fe80::947:89f3:4c57:3a9e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:53:65:31 txqueuelen 1000 (Ethernet)
RX packets 5079 bytes 2261795 (2.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2721 bytes 320440 (312.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.100 netmask 255.255.255.255 broadcast 20.0.0.100
ether 00:0c:29:53:65:31 txqueuelen 1000 (Ethernet)
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 648 bytes 56232 (54.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 648 bytes 56232 (54.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:8f:c7:54 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
5)修改内核参数
[root@lvs network-scripts]# vim /etc/sysctl.conf
在文件末行添加内容
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@lvs network-scripts]# sysctl -p
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
6)配置负载分配策略
[root@lvs network-scripts]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lvs network-scripts]# ipvsadm -A -t 20.0.0.100:80 -s rr
[root@lvs network-scripts]# ipvsadm -a -t 20.0.0.100:80 -r 20.0.0.147:80 -g
[root@lvs network-scripts]# ipvsadm -a -t 20.0.0.100:80 -r 20.0.0.132:80 -g
[root@lvs network-scripts]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP lvs:http rr
-> 20.0.0.147:http Route 1 0 0
-> 20.0.0.132:http Route 1 0 0
[root@lvs network-scripts]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 20.0.0.100:80 rr
-> 20.0.0.147:80 Route 1 0 0
-> 20.0.0.132:80 Route 1 0 0
(4)验证
关闭两台节点服务器的nginx长连接
[root@jd2 network-scripts]# vim /etc/nginx/nginx.conf
[root@jd2 network-scripts]# systemctl restart nginx