1. 程式人生 > >玩轉OpenStack--10>動手實踐虛擬網路

玩轉OpenStack--10>動手實踐虛擬網路

玩轉OpenStack--10>動手實踐虛擬網路

本節將演示如何在實驗環境中實現下圖所示的虛擬網路

配置 Linux Bridge br0

編輯 /etc/network/interfaces,配置 br0。

下面用 vmdiff 展示了對 /etc/network/interfaces 的修改

有兩點需要注意: 1. 之前宿主機的 IP 是通過 dhcp 配置在 eth0 上的;建立 Linux Bridge 之後,IP 就必須放到 br0 上了 2. 在 br0 的配置資訊中請注意最後一行 “bridge_ports eth0”,其作用就是將 eth0 掛到 br0 上

重啟宿主機,檢視 IP 配置,可以看到 IP 已經放到 br0 上了

用 brctl show 檢視當前 Linux Bridge 的配置。 eth0 已經掛到 br0 上了

# brctl show bridge

name     bridge id               STP enabled     interfaces

br0             8000.000c298decbe       no                    eth0

virbr0          8000.000000000000       yes

除了 br0,大家應該注意到還有一個 virbr0 的 Bridge,而且 virbr0 上已經配置了 IP 地址 192.168.122.1。

virbr0 的作用我們會在後面介紹。

在宿主機中 CloudMan 已經提前建立好了虛機 VM1 和 VM2,現在都處於關機狀態

# virsh list --all Id    

Name                           State

---------------------------------------------------- -    

VM1                            shut off -     VM2                            shut off

配置 VM1

下面我們在 virt-manager 中檢視一下 VM1 的網絡卡配置(為了使大家能夠熟練使用命令列工具 virsh 和圖形工具 virt-manager,CloudMan 在演示的時候會同時用到它們,兩個工具都很重要)

可以看到虛擬網絡卡的 source device 我們選擇的是 br0

下面我們啟動 VM1,看會發生什麼

# virsh start VM1 Domain VM1 started

# brctl show

bridge name     bridge id               STP enabled     interfaces

br0             8000.000c298decbe       no                    eth0

                                                                                 vnet0

virbr0          8000.000000000000       yes

brctl show 告訴我們 br0 下面添加了一個 vnet0 裝置,通過 virsh 確認這就是VM1的虛擬網絡卡。

# virsh domiflist

VM1 Interface  Type       Source     Model       MAC

-------------------------------------------------------

vnet0      bridge     br0        rtl8139     52:54:00:75:dd:1a

VM1 的 IP 是 DHCP 獲得的(設定靜態 IP 當然也可以),通過 virt-manager 控制檯登入 VM1,檢視 IP。

# ifconfig eth0

     Link encap:Ethernet  HWaddr 52:54:00:75:dd:1a

         inet addr:192.168.111.106  Bcast:192.168.111.255  Mask:255.255.255.0

         inet6 addr: fe80::5054:ff:fe75:dd1a/64 Scope:Link

         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

         RX packets:400 errors:0 dropped:0 overruns:0 frame:0

         TX packets:101 errors:0 dropped:0 overruns:0 carrier:0

         collisions:0 txqueuelen:1000

         RX bytes:41950 (41.9 KB)  TX bytes:12583 (12.5 KB)

VM1 通過 DHCP 拿到的 IP 是 192.168.111.106,與宿主機(IP為192.168.111.107)是同一個網段。Ping 一下外網

[email protected]:~# ping www.baidu.com

PING www.a.shifen.com (180.97.33.108) 56(84) bytes of data.

64 bytes from 180.97.33.108: icmp_seq=1 ttl=53 time=34.9 ms

64 bytes from 180.97.33.108: icmp_seq=2 ttl=53 time=36.2 ms

64 bytes from 180.97.33.108: icmp_seq=3 ttl=53 time=38.8 ms

沒問題,可以訪問。

另外,在 VM1 中虛擬網絡卡是 eth0,並不是 vnet0。

vent0 是該虛擬網絡卡在宿主機中對應的裝置名稱,其型別是 TAP 裝置,這裡需要注意一下。

配置 VM2

跟 VM1 一樣,VM2 的虛擬網絡卡也掛在 br0上,啟動 VM1,檢視網絡卡資訊

# virsh start VM2 Domain VM2 started

# brctl show

bridge name     bridge id               STP enabled     interfaces

br0             8000.000c298decbe       no                    eth0

                                                                                 vnet0

                                                                                 vnet1

virbr0          8000.000000000000       yes

br0 下面多了 vnet1,通過 virsh 確認這就是 VM2 的虛擬網絡卡。

# virsh domiflist

VM2 Interface  Type       Source     Model       MAC

-------------------------------------------------------

vnet0      bridge     br0        rtl8139     52:54:00:cf:33:a1

VM2 通過 DHCP 拿到的 IP 是 192.168.111.108,登入 VM2,驗證網路的連通性

Ping VM1

[email protected]:~# ping VM1

PING VM1 (192.168.111.106) 56(84) bytes of data.

64 bytes from 192.168.111.106: icmp_seq=1 ttl=64 time=4.54 ms

64 bytes from 192.168.111.106: icmp_seq=2 ttl=64 time=1.63 ms

64 bytes from 192.168.111.106: icmp_seq=3 ttl=64 time=2.16 ms

Ping 宿主機

[email protected]:~# ping 192.168.111.107

PING 192.168.111.107 (192.168.111.107) 56(84) bytes of data.

64 bytes from 192.168.111.107: icmp_seq=1 ttl=64 time=1.02 ms

64 bytes from 192.168.111.107: icmp_seq=2 ttl=64 time=0.052 ms

64 bytes from 192.168.111.107: icmp_seq=3 ttl=64 time=0.064 ms

Ping 外網

[email protected]:~# ping www.baidu.com

PING www.a.shifen.com (180.97.33.107) 56(84) bytes of data.

64 bytes from 180.97.33.107: icmp_seq=1 ttl=53 time=53.9 ms

64 bytes from 180.97.33.107: icmp_seq=2 ttl=53 time=45.0 ms

64 bytes from 180.97.33.107: icmp_seq=3 ttl=53 time=44.2 ms

可見,通過 br0 這個 Linux Bridge,我們實現了 VM1、VM2、宿主機和外網這四者之間的資料通訊。