在伺服器環境中,雙網卡綁定(bonding)能提供高可用與負載平衡的效果。以下以中標麒麟系統為例,示範如何設定 bond0 與 bond1 兩組綁定,並說明 mode=1(主備)與 mode=0(負載平衡)的差異。
先進入網路設定目錄 /etc/sysconfig/network-scripts/,建立 bond0 的設定檔:
cat ifcfg-bond0 DEVICE=bond0 NM_CONTROLLED=no # 設為 yes 時不需重啟網路即可生效 BOOTPROTO=static ONBOOT=yes IPADDR=10.225.3.164 NETMASK=255.255.255.0 GATEWAY=10.225.3.254 DNS1=10.225.5.16 USERCTL=no BONDING_OPTS="mode=1 miimon=100" # RedHat 6 可直接在此設定
再建立 bond1:
cat ifcfg-bond1 DEVICE=bond1 NM_CONTROLLED=no BOOTPROTO=static ONBOOT=yes IPADDR=192.168.0.1 NETMASK=255.255.255.0 USERCTL=no BONDING_OPTS="mode=1 miimon=100" # 1 為高可用,0 為負載均衡
接著把 eth0~eth3 分別掛到對應的 bond 上:
cat ifcfg-eth0 DEVICE="eth0" NM_CONTROLLED=no ONBOOT=yes TYPE=Ethernet BOOTPROTO=static MASTER=bond0 SLAVE=yes USERCTL=no
eth1 同樣 MASTER=bond0;eth2、eth3 則改為 MASTER=bond1,其餘欄位相同。
編輯 /etc/rc.d/rc.local,加入:
ifenslave bond0 eth0 eth1
在主備模式下,當某一個網路介面失效(例如主交換器斷電)時,系統會依 rc.local 中指定的順序自動切換到備用網卡,網路不會中斷,達到失效保護的效果。
接著關閉 NetworkManager,否則 eth1 等網卡可能各自持有 IP,路由表也會異常:
chkconfig NetworkManager off service NetworkManager stop chkconfig network on service network restart
可透過以下指令查看目前 bond1 正在使用哪一張網卡,並用 ifdown/ifup 起停埠來測試冗餘是否正常:
cat /proc/net/bonding/bond1
正常情況下,ifconfig 會看到 bond0、bond1 各自的 IP,而 eth0~eth3 皆顯示為 SLAVE 狀態,例如:
bond0 Link encap:Ethernet HWaddr C4:54:44:88:DE:E4 inet addr:10.225.3.167 Bcast:10.225.3.255 Mask:255.255.255.0 UP BROADCAST RUNNING MASTER MULTICAST ... eth0 Link encap:Ethernet HWaddr C4:54:44:88:DE:E4 UP BROADCAST RUNNING SLAVE MULTICAST ...
若採 mode=0,可實現雙網卡負載均衡(例如單卡 100M,雙卡理論上可達 200M,但千兆網卡實際綁定約在 140~150M 之間)。重點步驟如下:
1、確認系統支援 bonding:cat /boot/config-xxxx | grep -i bonding,回傳 CONFIG_BONDING=m 即可。
2、先備份網卡設定目錄。
3、建立 ifcfg-bond0,內容含 BONDING_OPTS="mode=0 miimon=200"。
4、修改 ifcfg-em1、ifcfg-em2,加入 MASTER=bond0、SLAVE=yes。
5、在 /etc/modprobe.d/dist.conf 最後加上 alias bond0 bonding。
6、在 /etc/rc.d/rc.local 加上 ifenslave bond0 eth0 eth1。
7、關閉 NetworkManager 後重開機。
8、用 ifconfig 確認 em1、em2 的 MAC 與 bond0 一致。
9、用 cat /proc/net/bonding/bond0 查看運行狀態。