Linux网络虚拟化之gre-连接两个子网

        实验如何通过Linux的gre虚拟网络设备实现子网连接。

1.需求

        假设有如下图1所示之需求:


                                                                                       图1

上图中局域网1位于上海,局域网2位于南京,两个网络都通过路由器接入公用网络。模拟这个网络环境,并通过Linux的gre虚拟网络设备,在公有网络之上,虚拟出一个二层数据链路,即图1中的绿色部分。

2.模拟组网图


                                                                                     图2

上图中,用LinuxBridge模拟了图1中的交换机,用VirtualBox仅主机(Honst-Only)网络模拟了外部公用网络,用宿主机的网络栈及gretap设备实现两个子网连接,重点就是gretap设备。

3.网络地址规划

网络名称 vni 网关 DHCP地址 IP地址范围
test-br-01 1000 172.16.0.1 172.16.0.2 172.16.0.3/24~172.16.0.50/24

4.配置dnsmaq

root登录图2中的ubuntu-01主机,配置dnsmasq。

  • 创建文件/etc/test-dnsmasq.conf,添加如下内容:
# DNS监听端口
interface=test-veth-02
# 绑定
bind-interfaces
# 网段
dhcp-range=172.16.0.2,172.16.0.50,255.255.255.0,12h
# 管理IP地址数据文件
dhcp-leasefile=/var/lib/misc/test-dnsmasq.leases
# 网关
dhcp-option=option:router,172.16.0.1
  • 执行如下命令,启动dnsmasq:
ip link add test-veth-01 type veth peer name test-veth-02
ip netns add test-ns-01
ip link set test-veth-02 netns test-ns-01
ip netns exec test-ns-01 ip addr add 172.16.0.2/24 dev test-veth-02
ip netns exec test-ns-01 ip link set test-veth-02 up
ip netns exec test-ns-01 /usr/sbin/dnsmasq --conf-file=/etc/test-dnsmasq.conf

5.创建gre

  • root登录ubuntu-01,执行如下指令:
brctl addbr test-br-01
ip link add gretap01 type gretap mode gre local 192.168.187.4 remote 192.168.187.3 ttl 255
brctl addif test-br-01 test-veth-01
brctl addif test-br-01 gretap01
ip link set test-br-01 up
ip link set gretap01 up
ip link set test-veth-01 up

特别注意上述代码中的第二行,这里创建的是gretap设备。

  • root登录ubuntu-02,执行如下指令:
brctl addbr test-br-01
ip link add gretap01 type gretap mode gre local 192.168.187.3 remote 192.168.187.4 ttl 255
brctl addif test-br-01 gretap01
ip link set test-br-01 up
ip link set gretap01 up

上述两块代码中,涉及local的地址时,指的是本机enp0s8网卡上的地址,remote时指的是对端主机enp0s8网卡上的IP地址。同时请注意无论是在ubuntu-01与ubuntu-02中,正确的设置路由表,网段192.168.187.0/24内的地址,要通过enp0s8转发。如下图3红框:


                                                    图3

从以上的配置过程可以看出,gre封装后的数据最终从那张网卡转发出去,取决于路由表的内容。至此vpn网络搭建完成。凡是连接在test-br-01网络上的主机,无论是位于上海还是南京,它们在逻辑上通过vpn组成了一个大二层的网络。

猜你喜欢

转载自blog.csdn.net/dkfajsldfsdfsd/article/details/79530643