2010年8月31日 星期二

網卡綁定 Network Bonding


當我們一張網卡的速度不夠的時候,就可以試著兩張網卡綁在一起來使用。

環境介紹:
系統 ubuntu 9.04 64bit
2張Gigabit網卡: 主機板內建 Realtek RTL8111/8168B、PCI介面 Intel 82541PI
Switch: Linsys 48port Gigabit SRW2048

此server上有大量的影音檔,多是超過1G以上的大檔案,在區網多人存取檔案時,1張Gigabit的網卡可能不夠用,於是就想到了試試將兩張網卡綁在一起使用。


以下內容,紅字部份皆為指令或是您需要留意或修改的部份 ,藍字部份則為設定檔內容。

1.安裝套件

sudo apt-get install ifenslave

2.編輯設定檔

sudo vim /etc/modprobe.d/bonding.conf

########################
alias bond0 bonding
options bonding mode=0 miimon=100
########################

mode=0
有0~6底下這幾種模式可以設定


mode=0 (balance-rr)
負載平衡模式(Round-robin policy), 需有 switch 設定 (port trunk,或是 Link Aggregation ) 支援才能發揮實質效果,具容錯功能, 其中一張網卡失效仍可持續運作。運作模式就是所有綁定的網卡皆會依序的被使用到。
Round-robin policy: Transmit packets in sequential order from the first available slave through the last. This mode provides load balancing and fault tolerance.
   
mode=1 (active-backup)
同一時間只有單一網卡運作,需設定主要(primary)與次要網卡,當主要網卡失效時自動啟用次要網卡,不需 switch 支援。
Active-backup policy: Only one slave in the bond is active. A different slave becomes active if, and only if, the active slave fails. The bond's MAC address is externally visible on only one port (network adapter) to avoid confusing the switch. This mode provides fault tolerance. The primary option affects the behavior of this mode.
   
