本节主要通过使用vlan TAG,模拟4个虚拟机来在一台宿主机上模拟简历两个虚拟的网络环境。本节内容比较少,涉及的内容可能比较陌生。
在这简单介绍下: 一台交换机有很多端口,在本文中使用vlan tag的目的是为了为了网络隔离,如图(在此只介绍我们本文能用到的部分):
具体参照:https://zh.wikipedia.org/wiki/IEEE_802.1Q
# cat init_vms
#! /bin/bash -x
ovs-vsctl add-br br-int
ip netns add vm_a_1
ip netns add vm_a_2
ip netns add vm_b_1
ip netns add vm_b_2
ovs-vsctl add-port br-int vm_a_1 -- set interface vm_a_1 type=internal
ovs-vsctl add-port br-int vm_a_2 -- set interface vm_a_2 type=internal
ovs-vsctl add-port br-int vm_b_1 -- set interface vm_b_1 type=internal
ovs-vsctl add-port br-int vm_b_2 -- set interface vm_b_2 type=internal
ip link set vm_a_1 netns vm_a_1
ip link set vm_a_2 netns vm_a_2
ip link set vm_b_1 netns vm_b_1
ip link set vm_b_2 netns vm_b_2
ip netns exec vm_a_1 ifconfig vm_a_1 up
ip netns exec vm_a_2 ifconfig vm_a_2 up
ip netns exec vm_b_1 ifconfig vm_b_1 up
ip netns exec vm_b_2 ifconfig vm_b_2 up
==================================================
# cat clear_vms
#! /bin/bash -x
ovs-vsctl del-br br-int
ip netns del vm_a_1
ip netns del vm_a_2
ip netns del vm_b_1
ip netns del vm_b_2
我们首先运行初始化脚本,创建br-int,创建4个port,创建4个namespace,把4个port分别加载到4个namespace中,然后设置4个网络设备up状态。
在接下来的代码块中 如果标题有vm_a_1 字样,则表示在该namespace中执行的命令代码块,否则表示在宿主机中执行的命令
root@www /home/abaobo/tmp 2015-08-15 12:27:00
# ip netns exec vm_a_1 ip a a 10.0.0.11/24 dev vm_a_1
root@www /home/abaobo/tmp 2015-08-15 12:27:06
# ip netns exec vm_a_2 ip a a 10.0.0.12/24 dev vm_a_2
root@www /home/abaobo/tmp 2015-08-15 12:27:17
# ip netns exec vm_b_1 ip a a 10.0.0.21/24 dev vm_b_1
root@www /home/abaobo/tmp 2015-08-15 12:27:28
# ip netns exec vm_b_2 ip a a 10.0.0.22/24 dev vm_b_2
root@www /home/abaobo/tmp 2015-08-15 12:27:37
#
root@www /home/abaobo/tmp 2015-08-15 12:29:29
# ip netns exec vm_a_1 ping -c 1 10.0.0.21
PING 10.0.0.21 (10.0.0.21) 56(84) bytes of data.
64 bytes from 10.0.0.21: icmp_seq=1 ttl=64 time=0.446 ms
--- 10.0.0.21 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.446/0.446/0.446/0.000 ms
root@www /home/abaobo/tmp 2015-08-15 12:29:43
# ip netns exec vm_a_1 ping -c 1 10.0.0.22
PING 10.0.0.22 (10.0.0.22) 56(84) bytes of data.
64 bytes from 10.0.0.22: icmp_seq=1 ttl=64 time=0.459 ms
--- 10.0.0.22 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.459/0.459/0.459/0.000 ms
root@www /home/abaobo/tmp 2015-08-15 12:29:46
#
root@www /home/abaobo/tmp 2015-08-15 12:33:36
# ovs-vsctl set port vm_a_1 tag=1 -- set port vm_a_2 tag=1 -- set port vm_b_1 tag=2 -- set port vm_b_2 tag=2
root@www /home/abaobo/tmp 2015-08-15 12:33:45
# ovs-vsctl show
69581e33-310b-4971-84f3-2c06c4f8a75c
Bridge br-int
Port "vm_b_2"
tag: 2
Interface "vm_b_2"
type: internal
Port "vm_a_1"
tag: 1
Interface "vm_a_1"
type: internal
Port "vm_b_1"
tag: 2
Interface "vm_b_1"
type: internal
Port "vm_a_2"
tag: 1
Interface "vm_a_2"
type: internal
Port br-int
Interface br-int
type: internal
ovs_version: "2.4.0"
然后我们进行测试
root@www /home/abaobo/tmp 2015-08-15 12:34:18
# ip netns exec vm_a_1 ping -c 1 10.0.0.12
PING 10.0.0.12 (10.0.0.12) 56(84) bytes of data.
64 bytes from 10.0.0.12: icmp_seq=1 ttl=64 time=0.395 ms
--- 10.0.0.12 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.395/0.395/0.395/0.000 ms
root@www /home/abaobo/tmp 2015-08-15 12:34:23
# ip netns exec vm_a_1 ping -c 1 10.0.0.21
PING 10.0.0.21 (10.0.0.21) 56(84) bytes of data.
--- 10.0.0.21 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
1 root@www /home/abaobo/tmp 2015-08-15 12:34:40
# ip netns exec vm_a_1 ping -c 1 10.0.0.22
PING 10.0.0.22 (10.0.0.22) 56(84) bytes of data.
--- 10.0.0.22 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
1 root@www /home/abaobo/tmp 2015-08-15 12:34:54
#
通过TAG分别制造两个虚拟2层网络,其3层的网络地址也可以重复,也就是说左边的两个port都可以使用10.0.0.11这个地址,由于两个port处于不同的2层网络中,二者不可见,就像一个宿舍用了两个路由器,不同的电脑插在不同的路由器上。
在本节我们讲述了如何在一个宿主机中创建互相隔离的2层网络,下一节将会讲述如何跨节点创建互相隔离的2层网络,使用的知识有GRE通道相关和openflow规则相关。