原理
关于LVS的更多详细理论,可参考本人博客:LVS-NAT
- DR模式
直接路由
Direct Routing,简称DR模式;
采用半开放式的网络结构,与TUN模式的结构类似,但各节点并不是分散在各地,而是与调度器位于同一个物理网络;
负载调度器与各节点服务器通过本地网络连接,不需要建立专用的IP隧道
DR模式和NAT模式的不同 - NAT模式通过分发器做DNAT改变目标mac地址 数据进出必须经过分发器 realserver达到多台是会影响服务器的上传速度
DR直接路由解决了对分发器造成的负担 通过网关路由接收到用户的请求 通过分发器访问分发器IP 分发器看到目标地址和自己地址一样所以收下这个包 但是作为分发器不作处理 然后根据分发器选取一台realserver 将包转发过去 但是如果realserver要接受这个包必须也拥有和分发器同样的IP地址 最后由realserver直接发送给路由 数据出去不经过分发器 目标地址为客户端地址 源地址为分发器地址这样避免下次发包直接发给这台realserver 因为数据进来必须经过分发器 - DR和TUN的相同点:
都是各web节点直接相应客户机 - 不同点:
TUN各节点都有独立的公网地址,DR没有;
TUN各web节点和调度器是IP隧道的方式通讯,DR各web节点和调度器是局域网方式通讯;
TUN各web节点直接响应客户机,DR通过路由器;
项目部署
组成部分
LVS服务器: 1台
NFS服务器: 1台
web服务器: 2台
客户机
实际部署
- 在LVS服务器上的操作
1. 开启路由功能
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl –p
2. 加载LVS模块,并查看版本信息
modprobe ip_vs
cat /proc/net/ip_vs
3. 安装管理软件
yum -y install ipvsadm.x86_64
4. 编辑调度服务器脚本
vim dr.sh
#!/bin/bash
ifconfig ens33:0 192.168.31.100 broadcast 192.168.31.100 netmask 255.255.255.255 up
route add -host 192.168.31.100 dev ens33:0
ipvsadm -C
ipvsadm -A -t 192.168.31.100:80 -s rr
ipvsadm -a -t 192.168.31.100:80 -r 192.168.31.30:80 -g
ipvsadm -a -t 192.168.31.100:80 -r 192.168.31.50:80 -g
ipvsadm -Ln
5. 执行脚本
sh dr.sh
- NFS服务器上的部署
yum -y install nfs-utils rpcbind #rpcbind默认已安装
配置共享目录
mkdir /opt/web1
echo "<h1>wawawaw<h1>" > /opt/web1/index.html
mkdir /opt/web2
echo "<h1>hahahaha<h1>" > /opt/web2/index.html
资源共享
vim /etc/exports
/opt/web1 192.168.31.30/24(ro)
/opt/web2 192.168.31.50/24(ro)
将服务重启
systemctl restart nfs
systemctl restart rpcbind
查看资源共享情况
showmount –e
- web服务器上的部署
1. yum –y install httpd
2. 查询nfs资源共享情况
showmount -e 192.168.31.40
3. 挂载使用nfs共享资源
mount 192.168.31.40:/opt/web1 /var/www/html
4. 启动apache
systemctl start httpd
5. vim web1.sh
#!/bin/bash
ifconfig lo:0 192.168.31.100 broadcast 192.168.31.100 netmask 255.255.255.255 up
route add -host 192.168.31.100 dev lo:0
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p &> /dev/null
6. sh web1.sh
7. ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.31.30 netmask 255.255.255.0 broadcast 192.168.31.255
inet6 fe80::52ee:7827:1ca5:d67e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:5f:82:06 txqueuelen 1000 (Ethernet)
RX packets 9966 bytes 1092688 (1.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5609 bytes 692958 (676.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 1 (Local Loopback)
RX packets 204 bytes 20381 (19.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 204 bytes 20381 (19.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.31.100 netmask 255.255.255.255
loop txqueuelen 1 (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:26:08:49 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
另一台web服务器上
1. yum –y install httpd
2. 查询nfs资源共享情况
showmount -e 192.168.31.40
3. 挂载使用nfs共享资源
mount 192.168.31.40:/opt/web2 /var/www/html
4. 启动apache
systemctl start httpd
5. vim web2.sh
#!/bin/bash
ifconfig lo:0 192.168.31.100 broadcast 192.168.31.100 netmask 255.255.255.255 up
route add -host 192.168.31.100 dev lo:0
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p &> /dev/null
6. sh web2.sh
7. ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.31.50 netmask 255.255.255.0 broadcast 192.168.31.255
inet6 fe80::df4b:9ede:7f72:cd5c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:af:3c:82 txqueuelen 1000 (Ethernet)
RX packets 9588 bytes 976075 (953.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4956 bytes 541756 (529.0 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 1 (Local Loopback)
RX packets 21429 bytes 1799548 (1.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21429 bytes 1799548 (1.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.31.100 netmask 255.255.255.255
loop txqueuelen 1 (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:90:ba:94 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