[原] 利用 OVS 创建 VxLAN 虚拟网络实验

OVS 配置 VxLAN

HOST A
------------------------------------------
|       zh-veth0(10.1.1.1) VM A          |
|       ---|----                         |
|          |                             |
|          |  |------------|             |
|  zh-veth1|--|  zh-br-int |             |
|             |------------|             |
-----------------| eth0 |-----------------
                    |(172.17.6.223)
                    |                                                 HOST B
                    |                               ------------------------------------------
                    |                               |       zh-veth0(10.1.1.2)   VM B        |
                    |                               |       ---|----                         |
                    |                               |          |                             |
                    |                               |          |  |------------|             |
                    |                               |  zh-veth1|--|  zh-br-int |             |
                    |                               |             |------------|             |
                    |                               -----------------| eth0 |-----------------
                    |                                                   |(172.17.6.249)
                    |---------------------------------------------------|

背景知识

网络地址空间: Linux network namespace(netns)

用于隔离同一台宿主机的不一样网络配置,是Linux namespace的一个子系统.
root@ubuntu:~# ip netns add zh 将建立一个网络地址空间命名为zh.
网络地址空间在docker或者LXC 还有一些虚拟化软件里面被普遍使用.

OVS: Open Virtual Switch

用于在Linux上虚拟出交换机,用软件形式模拟交换机的工做,同时也支持openflow等控制协议.

VxLAN

有种overlay技术,本质为利用UDP封装二层协议,创建三层的隧道,此隧道转发二层协议。
利用VxLAN能够突破VLAN的4096个限制,实现同一个网断下不一样租户的隔离.

实验

经过配置VxLAN, 使得vm A 与 vm B 可以互相通讯

步骤
1. 创建虚拟交换机
2. 将eth0 挂载到虚拟交换机
3. 利用网络地址空间,模拟出VM网卡对
4. 为虚拟机配置IP
5. 创建HOST A与HOST B的VxLAN 隧道
步骤详细
1. 创建虚拟交换机
root@ubuntu:~# ovs-vsctl br-add zh-br-int
此处建立了一个名为zh-br-int的交换机,如上图所示

root@ubuntu:~# ovs-vsctl show
0979572b-2478-4f6c-9d48-82f9ec58da7c
        Port zh-br-int
            Interface zh-br-int
                type: internal
        Port "eth0"
            Interface "eth0"
    ovs_version: "1.4.6"
2. 将eth0挂载到虚拟交换机
root@ubuntu:~# ovs-vsctl add-port zh-br-int eth0 \
&& ifconfig eth0 0.0.0.0 && ip a add 172.17.6.223/16 brd + dev zh-br-int \
&& ip r add default via 172.17.0.1
 
将eth0挂载到交换机,觉得着交换机直接与外部网络链接起来,这时候须要将原来的eth0的IP配置到虚拟交换机上,也就是 zh-br-int 这个接口,默认状况下,建立的交换机都会有一个与其相同命名的接口,用来对此交换机作配置,否则交换机是没有IP这个概念的.
除了配置IP外,还须要加上默认网关。
这里的命令须要一次性完成,否则挂载eth0到交换机会且配置eth0的过程会致使网络端口,除非你有两块网卡能够避免这种问题.

配置完后能够看到在网卡配置下面是这样的
root@ubuntu:~# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:5d:45:a7  
          inet6 addr: fe80::20c:29ff:fe5d:45a7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1043789006 errors:4 dropped:2648455 overruns:0 frame:0
          TX packets:4136534 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:79136937123 (79.1 GB)  TX bytes:1685730355 (1.6 GB)
          Interrupt:19 Base address:0x2000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:2029312 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2029312 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1075915373 (1.0 GB)  TX bytes:1075915373 (1.0 GB)

zh-br-int Link encap:Ethernet  HWaddr 00:0c:29:5d:45:a7  
          inet addr:172.17.6.223  Bcast:172.17.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fe5d:45a7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:18748892 errors:0 dropped:12476 overruns:0 frame:0
          TX packets:88498 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:3315299018 (3.3 GB)  TX bytes:18437372 (18.4 MB)
3. 利用网络地址空间,模拟出VM网卡对
root@ubuntu:~# ip netns add zh
root@ubuntu:~# ip netns
zh

建立了网络地址空间,这里能够看作,zh 就是虚拟机的网络,进入 zh 配置就是进行虚拟机的配置.

root@ubuntu:~# ip link add zh-veth0 type veth peer name zh-veth1
root@ubuntu:~# ip link set veth0 netns zh

对应了图的两个接口,一个是 zh-veth0 是虚拟机的网络接口,一个 zh-veth1 的交换机的接口
这里将 zh-veth0 放入到了网络地址空间zh, 意味着在宿主机上是看不见这个网口的,他在虚拟机中与 zh-veth1 是能够看作一条网线接着的两个口.

root@ubuntu:~# ovs-vsctl add-port br-int zh-veth1
root@ubuntu:~# ifconfig zh-veth1
zh-veth1  Link encap:Ethernet  HWaddr c6:c6:9b:7f:bb:73  
          inet6 addr: fe80::c4c6:9bff:fe7f:bb73/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:79 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9775120 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:6669 (6.6 KB)  TX bytes:1585140114 (1.5 GB)

这样就在本地虚拟交换机与VM之间创建了一条链接

root@ubuntu:~# ip netns exec zh ip link set zh-veth0 up

将VM中的zh-veth0置为UP

root@ubuntu:~# ip netns exec zh ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
13: zh-veth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 5a:bb:cb:54:6c:2f brd ff:ff:ff:ff:ff:ff
    inet 10.1.1.1/24 scope global zh-veth0
       valid_lft forever preferred_lft forever
    inet6 fe80::58bb:cbff:fe54:6c2f/64 scope link 
       valid_lft forever preferred_lft forever
4. 为虚拟机配置IP
root@ubuntu:~# ip netns exec zh ip addr add 10.1.1.1/24 dev zh-veth0
root@ubuntu:~# ip link set zh-veth1 up
命令 ip netns exec zh 这一串表明了进入zh 进行网络操做,能够看作是配置虚拟机 zh 的网路
这里给 VM A 配置 IP 地址为 10.1.1.1/24 , 这个地址不一样于本地网路的 172.17.0.0/16 地址段,是一个彻底私有的地址段,这也就是虚拟网络的虚拟局域网概念
5. 创建HOST A与HOST B的VxLAN 隧道
root@ubuntu:~# ovs-vsctl add-port zh-br-int zh-vxlan0 -- set interface zh-vxlan0 type=vxlan options:remote_ip=172.17.6.249

HOST B 上面的配置与 HOST A相同,只不过VM 的IP须要配置为对端的,而且VxLAN隧道的remote_ip也不同.

结果

咱们在 VM B 上面启动一个 HTTP Server

root@ubuntu:~# ip netns exec zh python -m SimpleHTTPServer

在 VM A 上利用 curl 访问

root@ubuntu:~# ip netns exec zh curl 10.1.1.2:8000

在 VM B 上看到日志
10.1.1.1 - - [24/Sep/2015 18:06:10] "GET / HTTP/1.1" 200 -
10.1.1.1 - - [24/Sep/2015 18:08:29] "GET / HTTP/1.1" 200 -
10.1.1.1 - - [24/Sep/2015 18:08:34] "GET / HTTP/1.1" 200 -
10.1.1.1 - - [25/Sep/2015 15:03:06] "GET / HTTP/1.1" 200 -
相关文章
相关标签/搜索