Linux 單網卡多IP 及 多網卡多IP備援

一般而言,在 Linux 上最常用到的便是 Bridge ... 關於這個在之前已經 PO 過


這一篇是要來記錄一下在 Linux 上怎樣做到 單網卡多IP 及 多網卡多IP備援機制
首先 ... 單網卡多IP非常的簡單 ... 鳥哥的網頁也寫的非常清楚 !


( 當然細節還會扯到一些路由的問題 ... 有興趣者可以再自己研究)

直接講怎樣操作比較快 ! 先直接看一下目前狀況

# ifconfig 


然後直接把 eth0 複製一個為 eth0:0,然後進去編輯,修改 IP (如上圖紅框)

# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-script/ifcfg-eth0:0
# vi /etc/sysconfig/network-scripts/ifcfg-eth0:0


最後直接把新的 eth0:0 n啟動就可以啦 ! 接著用 FTP 連線來測測看吧 !


到這邊 ... 單網卡多IP 已經搞定 ... 主要是用來分散流量 ... 

下面接著來操作多網卡多IP備援 .... 先說明一下有那幾種模式 ...


在 Linux Kernel bonding 的 kernel module 裡面,是可以依設定 mode=x 的方式來決定運作的模式,主要有下面幾種 mode ,分別如下所示:

1. mode=0 (balance-rr)
Round-robin policy: Transmit packets in sequential order from the first available slave through the last. This mode provides load balancing and fault tolerance.
當兩張網路卡設成 Balance 時,還會包含容錯功能 (Fault tolerance),如果有一張網卡掛點另一張還是可以正常運作滴。

2. mode=1 (active-backup)
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.
假設有兩張網卡時其中一張是 Primary 而另一張是 Secondary (也就是 backup) 這時候流量只在 Primary 的網卡上傳,當 Primary 掛點時 Secondary 會自動啟動接手變為 Primary,另外如果原來 Primary 恢復時會自動變成 Secondary 了。

3. 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.

4. mode=3 (broadcast)
Broadcast policy: transmits everything on all slave interfaces. This mode provides fault tolerance.

5. mode=4 (802.3ad : Dynamic Link Aggregation)
假設將兩張網路卡設成 Dynamic Link Aggregation 時,進來流量為 2G 而出去流量為 2G,這種方式需將網卡接到有支援 Dynamic Link Aggregation 的 Switch,另外,在 Dynamic Link Aggregation 下也有容錯功能 (Fault tolerance),如果有一張網卡掛點另一張還是可以繼續工作。

6. mode=5 (balance-tlb : Transmit load balancing)
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.

7. mode=6 (balance-alb : Adaptive load balancing)
Adaptive load balancing: includes balance-tlb + 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 server on their way out and overwrites the src hw address with the unique hw address of one of the slaves in the bond such that different clients use different hw addresses for the server.