Kvm虛擬機NAT、橋接網絡模式配置

網橋概念

網橋工作在OSI模型中的第二層,即鏈路層。完成數據幀(frame)的轉發,主要目的是在連接的網絡間提供透明的通信。網橋的轉發是依據數據幀中的源地址和目的地址來判斷一個幀是否應轉發和轉發到哪個端口。幀中的地址稱爲“MAC”地址或“硬件”地址,一般就是網卡所帶的地址。

網橋的作用是把兩個或多個網絡互連起來,提供透明的通信。網絡上的設備看不到網橋的存在,設備之間的通信就如同在一個網上一樣方便。由於網橋是在數據幀上進行轉發的,因此只能連接相同或相似的網絡(相同或相似結構的數據幀),如以太網之間、以太網與令牌環(token ring)之間的互連,對於不同類型的網絡(數據幀結構不同),如以太網與X.25之間,網橋就無能爲力了。

網橋擴大了網絡的規模,提高了網絡的性能,給網絡應用帶來了方便,在以前的網絡中,網橋的應用較爲廣泛。但網橋互連也帶來了不少問題:一個是廣播風暴,網橋不阻擋網絡中廣播消息,當網絡的規模較大時(幾個網橋,多個以太網段),有可能引起廣播風暴(broadcasting storm),導致整個網絡全被廣播信息充滿,直至完全癱瘓。第二個問題是,當與外部網絡互連時,網橋會把內部和外部網絡合二爲一,成爲一個網,雙方都自動向對方完全開放自己的網絡資源。這種互連方式在與外部網絡互連時顯然是難以接受的。問題的主要根源是網橋只是最大限度地把網絡溝通,而不管傳送的信息是什麼。
In the case of isolated, nat, and routed networks, DHCP and DNS are provided on the virtual network by libvirt, and the IP range can be determined by examining the virtual network config with ‘virsh net-dumpxml [networkname]’. There is one virtual network called ‘default’ setup out of the box which does NAT’ing to the default route and has an IP range of 192.168.122.0/255.255.255.0. Each guest will have an associated tun device created with a name of vnetN, which can also be overridden with the element (see overriding the target element).
有關概念的詳細介紹,請參看http://blog.csdn.net/gsl371/article/details/78662258

網橋配置

圖形方法

通過virt-manager的圖形界面方式配置nat或橋接網絡還是比較直觀和簡單的,方法是:
運行virt-manager,選擇edit-connection detail
選擇virtual network 標籤
選擇“+”添加,根據提示選擇nat或是路由方式,以實現和外部互通;或者孤立網絡模式,孤立網絡模式可以通過添加host的物理網卡,實現直接和外部互通。

命令方式

下面重點介紹如何通過virsh命令方式實現這兩種網絡模式配置。具體是nat還是橋接或是路由和隔離模式,取決於你選擇的xml模版,下面以Nat爲例;其他雷同。

複製配置文件

首先複製一份已存在的網絡的xml文件,做適當修改,比如

[root@vmhost networks]# cp default.xml testnet.xml

[root@vmhost networks]# vi  testnet.xml

修改後如下:

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh net-edit testnet
or other application using the libvirt API.
-->

<network>
  <name>testnet</name>
  <uuid>a7884784-5f15-4832-8c49-79b194c592cf</uuid>
  <forward dev='em2' mode='nat'>
    <interface dev='em2'/>
  </forward>
  <bridge name='virbr4' stp='on' delay='0'/>
  <mac address='72:54:00:7e:c8:55'/>
  <domain name='testnet'/>
  <ip address='192.169.100.1' netmask='255.255.255.0'>
  </ip>
</network>

定義網絡

[root@vmhost networks]# vi testnet.xml
[root@vmhost networks]# virsh net-define testnet.xml
Network testnet defined from testnet.xml

創建網絡

[root@vmhost networks]# virsh net-create testnet.xml
Network testnet created from testnet.xml

查看網絡,沒有自動啓動

[root@vmhost networks]# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 isbridge             active     yes           yes
 osbridge             active     yes           yes
 testnet              active     no            yes

添加自動啓動


[root@vmhost networks]# virsh net-autostart testnet
Network testnet marked as autostarted

[root@vmhost networks]# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 isbridge             active     yes           yes
 osbridge             active     yes           yes
 testnet              active     yes           yes

查看網橋

發現網橋virbr4,已自動生成

[root@vmhost network-scripts]# brctl show
bridge name     bridge id               STP enabled     interfaces
testbr0         8000.000000000000       yes
virbr1          8000.5254007ec855       yes             virbr1-nic
                                                        vnet0
                                                        vnet1
virbr2          8000.525400ed6f8b       yes             virbr2-nic
virbr4          8000.7254007ec855       yes             virbr4-nic

配置虛擬機網絡接口

查看虛擬機現有的接口
[root@vmhost network-scripts]# virsh domiflist BlockNode
Interface  Type       Source     Model       MAC
-------------------------------------------------------
macvtap2   direct     em2        rtl8139     52:54:00:85:06:17
刪除這個接口
[root@vmhost network-scripts]# virsh detach-interface BlockNode direct --mac 52:54:00:85:06:17 --config
Interface detached successfully

重啓這個虛擬機,接口已被刪除,

[root@vmhost network-scripts]# virsh domiflist BlockNode
Interface  Type       Source     Model       MAC
-------------------------------------------------------
重新添加虛擬機的接口

[root@vmhost ~]# virsh attach-interface BlockNode network testnet --target ens3
Interface attached successfully

–target指定了vm的tun/tap接口的名字,並被添加到了網絡的虛擬網橋中。–mac選項可以指定vm中已存在interface的mac,直接可用。

查看虛擬機的接口卡


[root@vmhost networks]# virsh domiflist BlockNode
Interface  Type       Source     Model       MAC
-------------------------------------------------------
ens3       network    testnet    rtl8139     52:54:00:44:86:8c

已經有了你重新命名,新分配的mac的接口卡,但是還沒有ip地址,進入虛擬機可以查看到

[root@localhost ~]# nmcli
virbr0: connected to virbr0
        bridge, 52:54:00:EA:83:29, sw, mtu 1500
        inet4 192.168.122.1/24

ens3: connecting (getting IP configuration) to ens3
        "Realtek RTL-8100/8101L/8139 PCI Fast Ethernet Adapter (QEMU Virtual Machine)"
        ethernet (8139cp), 52:54:00:44:86:8C, hw, mtu 1500

給虛擬機指定靜態IP的命令行方法目前還沒有看到,一般是通過dhcp的方式分配ip;只有啓動虛擬機後進去設置了


發佈了36 篇原創文章 · 獲贊 23 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章