LVS-DR负载均衡群集搭建(原理加实操)很详细!
一:LVS-DR工作原理
1.1:DR模式概述
负载均衡群集工作模式–直接路由(Direct Routing)
●简称DR模式,采用半开放式的网络结构,与TUN模式的结构类似,但各节点并不是分散在各地,而是与调度器位于同一个物理网络
●负载调度器与各节点服务器通过本地网络连接,不需要建立专用的IP隧道
1.2: LVS-DR数据包流向
如图所示:
1.3: LVS-DR数据包流向分析
1.4: LVS-DR中存在的ARP问题
在局域网中具有相同的IP地址,势必会造成各服务器ARP通信的紊乱
●当一个ARP广播发送到LVS-DR集群时,因为负载均衡器和节点服务器都是连接到相同的网络上的,它们都会接收到ARP广播
●此时只有前端的负载均衡器进行响应,其他节点服务器不应该响应ARP广播
对节点服务器进行处理,使其不响应针对VIP的ARP请求
●使用虚接口lo:0承载ⅥP地址
●设置内核参数 arp_ignore=1:系统只响应目的IP为本地IP的ARP请求
■RealServe返回报文(源IP是VIP)经路由器转发,在重新封装报文时需要先获取路由器的MAC地址
- 发送ARP请求时,
Linux默认使用IP包的源IP地址(即VIP)作为ARP请求包中的源IP地址,而不使用发送接口(例如ens33)的IP地址
- 路由器收到ARP请求后,将更新ARP表项,原有的VIP对应 Director的MAC地址会被更新为ⅥP对应RealServer的MAC地址
- 此时新来的请求报文,路由器根据ARP表项,会将该报文转发给RealServer,从而导致 Director的VIP失效
解决办法
●对节点服务器进行处理,设置内核参数arp_announce=2:系统不使用IP包的源地址来设置ARP请求的源地址,而选择发送接口的IP地址
- 解决上述两个ARP问题的方法
修改/etc/sysctl.conf文件
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
1.5:路由器转发数据包的封装过程
二:LVS-DR部署实验
2.1:实验环境
一台调度器: VM1:192.168.100:41
二台WEB服务器集群池:192.168.100.42、192.168.100.43
一台NFS共享服务器:192.168.100.44
2.2:在调度器配置:192.168.100.41
2.2.1: 配置虚拟IP地址(VIP)
[aaa@qq.com ~]# cd /etc/sysconfig/network-scripts/
[aaa@qq.com network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[aaa@qq.com network-scripts]# vi ifcfg-ens33:0
NAME=ens33:0
DEVICE=ens33:0
ONBOOT=yesIPADDR=192.168.100.10
NETMASK=255.255.255.255
[aaa@qq.com network-scripts]# ifup ifcfg-ens33:0
[aaa@qq.com network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.41 netmask 255.255.255.0 broadcast 192.168.32.255
inet6 fe80::2fe2:b428:9454:6c2b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:9b:a7:cc txqueuelen 1000 (Ethernet)
RX packets 630 bytes 76327 (74.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 368 bytes 50671 (49.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.10 netmask 255.255.255.0 broadcast 192.168.32.255
ether 00:0c:29:9b:a7:cc 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 992 bytes 85912 (83.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 992 bytes 85912 (83.8 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:ca:42:28 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
2.2.2 : 调整/proc响应参数
-
对于 DR 群集模式来说,由于 LVS 负载调度器和各节点需要共用 VIP 地址,应该关闭 Linux 内核的重定向参数响应
-
服务器不是一台路由器,那么它不会发送重定向,所以可以关闭该功能
[aaa@qq.com network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[aaa@qq.com network-scripts]# sysctl -p ###生效
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
2.2.3 : 配置负载分配策略
[aaa@qq.com /]# ipvsadm -v
[aaa@qq.com ~]# modprobe ip_vs
[aaa@qq.com ~]# cat /proc/net/ip_vs
[aaa@qq.com ~]#yum -y install ipvsadm
[aaa@qq.com ~]#ipvsadm -A -t 192.168.100.10:80 -s rr
[aaa@qq.com ~]#ipvsadm -a -t 192.168.100.10:80 -r 192.168.100.42:80 -g -w 1
[aaa@qq.com ~]#ipvsadm -a -t 192.168.100.10:80 -r 192.168.100.43:80 -g -w 1
[aaa@qq.com network-scripts]# ipvsadm-save
[aaa@qq.com network-scripts]# systemctl enable ipvsadm
2.3: 配置存储服务器:192.168.100.44
rpm -q nfs-utils ###如果没装,yum -y install nfs-utils
rpm -q rpcbind ###如果没装,yum -y install rpcbind
[aaa@qq.com ~]# systemctl start nfs
[aaa@qq.com ~]# systemctl start rpcbind
[aaa@qq.com ~]# vi /etc/exports
/opt/51xit 192.168.100.0/24 (rw,sync)
/opt/52xit 192.168.100.0/24 (rw,sync)
[aaa@qq.com ~]# systemctl restart nfs
[aaa@qq.com ~]# systemctl restart rpcbind
[aaa@qq.com ~]# systemctl enable nfs
[aaa@qq.com ~]# systemctl enable rpcbind
[aaa@qq.com ~]# mkdir /opt/51xit /opt/52xit
[aaa@qq.com ~]# echo "this is 51xit" >/opt/51xit/index.html
[aaa@qq.com ~]# echo "this is 52xit" >/opt/52xit/index.html
2.4: 配置节点服务器:192.168.100.42
2.4.1 : 配置虚拟IP地址
[aaa@qq.com ~]# cd /etc/sysconfig/network-scripts/
[aaa@qq.com network-scripts]# cp ifcfg-lo ifcfg-lo:0
[aaa@qq.com network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.10
NETMASK=255.255.255.255
ONBOOT=yes
[aaa@qq.com network-scripts]# ifup lo:0
[aaa@qq.com network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.42 netmask 255.255.255.0 broadcast 192.168.32.255
inet6 fe80::8edf:281f:bd34:b245 prefixlen 64 scopeid 0x20<link>
ether 00:50:56:22:ca:a7 txqueuelen 1000 (Ethernet)
RX packets 787 bytes 91078 (88.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 366 bytes 54581 (53.3 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 884 bytes 76416 (74.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 884 bytes 76416 (74.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.100.10 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
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:ca:42:28 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
[aaa@qq.com network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.100.10 dev lo:0 重启以后自动加载vip地址,如果不加的话,下次重启时需要重新配置虚拟ip地址
[aaa@qq.com network-scripts]# route add -host 192.168.100.10 dev lo:0
2.4.2:调整/proc响应参数
[aaa@qq.com network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[aaa@qq.com network-scripts]# sysctl -p
2.4.3:安装httpd 挂载测试页
[aaa@qq.com ~]# showmount -e 192.168.100.44 ####如果还没发布,请到存储服务器发布下,exportfs -rv
Export list for 192.168.100.44:
/opt/51xit (everyone)
/opt/52xit (everyone)
[aaa@qq.com ~]# yum -y install httpd
[aaa@qq.com ~]# mount 192.168.100.44:/opt/51xit /var/www/html/
[aaa@qq.com ~]# vi /etc/fstab
192.168.100.44:/opt/51xit/ /var/www/html/ nfs rw,tcp,intr 0 1 ###开机自动挂载,注意格式对齐
[aaa@qq.com ~]# systemctl start httpd
[aaa@qq.com ~]# systemctl enable httpd
2.5: 配置节点服务器:192.168.100.43
2.5.1:配置虚拟IP地址
[aaa@qq.com ~]# cd /etc/sysconfig/network-scripts/
[aaa@qq.com network-scripts]# cp ifcfg-lo ifcfg-lo:0
[aaa@qq.com network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.10
NETMASK=255.255.255.255
ONBOOT=yes
[aaa@qq.com network-scripts]# ifup lo:0
[aaa@qq.com network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.43 netmask 255.255.255.0 broadcast 192.168.32.255
inet6 fe80::8edf:281f:bd34:b245 prefixlen 64 scopeid 0x20<link>
ether 00:50:56:22:ca:a7 txqueuelen 1000 (Ethernet)
RX packets 787 bytes 91078 (88.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 366 bytes 54581 (53.3 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 884 bytes 76416 (74.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 884 bytes 76416 (74.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.100.10 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
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:ca:42:28 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
[aaa@qq.com network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.100.10 dev lo:0
[aaa@qq.com network-scripts]# route add -host 192.168.100.10 dev lo:0
2.5.2: 调整/proc响应参数
[aaa@qq.com network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[aaa@qq.com network-scripts]# sysctl -p
2.5.3: 安装httpd 挂载测试页
[aaa@qq.com ~]# showmount -e 192.168.100.44 ####如果还没发布,请到存储服务器发布下,exportfs -rv
Export list for 192.168.100.44:
/opt/accp (everyone)
/opt/bdqn (everyone)
[aaa@qq.com ~]# yum -y install httpd
[aaa@qq.com ~]# mount 192.168.100.44:/opt/52xit /var/www/html/
[aaa@qq.com ~]# vi /etc/fstab
192.168.100.44:/opt/52xit/ /var/www/html/ nfs rw,tcp,intr 0 1 ###开机自动挂载,注意格式对齐
[aaa@qq.com ~]# systemctl start httpd
[aaa@qq.com ~]# systemctl enable httpd
2.6: 测试
登录192.168.100.43测试网站是否正常
测试节点服务器是否正常
测试网站
192.168.100.10
打开浏览器进行访问节点服务器的vip地址,出现“this is 51xit”
再重新访问,通过LVS的负载均衡算法:轮询,按照顺序轮流分配给群集中各节点,出现下一个节点“this is 52xit”
上一篇: 通过Nginx实现负载均衡
下一篇: oracle本地备份远程数据库教程