mode=2 (balance-xor)
XOR policy: Transmit based on [(source MAC address XOR'd with destination MAC address) modulo slave count]. This selects the same slave for each destination MAC address. This mode provides load balancing and fault tolerance.


mode=3 (broadcast)
所有 Slave 網卡一齊收送網路封包具容錯功能, 其中一張 Slave 網卡失效仍可持續運作
Broadcast policy: transmits everything on all slave interfaces. This mode provides fault tolerance.

mode=4 (802.3ad)
802.3ad是比較正規的做法,需 switch 支援及設定。參考資料1參考資料2
若有兩張網路卡,則進來流量為2000M出去流量為2000M,具容錯功能。ALA (Adapter Link Aggregation)

IEEE 802.3ad Dynamic link aggregation. Creates aggregation groups that share the same speed and duplex settings. Utilizes all slaves in the active aggregator according to the 802.3ad specification.
   
需求:
Ethtool support in the base drivers for retrieving the speed and duplex of each slave.
A switch that supports IEEE 802.3ad Dynamic link aggregation. Most switches will require some type of configuration to enable 802.3ad mode.


mode=5 (balance-tlb)
傳出自動負載平衡,傳入由次要網卡負責,具容錯功能, 其中一張次要網卡失效仍可持續運作,不需 switch 支援及設定 
Adaptive transmit load balancing: channel bonding that does not require any special switch support. The outgoing traffic is distributed according to the current load (computed relative to the speed) on each slave. Incoming traffic is received by the current slave. If the receiving slave fails, another slave takes over the MAC address of the failed receiving slave.
需求: Ethtool support in the base drivers for retrieving the speed of each slave.
   
mode=6 (balance-alb)
傳出及傳入皆自動負載平衡具容錯功能, 其中一張 Slave 網卡失效仍可持續運作;Slave 網卡 driver 需支援 setting hardware address 功能,不需 switch 支援及設定。  

若兩張網路卡設成ALB時,進來流量為1000M出去流量為2000M這時候無論接往何種交換器或集線器都可支援,ALB包含容錯功能如果一張網卡故障另一張依然運作,現在INTEL可以做到8張網卡併頻寬

Adaptive load balancing: includes balance-tlb plus receive load balancing (rlb) for IPV4 traffic, and does not require any special switch support. The receive load balancing is achieved by ARP negotiation. The bonding driver intercepts the ARP Replies sent by the local system on their way out and overwrites the source hardware address with the unique hardware address of one of the slaves in the bond such that different peers use different hardware addresses for the server.


3.檢查網卡是否支援 mii

有些較舊的網卡或driver可能不支援網卡綁定(bonding)的功能,可用以下指令確定

root@u21:/# mii-tool
eth0: negotiated 1000baseT-FD, link ok
eth1: negotiated 1000baseT-FD, link ok

上述的狀況是兩張網卡都有支援 mii

root@245 ~# mii-tool
SIOCGMIIPHY on 'eth0' failed: Operation not supported
SIOCGMIIPHY on 'eth1' failed: Operation not supported
no MII interfaces found

上述的狀況是兩張網卡沒有支援 mii 功能,也就是不能用 bonding




4.網卡設定

sudo vim /etc/network/interfaces

########################
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static

auto eth1
iface eth1 inet static

auto bond0
iface bond0 inet static
address 192.168.0.9
gateway 192.168.0.1
netmask 255.255.255.0
slaves eth0 eth1
bond-mode 0
bond-miimon 100
########################

sudo vim /etc/resolv.conf
########################
nameserver 168.95.192.1
nameserver 168.95.1.1
########################

紅字的部份說明:
由於 ubuntu 有內建圖型介面網路管理程式 (network-manager-gnome) 由這個套件所做的網路設定會跟我們設定在 /etc/network/interfaces 這裡面的內容相衝突。

換句話說,就是你一旦在 /etc/network/interfaces 裡面有針對網卡 (eth0, eth1) 做任何設定的話,圖型介面網路管理程式就會不起作用(會呈斷線狀態而無法設定,但實際上網路是通的),而以 /etc/network/interfaces 的設定內容為主。若你想回頭用圖型介面網路管理程式的話,那麼只要把 /etc/network/interfaces 此檔內與網卡 (eth0, eth1) 有關的設定清空後重開機,即可使用圖型介面網路管理程式,理解了這基本的情況之後,我們再來看看底下比較複雜的情況。

那4行紅字(eth0,eth1)的詳細說明:
auto eth0
iface eth0 inet static
auto eth1
iface eth1 inet static

現在的情況有些複雜,我按這兒的教學設定完(還沒有加那4行紅字),重開機之後,網路不通,得下指令 sudo /etc/init.d/networking restart 重啟一下網卡之後,網路才會通。每次開機都還要下這道指令才會通,太麻煩了。我估計是 (network-manager-gnome) 這個套件所引起的衝突,所以試著完整移除此套件,重開機之後,發現還是不行,它好像還是吃 (network-manager-gnome) 的設定,它的eth0還是會透過 dhcp 取得IP。後來找到了 (network-manager-gnome)  的設定檔是放在 /etc/NetworkManager 這個資料夾底下,於是我就把裡面的設定檔 (nm-system-settings.conf 和 dispatcher.d/01ifupdown) 給移除,重開機後,網路是通了,但是本機的 hostname 不見了,變成 localhost,雖然網路是通了,但我估計這樣可能還是會出問題,所以最後我才想到了這樣的解法。

我自己加進去的,那4行紅字(eth0,eth1),目地是讓 (network-manager-gnome) 判斷 /etc/network/interfaces 裡面已經有針對 eth0 和 eth1 的設定了,所以它就自動的不起作用了,而以 /etc/network/interfaces 的設定為主,若是不加這4行紅字的話 (network-manager-gnome) 會跑出來搗蛋。

bond-mode 0
這裡的 mode 有 0~6 幾種模式可選擇,請參考第2步驟的詳細說明


5.switch設定
每個不同廠牌的 switch 的設定方法都不同,這裡只能大概提一下,進 switch 設定畫面後,針對 port 位設定 trunk 或 Link Aggregation 即可。
Linsys switch 預設的帳號為 admin 密碼空白
我這台 Linsys switch 只能使用它的 web 介面 (IE only) 來設定
在web介面==>Port Management-->Link Aggregation-->Detail 然後選定幾個 port 位打勾之後,就可以將這些 port 位綁在一起使用。


6.速度測試

都設定好了之後,當然就要來試一下看看兩張網卡綁在一起的速度能不能超過 1000M

我們使用測網卡速度的程式是 iperf (中文介紹) ,  Windows 版本的執行檔 iperf.exe 可在此下載

iperf 是用來測網卡速度算是最好用的程式了,它可以排除硬碟資料傳輸不夠快的問題,直接操網卡的速度,當然你的CPU速度太慢的話,也是會影響網卡的速度。


ubuntu 底下,一行指令就能自動安裝 iperf 了

sudo aptitude install iperf

它的使用方法分成 server 端和 client 端。在我們這裡的情況,設定雙網卡綁定的那台機器就是要當 server 端,其它的電腦則當 client 端。

server 端,它的設定比較簡單,一行指令就搞定了
iperf -s

client 端的設定,它也是一行指令就行了,不過它帶的參數複雜一點。
指令範例如下:
iperf -c 192.x.x.x -i 10 -t 120 -P 10

-c 連線到某個 IP
-i 10 每10秒顯示一次
-t 120 持續運行120秒
-P 10  開 10 個線程

server 端若想要看當前網卡的流量,可用以下小程式來看

sudo aptitude install ifstat

ifstat -t


經過實測後,發現使用 mode=0 的模式,就能夠使server的頻寬超過 1000M 的速度。
也有自動容錯的功能,也就是說在傳檔案的途中,突然把一條網路線拔掉之後,它還是能正常的傳檔,只不過我在用 iperf 衝流量時,拔掉一條網路線時, iperf 曾引起server的網路當掉好一會兒,直到我把 iperf 這程序 kill 掉時,網路才恢愎正常,簡言之, mode=0 這個模式,就很好用啦!


補充說明:

傳輸速率
單顆 SATA 7200轉的硬碟 66~80 MB/s 左右
單顆   IDE 5400轉的硬碟 30~50 MB/s 左右
兩顆 SATA 7200轉的硬碟 110~120 MB/s 左右 (Raid 0)
10/100 Mbps 網卡 理論值可達 12.5 MB/s, 實際速度 11 MB/s 左右
10/100/1000 Mbps 網卡 理論值 125 MB/s,實際速度 110 MB/s 左右

由以上數字可知,在 1Gbps 的網路環境底下,單顆硬碟的傳輸速率根本達不到 1G 的速度,所以想要測網卡的速度,一定要排除硬碟的傳輸速率,這樣測出來的速度才會準確

測硬碟速率指令
hdparm -Tt /dev/sda



參考資料:
http://ubuntuforums.org/showthread.php?t=863316
http://www.howtoforge.com/network_bonding_ubuntu_6.10
https://help.ubuntu.com/community/UbuntuBonding
https://help.ubuntu.com/community/LinkAggregation
石頭閒語
Linux雙網卡綁定

想要自行查閱更多資料的話,可鍵入以下關鍵字搜尋: network bonding, Link Aggregation, teaming, 寬頻聚合, 頻寬聚合, 網卡綁定, 鏈路聚合...

LACP (Link Aggregation Control Protocal)
LACP 可稱為靜態埠聚集或頻寬聚集是 IEEE 標準規格 802.3ad 協定中的一部份


網卡綁定,別說中文沒有個統一的叫法,就連英文也都有他們自己的叫法:
INTEL 叫 LINK AGGREGATION
CISCO 叫 FAST ETHERCHANNEL
3COM 叫 PORT TRUNKING
國際標準叫 IEEE802.3ad

Ubuntu 客製化


Remastersys 是做客製化光碟最簡單的做法

1 裝好ubuntu,並改好所有設定和更新
2 安裝打包工具,Remastersys

a.編輯此檔,請下指令:

sudo gedit /etc/apt/sources.list



b.加入下列兩行:


註意:ubuntu 9.04 以前的版本,請加入底下這行
# Remastersys
deb http://www.geekconnection.org/remastersys/repository ubuntu/

註意: 若你是 ubuntu 9.10 或 10.04 以後的版本,要加入底下這行,因為它們使用的是 grub2 裝錯版本會使你的 ubuntu 不能開機
# Remastersys
deb http://www.geekconnection.org/remastersys/repository karmic/


c.請下指令

sudo apt-get update
sudo apt-get install remastersys



3.開始打包
請下指令

sudo remastersys backup custom.iso



運行時間大約10~20幾分鐘,此指令會備份你的系統,並產生一個 custom.iso 檔,位於 /home/remastersys 內,將此檔燒成光碟即可。用此光碟來安裝就可以你原來的設定安裝到別台上。(註:光碟開機會提示你要打install,才會進入安裝程序)


4.最後,清除由 remastersys 產生的臨時檔

sudo remastersys clean





參考資料1參考資料2


from http://dominic16y.world.edoors.com/CjNPYjaJc2Tk

tar 指令的常用語法

常用參數


  • -c 打包一個 tar 檔案

  • -x 解開一個 tar 檔案

  • -t 檢視 tar 檔案的內容

  • -z 使用 gzip 壓縮

  • -v 顯示建立 tar 檔案的過程

  • -P 使用絕對路徑

  • -f 指定 tar 檔案的檔案名稱(此參數的後面必須接目標檔名)

  • 常用語法

  • 建立壓縮檔

  • tar -czvf target.tgz /directory
    將 /directory 下的所有檔案及目錄打包成一個名稱為 target.tgz 的壓縮檔
  • 檢視壓縮檔的內容

  • tar -tzvf target.tgz
  • 解開壓縮檔的檔案到現行目錄

  • tar -xzvf target.tgz
  • 複製目錄

  • tar -cvf - /directory | tar -xvf -
    將 /directory 目錄下的檔案及次目錄複製到現行目錄,這樣做的好處是可以保留原來的檔案屬性

    SVN + Trac + Apache 完整安裝攻略

    How to use simple speedtest in RaspberryPi CLI

      pi@ChunchaiRPI2:/tmp $  wget -O speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py --2023-06-26 10:4...