筆者現(xiàn)在在做云計(jì)算的網(wǎng)絡(luò)設(shè)計(jì),涉及到上百臺(tái)服務(wù)器與交換機(jī),在實(shí)測(cè)前必須進(jìn)行原型測(cè)試,但是我只有一個(gè)工作用開發(fā)機(jī),本文介紹一種方法,使用這一臺(tái)機(jī)器模擬數(shù)十臺(tái)服務(wù)器以及路由器的網(wǎng)絡(luò)環(huán)境。
Linux的網(wǎng)絡(luò)虛擬化是LXC項(xiàng)目中的一個(gè)子項(xiàng)目,LXC包括文件系統(tǒng)虛擬化,進(jìn)程空間虛擬化,用戶虛擬化,網(wǎng)絡(luò)虛擬化,等等 [1],這里使用LXC的網(wǎng)絡(luò)虛擬化來模擬多個(gè)網(wǎng)絡(luò)環(huán)境。
使用命令
$ ip netns add net0 |
可以創(chuàng)建一個(gè)完全隔離的新網(wǎng)絡(luò)環(huán)境,這個(gè)環(huán)境包括一個(gè)獨(dú)立的網(wǎng)卡空間,路由表,ARP表,ip地址表,iptables,ebtables,等等??傊?,與網(wǎng)絡(luò)有關(guān)的組件都是獨(dú)立的。
ip命令需要root權(quán)限的,但是由于本文大量使用ip命令,于是筆者給ip命令添加了capability,使普通用戶也能使用ip命令
使用命令
$ ip netns listnet0 |
可以看到我們剛才創(chuàng)建的網(wǎng)絡(luò)環(huán)境
使用命令
$ ip netns exec net0 `command` |
我們可以在 net0 虛擬環(huán)境中運(yùn)行任何命令
$ ip netns exec net0 bash$ ip ad1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 |
這樣我們可以在新的網(wǎng)絡(luò)環(huán)境中打開一個(gè)shell,可以看到,新的網(wǎng)絡(luò)環(huán)境里面只有一個(gè)lo設(shè)備,并且這個(gè)lo設(shè)備與外面的lo設(shè)備是不同的,之間不能互相通訊。
新的網(wǎng)絡(luò)環(huán)境里面沒有任何網(wǎng)絡(luò)設(shè)備,并且也無(wú)法和外部通訊,就是一個(gè)孤島,通過下面介紹的這個(gè)方法可以把兩個(gè)網(wǎng)絡(luò)環(huán)境連起來,簡(jiǎn)單的說,就是在兩個(gè)網(wǎng)絡(luò)環(huán)境之間拉一根網(wǎng)線
$ ip netns add net1 |
先創(chuàng)建另一個(gè)網(wǎng)絡(luò)環(huán)境net1,我們的目標(biāo)是把net0與net1連起來
$ ip link add type veth$ ip ad1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:0081: veth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 12:39:09:81:3a:dd brd ff:ff:ff:ff:ff:ff82: veth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 32:4f:fd:cc:79:1b brd ff:ff:ff:ff:ff:ff |
這里創(chuàng)建連一對(duì)veth虛擬網(wǎng)卡,類似pipe,發(fā)給veth0的數(shù)據(jù)包veth1那邊會(huì)收到,發(fā)給veth1的數(shù)據(jù)包veth0會(huì)收到。就相當(dāng)于給機(jī)器安裝了兩個(gè)網(wǎng)卡,并且之間用網(wǎng)線連接起來了
$ ip link set veth0 netns net0$ ip link set veth1 netns net1 |
這兩條命令的意思就是把veth0移動(dòng)到net0環(huán)境里面,把veth1移動(dòng)到net1環(huán)境里面,我們看看結(jié)果
$ ip ad1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00$ ip netns exec net0 ip ad1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:0081: veth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 12:39:09:81:3a:dd brd ff:ff:ff:ff:ff:ff$ ip netns exec net1 ip ad1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:0082: veth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 32:4f:fd:cc:79:1b brd ff:ff:ff:ff:ff:ff |
veth0 veth1已經(jīng)在我們的環(huán)境里面消失了,并且分別出現(xiàn)在net0與net1里面。下面我們簡(jiǎn)單測(cè)試一下net0與net1的聯(lián)通性
$ ip netns exec net0 ip link set veth0 up$ ip netns exec net0 ip address add 10.0.1.1/24 dev veth0$ ip netns exec net1 ip link set veth1 up$ ip netns exec net1 ip address add 10.0.1.2/24 dev veth1 |
分別配置好兩個(gè)設(shè)備,然后用ping測(cè)試一下聯(lián)通性:
$ ip netns exec net0 ping -c 3 10.0.1.2PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data.64 bytes from 10.0.1.2: icmp_req=1 ttl=64 time=0.101 ms64 bytes from 10.0.1.2: icmp_req=2 ttl=64 time=0.057 ms64 bytes from 10.0.1.2: icmp_req=3 ttl=64 time=0.048 ms--- 10.0.1.2 ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 1999msrtt min/avg/max/mdev = 0.048/0.068/0.101/0.025 ms |
創(chuàng)建虛擬網(wǎng)絡(luò)環(huán)境并且連接網(wǎng)線
ip netns add net0ip netns add net1ip netns add bridgeip link add type vethip link set dev veth0 name net0-bridge netns net0ip link set dev veth1 name bridge-net0 netns bridgeip link add type vethip link set dev veth0 name net1-bridge netns net1ip link set dev veth1 name bridge-net1 netns bridge |
在bridge中創(chuàng)建并且設(shè)置br設(shè)備
ip netns exec bridge brctl addbr brip netns exec bridge ip link set dev br upip netns exec bridge ip link set dev bridge-net0 upip netns exec bridge ip link set dev bridge-net1 upip netns exec bridge brctl addif br bridge-net0ip netns exec bridge brctl addif br bridge-net1 |
然后配置兩個(gè)虛擬環(huán)境的網(wǎng)卡
ip netns exec net0 ip link set dev net0-bridge upip netns exec net0 ip address add 10.0.1.1/24 dev net0-bridge |
ip netns exec net1 ip link set dev net1-bridge upip netns exec net1 ip address add 10.0.1.2/24 dev net1-bridge |
測(cè)試
$ ip netns exec net0 ping -c 3 10.0.1.2PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data.64 bytes from 10.0.1.2: icmp_req=1 ttl=64 time=0.121 ms64 bytes from 10.0.1.2: icmp_req=2 ttl=64 time=0.072 ms64 bytes from 10.0.1.2: icmp_req=3 ttl=64 time=0.069 ms--- 10.0.1.2 ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 1999msrtt min/avg/max/mdev = 0.069/0.087/0.121/0.025 ms |
隨著虛擬網(wǎng)絡(luò)環(huán)境增加,環(huán)境中網(wǎng)卡數(shù)量也在不斷增加,經(jīng)常會(huì)忘記環(huán)境中哪些網(wǎng)卡連接到哪里,通過 lldp [2] 協(xié)議,我們可以清楚看到每個(gè)網(wǎng)卡連接到了哪些環(huán)境中的哪個(gè)網(wǎng)卡。
github 上有一個(gè) lldp 在 linux 下的開源實(shí)現(xiàn) [3],通過在每個(gè)環(huán)境中起一個(gè) lldp daemon,我們就可以實(shí)時(shí)查看每個(gè)網(wǎng)卡的連接情況
Bridge 上 lldp 的數(shù)據(jù)
$ lldpcli show neighborsLLDP neighbors:Interface: bridge-net0, via: LLDP, RID: 2, Time: 0 day, 00:06:53 Chassis: ChassisID: mac 82:be:2a:ec:70:69 SysName: localhost SysDescr: net0 Capability: Bridge, off Capability: Router, off Capability: Wlan, off Port: PortID: mac 82:be:2a:ec:70:69 PortDescr: net0-bridgeInterface: bridge-net1, via: LLDP, RID: 1, Time: 0 day, 00:06:53 Chassis: ChassisID: mac b2:34:28:b1:be:49 SysName: localhost SysDescr: net1 Capability: Bridge, off Capability: Router, off Capability: Wlan, off Port: PortID: mac b2:34:28:b1:be:49 PortDescr: net1-bridge
來自:http://blog.kghost.info/2013/03/01/linux-network-emulator/
聯(lián)系客服