[原] 利用 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 -
相關文章
相關標籤/搜索