一、有時候我們希望虛擬環境能夠連接到實體網路,但又不希望以NAT方式,而是想實際使用實體IP來做為連線,因此可以採虛擬橋接器的方式,如使用Docker時不想依賴預設的內網配置,而希望Docker的每個Container都能有一組實體的ip時,因此以下紀錄了如何在Ubuntu上新增一組虛擬橋接器br-etho設定完後即可將Docker的橋接設定指向br-eth0,關於Docker的橋接器設定可以參考:http://philipzheng.gitbooks.io/docker_practice/content/advanced_network/bridge.html。
二、首先我們可以用ifconfig來看網路設定
hduser@master:~$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:0c:29:1d:7c:d8
inet addr:192.168.121.129 Bcast:192.168.121.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:243 errors:0 dropped:0 overruns:0 frame:0
TX packets:98 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:35928 (35.9 KB) TX bytes:14139 (14.1 KB)
Interrupt:19 Base address:0x2000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:28 errors:0 dropped:0 overruns:0 frame:0
TX packets:28 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2169 (2.1 KB) TX bytes:2169 (2.1 KB)
三、下載bridge-utils套件
apt-get install bridge-utils
四、使用brctl增加一個bridge
brctl addbr br-etho
五、接下來將實體網路卡設定成promiscuous模式
ifconfig etho 0.0.0.0 promic
六、把我們新增的bridge接到eth0上
brctl addif br-eth0 eth0
七、如果是固定ip就將橋接器對應到該ip
ifconfig br-eth0 192.168.121.101
八、如果是採用DHCP則使用dhclient設定橋接器br-eth0
dhclient br-eth0
九、再查看一次ifconfig
hduser@master:~$ ifconfig
br-etho Link encap:Ethernet HWaddr 00:0c:29:1d:7c:d8
inet addr:192.168.121.129 Bcast:192.168.121.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:77 errors:0 dropped:0 overruns:0 frame:0
TX packets:67 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:6982 (6.9 KB) TX bytes:11819 (11.8 KB)
eth0 Link encap:Ethernet HWaddr 00:0c:29:1d:7c:d8
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1252 errors:0 dropped:0 overruns:0 frame:0
TX packets:407 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:111169 (111.1 KB) TX bytes:58284 (58.2 KB)
Interrupt:19 Base address:0x2000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:64 errors:0 dropped:0 overruns:0 frame:0
TX packets:64 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4647 (4.6 KB) TX bytes:4647 (4.6 KB)
十、以上的設定在重新開機後便會消失,如果想設定永久的Bridge,可以參考以下的配置,虛擬機為VMware player,作業系統ubuntu 14.04。
(一)固定IP:vi /etc/network/interfaces
#The primary network interface
auto lo
iface lo inet loopback
#The virtual bridge interface
auto br-eth0
iface br-eth0 inet static
bridge_ports eth0
bridge_stp off
bridge_maxwait 0
bridge_fd 0
address 192.168.121.2
netmask 255.255.255.0
gateway xxx.xxx.xxx.1
dns-nameservers 8.8.8.8 8.8.8.4
(二)動態IP:vi /etc/network/interfaces,將inet static改為inet dhcp,將以下Address設定部分拿掉即可。
auto lo
iface lo inet loopback
#The virtual bridge interface
auto br-eth0
iface br-eth0 inet dhcp
bridge_ports eth0
bridge_stp off
bridge_maxwait 0
bridge_fd 0
留言
張貼留言