乐趣区

关于linux:Open-vSwitch-入门实践2使用OVS构建隔离网络

前言

在后面咱们曾经应用 Linux Bridge 实现了多台网络设备的通信,然而它对于网络隔离的反对不是很好,长期以来,在 Linux 平台上短少一个性能齐备的虚构交换机,直到 OVS 的呈现。

试验

接下来咱们来尝试实现两个试验,单机无隔离网络、单机隔离网络。

试验一:单机无隔离网络

应用 ovs 构建无隔离网络非常简单,只须要增加一个网桥,而后在这个网桥上再减少几个外部端口,最初把端口挪动到 netns 中即可。

# 增加网桥
ovs-vsctl add-br br-int
# 增加三个外部端口
ovs-vsctl add-port br-int vnet0 -- set Interface vnet0 type=internal
ovs-vsctl add-port br-int vnet1 -- set Interface vnet1 type=internal
ovs-vsctl add-port br-int vnet2 -- set Interface vnet2 type=internal
# 增加三个 netns
ip netns add ns0
ip netns add ns1
ip netns add ns2
# 将外部端口别离挪动到 netns 中
ip link set vnet0 netns ns0
ip link set vnet1 netns ns1
ip link set vnet2 netns ns2

# 启动端口并配置 IP
ip netns exec ns0 ip link set lo up
ip netns exec ns0 ip link set vnet0 up
ip netns exec ns0 ip addr add 10.0.0.1/24 dev vnet0

ip netns exec ns1 ip link set lo up
ip netns exec ns1 ip link set vnet1 up
ip netns exec ns1 ip addr add 10.0.0.2/24 dev vnet1

ip netns exec ns2 ip link set lo up
ip netns exec ns2 ip link set vnet2 up
ip netns exec ns2 ip addr add 10.0.0.3/24 dev vnet2

测试

测试 ns0ns1是否通信ip netns exec ns0 ping 10.0.0.2

PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=1.05 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=0.056 ms
64 bytes from 10.0.0.2: icmp_seq=4 ttl=64 time=0.053 ms
^C
--- 10.0.0.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.053/0.304/1.051/0.431 ms

测试 ns 0 和ns2 是否通信ip netns exec ns0 ping 10.0.0.3

PING 10.0.0.3 (10.0.0.3) 56(84) bytes of data.
64 bytes from 10.0.0.3: icmp_seq=1 ttl=64 time=1.17 ms
64 bytes from 10.0.0.3: icmp_seq=2 ttl=64 time=0.067 ms
64 bytes from 10.0.0.3: icmp_seq=3 ttl=64 time=0.058 ms
64 bytes from 10.0.0.3: icmp_seq=4 ttl=64 time=0.064 ms
^C
--- 10.0.0.3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 0.058/0.341/1.177/0.482 ms

依据测试后果能够看到,三台设施都是能够相互拜访的,这样咱们就胜利搭建了一个无隔离的二层互通网络。

试验二:单机隔离网络

应用 ovs 构建隔离网络也很简略,只须要给相应的端口设置上 VLAN 标签,就能实现网络的隔离。

# 设置 vnet0 的 VLAN tag 为 100
ovs-vsctl set Port vnet0 tag=100
# 设置 vnet1 和 vnet2 的 VLAN tag 为 200
ovs-vsctl set Port vnet1 tag=200
ovs-vsctl set Port vnet2 tag=200

应用 ovs-vsctl show 命令查看 VLAN tag 是否配置胜利

90139c71-8d11-49b2-b44c-f34174259dc8
    Bridge br-int
        Port "vnet0"
            tag: 100
            Interface "vnet0"
                type: internal
        Port br-int
            Interface br-int
                type: internal
        Port "vnet2"
            tag: 200
            Interface "vnet2"
                type: internal
        Port "vnet1"
            tag: 200
            Interface "vnet1"
                type: internal
    ovs_version: "2.9.0"

测试

测试 ns0ns1的是否通信 ip netns exec ns0 ping 10.0.0.2

PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
^C
--- 10.0.0.2 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1000ms

测试 ns0ns2的是否通信 ip netns exec ns0 ping 10.0.0.3

PING 10.0.0.3 (10.0.0.3) 56(84) bytes of data.
^C
--- 10.0.0.3 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 999ms

测试 ns1ns2的是否通信 ip netns exec ns1 ping 10.0.0.3

PING 10.0.0.3 (10.0.0.3) 56(84) bytes of data.
64 bytes from 10.0.0.3: icmp_seq=1 ttl=64 time=0.930 ms
64 bytes from 10.0.0.3: icmp_seq=2 ttl=64 time=0.057 ms
64 bytes from 10.0.0.3: icmp_seq=3 ttl=64 time=0.056 ms
64 bytes from 10.0.0.3: icmp_seq=4 ttl=64 time=0.057 ms
^C
--- 10.0.0.3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.056/0.275/0.930/0.378 ms

测试 ns2ns1的是否通信 ip netns exec ns2 ping 10.0.0.2

PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.088 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.057 ms
64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=0.050 ms
64 bytes from 10.0.0.2: icmp_seq=4 ttl=64 time=0.060 ms
^C
--- 10.0.0.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.050/0.063/0.088/0.017 ms

依据测试后果能够看出,ns0是无法访问到 ns1ns2的,ns1ns2 能够相互拜访。这是因为端口 vnet0 的数据报文收回后被 OVS 批改了包头,减少了 VLAN 100标签,与 vnet1vnet2 的 VLAN 200标签不匹配,OVS 交换机便不再将 vnet0 的数据报文发送给其余两个端口,由此便实现了网络隔离。

清理试验环境

ovs-vsctl del-br br-int
ip netns del ns0
ip netns del ns1
ip netns del ns2

本文首发我的微信公众号:我在对面的角落
欢送关注,接管第一工夫更新告诉。

退出移动版