隧道模式

  • IP隧道(IP tunneling)是将一个IP报文封装在另一个IP报文的技术,这可以使得目标为一个IP地址的数据报文能被封装和转发到另一个IP地址。
  • IP隧道技术亦称为IP封装技术(IP encapsulation)。
  • IP隧道主要用于移动主机和虚拟私有网络(Virtual Private Network),在其中隧道都是静态建立的,隧道一端有一个IP地址,另一端也有唯一的IP地址

实验环境

主机 ip
server1(调度器) 172.25.5.1
server2(后端apache服务器) 172.25.5.2
server3(后端apache服务器) 172.25.5.3
foundation4(客户端,测试端) 172.25.5.250

1.配置TUN模式给server1

  • 添加隧道
  • 隧道添加对外暴露的VIP
  • 隧道**
[[email protected] ~]# modprobe ipip   ##加载模块

##删除上一个实验绑定的vip,此处可忽略
[[email protected] ~]# ip addr del 172.25.64.100/24 dev eth0
[[email protected] ~]# ip addr add 172.25.64.100/24  dev tunl0
 
[[email protected] ~]# ip link set up tunl0

LVS模式二:TUN隧道模式
LVS模式二:TUN隧道模式

  • 在server1上清除之前的策略重新添加新的策略
[[email protected] ~]# ipvsadm -C   ##清除策略
[[email protected] ~]# ipvsadm -A -t 172.25.5.100:80 -s rr    #添加vip,对后端服务器采用rr算法
[[email protected] ~]# ipvsadm -a -t 172.25.5.100:80 -r 172.25.64.2:80 -i    #添加后端真实服务器server2
[[email protected] ~]# ipvsadm -a -t 172.25.5.100:80 -r 172.25.64.3:80 -i    #添加后端真实服务器server3

[[email protected] ~]# /etc/init.d/ipvsadm save    ##保存策略

LVS模式二:TUN隧道模式
LVS模式二:TUN隧道模式
LVS模式二:TUN隧道模式

2.配置后端apache服务器(server3,server2)

server2

  • 添加隧道
  • 隧道添加对外暴露的VIP
  • 隧道**
    LVS模式二:TUN隧道模式
    LVS模式二:TUN隧道模式
  • 在server2上修改rp_filter参数
    LVS模式二:TUN隧道模式
[[email protected] html]# sysctl -a |grep rp_filter   ##关闭反向
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.all.arp_filter = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.arp_filter = 0
net.ipv4.conf.lo.rp_filter = 1
net.ipv4.conf.lo.arp_filter = 0
net.ipv4.conf.eth0.rp_filter = 1
net.ipv4.conf.eth0.arp_filter = 0
net.ipv4.conf.tunl0.rp_filter = 1
net.ipv4.conf.tunl0.arp_filter = 0

##将参数为1的都改为0
[[email protected] html]# sysctl -w net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.rp_filter = 0
[[email protected] html]# sysctl -w net.ipv4.conf.lo.rp_filter=0
net.ipv4.conf.lo.rp_filter = 0
[[email protected] html]# sysctl -w net.ipv4.conf.eth0.rp_filter=0
net.ipv4.conf.eth0.rp_filter = 0
[[email protected] html]# sysctl -w net.ipv4.conf.tunl0.rp_filter=0
net.ipv4.conf.tunl0.rp_filter = 0

[[email protected] html]# sysctl -a |grep rp_filter
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.all.arp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_filter = 0
net.ipv4.conf.lo.rp_filter = 0
net.ipv4.conf.lo.arp_filter = 0
net.ipv4.conf.eth0.rp_filter = 0
net.ipv4.conf.eth0.arp_filter = 0
net.ipv4.conf.tunl0.rp_filter = 0
net.ipv4.conf.tunl0.arp_filter = 0

注意:重新加载后,有一个参数始终不为0,需要在文件中修改

[[email protected] html]# sysctl -p   ##重新加载后
[[email protected] html]# sysctl -a |grep rp_filter
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.all.arp_filter = 0
net.ipv4.conf.default.rp_filter = 1   ##始终不为0,则这个需要在文件中改
net.ipv4.conf.default.arp_filter = 0
net.ipv4.conf.lo.rp_filter = 0
net.ipv4.conf.lo.arp_filter = 0
net.ipv4.conf.eth0.rp_filter = 0
net.ipv4.conf.eth0.arp_filter = 0
net.ipv4.conf.tunl0.rp_filter = 0
net.ipv4.conf.tunl0.arp_filter = 0

##在文件中修改
[[email protected] html]# vim /etc/sysctl.conf 
# Controls source route verification
net.ipv4.conf.default.rp_filter = 0

[[email protected] html]# sysctl -p

[[email protected] html]# sysctl -a |grep rp_filter
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.all.arp_filter = 0
net.ipv4.conf.default.rp_filter = 0  ##修改成功了
net.ipv4.conf.default.arp_filter = 0
net.ipv4.conf.lo.rp_filter = 0
net.ipv4.conf.lo.arp_filter = 0
net.ipv4.conf.eth0.rp_filter = 0
net.ipv4.conf.eth0.arp_filter = 0
net.ipv4.conf.tunl0.rp_filter = 0
net.ipv4.conf.tunl0.arp_filter = 0

为什么要修改这个参数呢?

  • rp_filter参数用于控制系统是否开启对数据包源地址的校验。

  • 有三个值,0、1、2,具体含义:
    0:不开启源地址校验。
    1:开启严格的反向路径校验。对每个进来的数据包,校验其反向路径是否是最佳路径。如果反向路径不是最佳路径,则直接丢弃该数据包。
    2:开启松散的反向路径校验。对每个进来的数据包,校验其源地址是否可达,即反向路径是否能通(通过任意网口),如果反向路径不同,则直接丢弃该数据包

server3
和server2一样的配置

客户端测试

LVS模式二:TUN隧道模式

相关文章:

  • 2022-12-23
  • 2021-04-11
  • 2021-10-09
  • 2022-12-23
  • 2021-12-01
猜你喜欢
  • 2022-12-23
  • 2021-08-23
  • 2021-07-13
  • 2021-09-08
  • 2021-06-06
  • 2022-12-23
  • 2021-11-18
相关资源
相似解决方案