原文
網卡綁定是通過把多張網卡綁定為一個邏輯網卡,實現本地網卡的冗余,帶寬擴容和負載均衡。bond在實際網絡應用部署中是一種常用的技術
下面介紹如何在CentOS服務器上實現網卡綁定

1、服務器ifconfig查看可以看到有兩塊網卡eth0,eth1

要實現物理網口eth0,eth1網卡綁定,綁定后的虛擬邏輯網口bond0 IP地址和原來的eth0的IP保持一致
2、modinfo bonding 檢查自己的操作系統是否支持bonding

如果沒有消息返回,說明內核不支持bonding,需要重新編譯內核
3、檢查ifenslave,是否有這個命令

4、接下來進入正題
在cd /etc/sysconfig/network-scripts/下創建ifcfg-bond0配置文件
[root@YuanFan-CentOS ~]# cd /etc/sysconfig/network-scripts/
[root@YuanFan-CentOS network-scripts]# vi ifcfg-bond0
DEVICE=bond0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.31.2
NETMASK=255.255.255.0
GATEWAY=192.168.31.1
DNS1=114.114.114.114
DNS2=1.1.1.1
USERCTL=no

接下來修改ifcfg-eth0和ifcfg-eth1兩塊物理網卡的配置文件
將原有的IP、子網掩碼、網關信息等信息全部刪除,修改成如下格式
[root@YuanFan-CentOS ~]# cd /etc/sysconfig/network-scripts/
[root@YuanFan-CentOS network-scripts]# vi ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no

[root@YuanFan-CentOS network-scripts]# vi ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no

5、修改modprobe相關設定文件,並加載bonding模塊
[root@YuanFan-CentOS ~]#
vi /etc/modprobe.d/bonding.conf
alias bond0 bonding
options bonding mode=6 miimon=200

加載bonding模塊
[root@YuanFan-CentOS ~]# modprobe bonding
如果這時是SecureCRT等SSH終端連接的服務器后台,會話可能會斷開,在服務器上確認bonding模塊是否加載,然后用service network restart或者/etc/init.d/network restart命令重啟網絡
[root@YuanFan-CentOS ~]# lsmod | grep bonding
bonding 132885 0
[root@YuanFan-CentOS ~]# service network restart
Shutting down interface bond0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface bond0: Determining if ip address 192.168.31.2 is already in use for device bond0...
[ OK ]
[root@YuanFan-CentOS ~]#

查看bond網卡綁定后的模式
cat /proc/net/bonding/bond0
[root@YuanFan-CentOS ~]# cat /proc/net/bonding/bond0

6、測試一下負載均衡功能
手動斷開eth0鏈路的網線,看網絡是否還通
如下圖所示ethtool eth0 可以看到eth0網口已經斷開,右側可以看到ping 192.168.31.1網關一直可以ping通

注:mode bond網卡綁定的幾種模式之前有介紹,請參見本公眾號歷史文章
mode:Mode of operation
0 for balance-rr,
1 for active-backup,
2 for balance-xor,
3 for broadcast,
4 for 802.3ad,
5 for balance-tlb,
6 for balance-alb (charp)
