理解 virbr0 - 天天5分鐘玩轉 OpenStack(11)


virbr0 是 KVM 默認建立的一個 Bridge,其做用是爲鏈接其上的虛機網卡提供 NAT 訪問外網的功能。linux

virbr0 默認分配了一個IP 192.168.122.1,併爲鏈接其上的其餘虛擬網卡提供 DHCP 服務。ubuntu

下面咱們演示如何使用 virbr0。網絡

在 virt-manager 打開 VM1 的配置界面,網卡 Source device 選擇 「default」,將 VM1 的網卡掛在 virbr0 上。
dom

啓動 VM1,brctl show 能夠查看到 vnet0 已經掛在了 virbr0 上。ssh

# brctl show bridge name     bridge id               STP enabled     interfaces br0             8000.000c298decbe       no                    eth0 virbr0          8000.fe540075dd1a       yes                   vnet0spa

用 virsh 命令確認 vnet 就是 VM1 的虛擬網卡。orm

# virsh domiflist VM1 Interface  Type       Source     Model       MAC ------------------------------------------------------- vnet0      network    default    rtl8139     52:54:00:75:dd:1adns

virbr0 使用 dnsmasq 提供 DHCP 服務,能夠在宿主機中查看該進程信息進程

# ps -elf|grep dnsmasqit

5 S libvirt+  2422     1  0  80   0 -  7054 poll_s 11:26 ?        00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf

在 /var/lib/libvirt/dnsmasq/ 目錄下有一個 default.leases 文件,當 VM1 成功得到 DHCP 的 IP 後,能夠在該文件中查看到相應的信息

# cat /var/lib/libvirt/dnsmasq/default.leases 1441525677 52:54:00:75:dd:1a 192.168.122.6 ubuntu *

上面顯示 192.168.122.6 已經分配給 MAC 地址爲 52:54:00:75:dd:1a 的網卡,這正是 vnet0 的 MAC。以後就可使用該 IP 訪問 VM1 了。

# ssh 192.168.122.6 root@192.168.122.6's password: Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-30-generic x86_64) Last login: Sun Sep  6 01:30:23 2015 root@VM1:~# ifconfig eth0      Link encap:Ethernet  HWaddr 52:54:00:75:dd:1a          inet addr:192.168.122.6  Bcast:192.168.122.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:61 errors:0 dropped:0 overruns:0 frame:0          TX packets:66 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000          RX bytes:7453 (7.4 KB)  TX bytes:8649 (8.6 KB)

Ping一下外網。

root@VM1:~# 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=52 time=36.9 ms 64 bytes from 180.97.33.107: icmp_seq=2 ttl=52 time=119 ms 64 bytes from 180.97.33.107: icmp_seq=3 ttl=52 time=88.5 ms 64 bytes from 180.97.33.107: icmp_seq=4 ttl=52 time=38.0 ms 64 bytes from 180.97.33.107: icmp_seq=5 ttl=52 time=122 ms

沒有問題,能夠訪問外網,說明 NAT 起做用了。

須要說明的是,使用 NAT 的虛機 VM1 能夠訪問外網,但外網沒法直接訪問 VM1。 由於 VM1 發出的網絡包源地址並非 192.168.122.6,而是被 NAT 替換爲宿主機的 IP 地址了。

這個與使用 br0 不同,在 br0 的狀況下,VM1 經過本身的 IP 直接與外網通訊,不會通過 NAT 地址轉換。

下節咱們討論 vlan 在 linux bridge 中的實現

相關文章
相關標籤/搜索