轉: Linux下單網卡多vlan多虛擬機器

來源:互聯網
上載者:User

#啟動xen服務:
xend start
#列出虛擬機器
xm list
#start
xm create -c 虛擬機器名
#進入虛擬機器
xm console 虛擬機器名
#退出虛擬機器
ctrl]
#關閉
xm shutdown

linux bridge實現了ieee 802.1d協議,這個實現,不能支援VLAN的功能。也就是說,這個實現,只能承載一個廣播域,而不能承載多個廣播域。當然,可以建立多個bridge device,每個bridge都對應不同的vlan,在bridge內部,包通過fdb表來轉寄,但是這個fdb表裡面並沒有vlan的資訊。如果要在多個bridge device之間通訊,比必須在bridge device上建立vlan interface,然後配置路由,這樣可以實現不同bridge之間的轉寄。
linux vlan實現了ieee 802.1q協議。802.1q本來應該是一個二層協議,但是linux的實現需要建立vlan interface,而且可以在vlan interface上配置ip地址。所以,這個interface可以放到路由表裡面。一般來說,在這個interface上收到的包,會帶這個interface配置的vlan id,而從這個interface發出去的包,會打上這個interface的vlan id。

一、建立兩個bridge和網卡子介面

1、 兩個橋接器命名為Xenbr2,xenbr3。

2、 兩個網卡介面eth1.10 跑vlan10,橋接到xenbr3;eth1.40 跑vlan 40橋接到xenbr2。

3、 可以使用命令brctl addbr bridgename 增加bridge ,brctl addif bridgename devicename 將網卡加入橋接器;也可以建立設定檔。推薦使用設定檔。檔案位置和內容如下:

[root@localhost network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-xenbr2

DEVICE=xenbr2

BOOTPROTO=static

ONBOOT=yes

TYPE=Bridge

[root@localhost network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-xenbr3

DEVICE=xenbr3

BOOTPROTO=static

ONBOOT=yes

TYPE=Bridge

[root@localhost network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-eth1.40

DEVICE=eth1.40

BOOTPROTO=none

ONBOOT=yes

TYPE=Ethernet

VLAN=yes

BRIDGE=xenbr2

[root@localhost network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-eth1.10

DEVICE=eth1.10

BOOTPROTO=none

ONBOOT=yes

TYPE=Ethernet

VLAN=yes

BRIDGE=xenbr3

PS:將網卡子介面加入橋接器後就變成了橋接器的一個連接埠,橋接器是一個二層裝置,所以網卡子介面配置了IP也無效。可以在橋接器介面上配置IP供管理使用。

二、啟動橋接器

Ifup xenbr2

Ifup xenbr2

Ifup eth1.10

Ifup eth1.40

PS:橋接器和網卡子介面啟動有先後順序之先,要先啟動橋接器,再啟動網卡子介面。因為網卡了加入到橋接器中去呀~

三、為虛擬機器新增網卡

[root@xen-centos ~]# virsh

virsh # attach-interface centos bridge xenbr2

這個命令就能為centos這個虛擬機器添加了一塊網卡橋接到xenbr2上。

刪除網卡的命令,不加MAC預設刪除第一塊網卡。

virsh # detach-interface centos bridge --mac 00:16:3E:24:B5:3B

Ps:重啟不會丟失網卡,關機重新載入設定檔後會丟失網卡,下面提供修改設定檔的操作方法。

修改Xen虛擬機器的設定檔

設定檔在/etc/xen 下,設定檔中對應的項是vif

1、 虛擬機器只使用一塊網卡

[root@localhost xen]# cat /etc/xen/centos

name = "centos"

uuid = "eb2f58c6-cd95-8109-f29e-5df80df8c476"

maxmem = 256

memory = 256

vcpus = 1

bootloader = "/usr/bin/pygrub"

on_poweroff = "destroy"

on_reboot = "restart"

on_crash = "restart"

disk = [ "tap:aio:/virtual/centos/centos5.5.img,xvda,w" ]

vif = [ "mac=00:16:36:2b:3f:a1,bridge=xenbr3,script=vif-bridge"]

2、 虛擬機器使用兩塊網卡

[root@localhost xen]# cat /etc/xen/centos

name = "centos"

uuid = "eb2f58c6-cd95-8109-f29e-5df80df8c476"

maxmem = 256

memory = 256

vcpus = 1

bootloader = "/usr/bin/pygrub"

on_poweroff = "destroy"

on_reboot = "restart"

on_crash = "restart"

disk = [ "tap:aio:/virtual/centos/centos5.5.img,xvda,w" ]

vif = [ "mac=00:16:36:2b:3f:a1,bridge=xenbr3,script=vif-bridge","mac=00:16:36:2b:3f:a2,bridge=xenbr2,script=vif-bridge"]

四、最後一步

將交換器的連接埠模式改成trnuk

小結:以此類推的話,添加多個VLAN就建立網卡子介面跑不用的VLAN。要是有多塊網卡的,也可以分別橋接到不同的橋接器。組網很靈活。也可以將物理網卡使用bonding技術後再橋接,這個技術不錯,哈哈~

橋接器有如下特點:
(1) 在混雜模式下工作。
(2) 有一個將全域唯一地址映射到橋接器連接埠的地址表。
(3) 根據所接收幀的目的地址作出轉寄決定。
(4) 根據所接收幀的源地址建立和更新地址表。
(5) 當遇到未知的目的地址時,向每個連接埠(除接收此幀的連接埠外)轉寄該幀

 

使得我們機房的網路正式支援多vlan,目前是8個vlan.

   IP地址分布在9個網段:192.168.0.*-192.168.8.*

   第一機房: vlan8
   第二機房:vlan2
   第三機房:vlan3,vlan4,vlan5,vlan6,vlan7.

   主要是使用了vconfig工具。

   ip addres add 0.0.0.0 dev eth2

  
使 eth2 支援 3個機房的7個vlan: vlan2-vlan8
   vconfig add eth2   2
   vconfig
add eth2   3
   vconfig add eth3   4
   vconfig add eth2   5
  
vconfig add eth2   6
   vconfig add eth2   7
   vconfig add eth2   8

   eth0 走路由器,通外網
  
   eth1 連預設的vlan1,連內網的其它伺服器以及網管工作站。

   物理網卡eth2被掰成了7個虛擬網卡: eth2.2,eth2.3,eth2.4,eth2.5,eth2.6,eth2.7,eth2.8.

分別給這7個虛擬網卡設定ip地址。

ifconfig eth0.100 100.100.100.100 netmask 255.255.255.0 up

ifconfig eth0.100 100.101.100.101 netmask 255.255.255.0 up

最後,再給7個vlan設定靜態路由:

ip
route add 192.168.2.0/24 dev   eth2.2
ip route add 192.168.3.0/24 dev  
eth2.3
ip route add 192.168.4.0/24 dev   eth2.4
ip route add
192.168.5.0/24 dev   eth2.5
ip route add 192.168.6.0/24 dev   eth2.6
ip
route add 192.168.7.0/24 dev   eth2.7
ip route add 192.168.8.0/24 dev  
eth2.8

Linux 關於VLAN與Cisco交換器中繼串連:http://www.net527.com/caozuoxitong/Linux/1119.htmlhttp://www.net527.com/caozuoxitong/Linux/1119.html
linux 單網卡與8大vlan通訊 http://hi.baidu.com/zouhaoo/blog/item/3a85402acb061021d42af18d.html
Linux 設定 多ip,多vlan  http://kb.cnblogs.com/a/2205770/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.