雙網卡接入,既能添加網絡帶寬,同時又能做相應的冗余。通過這篇文章了解linux雙網卡(多網卡)的綁定方法
//測試的機器是centos6
===================================
操作部分:
0,先停止NetworkManager
service NetworkManager stop
chkconfig NetworkManager off //開機自啟動 關閉
1,配置文件
cp ifcfg-eth0{,.old} //備份網卡配置文件
cp ifcfg-eth1{,.old} //備份網卡配置文件
a, ifcfg-bond0文件配置
vim ifcfg-bond0 //創建bond0 配置文件 也可以用原有網卡配置文件修改
DEVICE=bond0 BONDING_OPTS="miimon=80 mode=0 primary=eth0 " //這一步也可以在下面 /etc/modprobe.d/bonidng.conf 里面填寫
IPADDR=10.0.0.7 NETMASK=255.255.255.0 GATEWAY=10.0.0.2 TYPE=Ethernet USERCTL=no BOOTPROTO=none ONBOOT=yes
b, ifcfg-eth0文件配置
DEVICE=eth0
TYPE=Ethernet
USERCTL=no
BOOTPROTO=dhcp
ONBOOT=yes
MASTER=bond0
SLAVE=yes
c, ifcfg-eth1文件配置
DEVICE=eth1
TYPE=Ethernet
USERCTL=no
BOOTPROTO=dhcp
ONBOOT=yes
MASTER=bond0
SLAVE=yes
d, 綁定模塊 配置文件 /etc/modprobe.d/bond.conf
有些文檔中用的配置文件為:/etc/modprobe.conf
會報錯WARNING: Deprecated config file /etc/modprobe.conf, all config files belong into /etc/modprobe.d/.
alias bond0 bonding
options bond0 miimon=100 mode=0 primary=eth0 //bond0文件或這里 配置一處就可以
2,開啟綁定
modprobe bonding //打開綁定模塊 lsmod | grep bonding //檢查模塊是否開啟 service network restart //重啟網絡
3,開機綁定,增加默認網關
echo “ifenslave bond0 eth0 eth1” >> /etc/rc.d/rc.local echo “route add default gw 10.0.0.1 ” >> /etc/rc.d/rc.local 能上網可不設置
4,檢查綁定
a
ifconfig -a //查看網卡信息 觀察mac地址
b
cat /proc/net/bonding/bond0 //查看綁定情況
5,取消綁定
rm ifcfg-bond0 //刪除 ifcfg-bond0文件
rm /etc/modprobe.d/bond.conf // 刪除bond.conf文件
mv ifcfg-eth0{.old,} //回復網卡默認配置文件
mv ifcfg-eth0{.old,} //回復網卡默認配置文件
rmmod bonding //對應之前的 modprobe bingding 不要忘記
service network restart //重啟網絡服務
6,測試
ping 斷網觀察
7,多網口綁定
for example : eth0+eth1=bond0 eth3+eth4=bond1
設置文件:/etc/modprobe.d/band.conf
band口同模式
alias bond0 bonding
alias bond1 bonding
alias bond3 bonding
options bonding miimon=200 mode=1 max_bonds=3 //max_bonds 配置的bond口個數
band口多模式
當2個或者多個bond網卡的參數(即bonding模塊的參數,如mode、miimon等)不同時,需要在加載bonding模塊時修改模塊的名稱(文檔中的說法是linux的模塊加載系統要求系統加載的模塊甚至相同模塊的不同實例都需要有一個唯一的命名)
install bond0 /sbin/modprobe --ignor-install bonding -o bond0 miimon=200 mode=1
install bond1 /sbin/modprobe --ignor-install bonding -o bond1 miimon=200 mode=0
install bond2 /sbin/modprobe --ignor-install bonding -o bond2 miimon=200 mode=1
install bond3 /sbin/modprobe --ignor-install bonding -o bond3 miimon=200 mode=6
等效於?:
alias bond0 bonding
options bond0 miimon=100 mode=1
alias bond1 bonding options bond1 miimon=100 mode=1
===================================
拓展閱讀:
1,miimon
miimon是用來進行鏈路監測的。 比如:miimon=100,那么系統每100ms監測一次鏈路連接狀態,如果有一條線路不通就轉入另一條線路;
2,mode
mode的值表示工作模式,他共有0,1,2,3,4,5,6六種模式,常用為0,6,1三種。
0,1可參考raid的模式
mode=0
表示load balancing (round-robin)為負載均衡方式,兩塊網卡都工作,但是與網卡相連的交換必須做特殊配置(這兩個端口應該采取聚合方式),因為做bonding的這兩塊網卡是使用同一個MAC地址
mode=1
表示fault-tolerance (active-backup)提供冗余功能,工作方式是主備的工作方式,也就是說默認情況下只有一塊網卡工作,另一塊做備份
mode=6
表示load balancing (round-robin)為負載均衡方式,兩塊網卡都工作,但是該模式下無需配置交換機,因為做bonding的這兩塊網卡是使用不同的MAC地址
mode=2
表示balance-x,提供負載均衡和冗余功能。
mode=3
表示broadcast,這個模式提供容錯性。
mode=4
表示802.3ad,提供了ethtool的迅速,以及使用了802.3ad模式
mode=5
表示balance-tlb,自動適應負載均衡,自動切換故障。在此基礎上Ethtool支持驅動。
注意:bonding只能提供鏈路監測,即從主機到交換機的鏈路是否接通。
如果只是交換機對外的鏈路down掉了,而交換機本身並沒有故障,那么bonding會認為鏈路沒有問題而繼續使用
3,NetworkManager
NM_CONTROLLED=yes這意味着網卡eth0得有NetworkManager托管。
當NM_CONTROLLED=yes 時,你想使你的網卡配置生效,要重啟下NetworkManager服務后,再重啟network服務。
當NM_CONTROLLED=no時,你想使你的網卡配置生效,不用重啟NetworkManager服務,直接重啟network服務就行了
4,交換機聚合
mode 0下bond所綁定的網卡的IP都被修改成相同的mac地址,正常情況下mac地址是全球唯一的,一個mac地址對應多個端口肯定使交換機迷惑了。所以 mode0下的bond如果連接到交換機,交換機這幾個端口應該采取聚合方式(cisco稱為 ethernetchannel,foundry稱為portgroup),因為交換機做了聚合后,聚合下的幾個端口也被捆綁成一個mac地址.我們的解 決辦法是,兩個網卡接入不同的交換機即可。
mode6模式下無需配置交換機,因為做bonding的這兩塊網卡是使用不同的MAC地址。
5,ethtool查看設置帶寬
cat /proc/net/bonding/bond0 #查看雙網卡信息
ethtool eth0 //查看eth0的的網卡信息
ethtoll -s eth0 speed 1000 //修改eth0 速度為1000M
ethtoo eth0 //查看eth0修改情況
/etc/init.d/network restart //重啟網絡服務
cat /proc/net/binding/bond0 //查看修改后的狀態
5,ifenslave 修改主網卡
ifenslave -c bond0 eth0 //修改主網卡
cat /proc/net/bonding/bond0 //查看修改結果