Linux服务器双网卡绑定实例

双网卡绑定在项目应用中较多,通常配置上网卡绑定有两种方法,第一种是采用传统方法修改配置文件,第二种是采用新的命令直接生成配置文件。

Bonding

传统配置方法步骤如下:
第一步:编辑网卡eth1配置文件
第二步:复制网卡eth1配置文件,重命名为eth2
第三步:复制网卡eth1配置文件,重命名为bond0,并设置双网卡绑定的相关选项如:双网卡之间检测周期100ms,双网卡之间的模式模式1
为主被模式,模式0为双主模式,如BONDING_OPTS=”miimon=100 mode=0″
第四步:重启网络服务

就是讲到快网卡绑定到同一IP地址对外服务,可以实现高可用或者负载均衡。当然,直接给两块网卡设置同一IP地址是不可能的。通过bonding,虚拟一块网卡对外提供连接,物理网卡被修改为同一MAC地址。

如下实例 :
[root@CentOS
/etc/sysconfig/network-scripts]#vim ifcfg-eth1
1 DEVICE=eth1                                                           
#编辑配置文件eth1
  2 BOOTPROTO=none
  3 MASTER=bond0
  4 SLAVE=yes
  5 USERCTL=no                                                         
    #是否允许普通用户管理此端口
[root@centos7  /etc/sysconfig/network-scripts]#cp ifcfg-eth1
ifcfg-eth2
cp: overwrite  ‘ifcfg-eth2’? y                                         
  #复制配置文件
[root@centos7  /etc/sysconfig/network-scripts]#vim ifcfg-eth2
1 DEVICE=eth2                                                           
#修改DEVICE=eth2
  2  BOOTPROTO=none
  3 MASTER=bond0
  4 SLAVE=yes
  5 USERCTL=no
[root@centos7  /etc/sysconfig/network-scripts]#cp ifcfg-eth1
ifcfg-bond0
[root@centos7  /etc/sysconfig/network-scripts]#vim ifcfg-bond0
1 DEVICE=bond0
  2 BOOTPROTO=none
  3 BONDING_OPTS=”miimon=100 mode=0″
  4 IPADDR=192.168.10.100
  5 PREFIX=24
  6 GATEWAY=192.168.10.254
  7 DNS1=114.114.114.114
  8 DNS2=8.8.8.8
[root@centos7  ~]#systemctl restart network                         
      #重启网络服务
[root@centos7  ~]#cat /proc/net/bonding/bond0
Ethernet Channel  Bonding Driver: v3.7.1 (April 27, 2011)
 
Bonding Mode: load  balancing (round-robin)
MII Status: up
MII Polling  Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface:  eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure  Count: 0
Permanent HW addr:  00:0c:29:cf:ed:5f
Slave queue ID: 0
 
Slave Interface:  eth2
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure  Count: 0
Permanent HW addr:  00:0c:29:cf:ed:69
Slave queue ID: 0
 
脚本中通常采用nmcli命令实现上网卡绑定,如若通过配置文件进行双网卡绑定较为复杂,具体命令步骤如下(在nmcli命令中con-name代表生成配置文件的名的名称,ifname是DEVICE的名称):
第一步:创建bond0
nmcli con add type bondcon-name bond0 ifname bond0 mode 1 miimon 100
第二步:加载slave成员
nmcli con add typebond-slave ifname eth1 master bond0
nmcli con add typebond-slave ifname eth1 master bond0
第三步:启动bond-slave-eth1和2成员,以及bond0成员
nmcliconnection up “bond-slave-eth1”
nmcliconnection up “bond-slave-eth1”
nmcliconnection up “bond0”
如若删除bond0
第四步:让bond0先down
nmcli connectiondown bond0
第五步:删除配置文件
nmcli connection deletebond0
nmcli connectiondelete bond-slave-eth1
nmcli connectiondelete bond-slave-eth2
具体实例演示:

一 Bonding 的工作模式

[root@centos7 ~]#nmcli con  add type bond con-name bond0 ifname bond0
mode 1 miimon 100
[root@centos7 ~]#nmcli con add type bond-slave ifname eth1 master
bond0
[root@centos7 ~]#nmcli con add type bond-slave ifname eth2 master
bond0
[root@centos7 ~]#nmcli connection show 
#发现bond-slave-eth1和2并无启动
NAME            UUID                                  TYPE           
DEVICE
bond-slave-eth1  aa3662d9-0382-4640-ab56-fb40d8da41de  802-3-ethernet 
—   
bond0            882245a8-c2f8-4fbd-9fbe-69769367bc43  bond           
bond0 
eth2              9b2cd717-a336-4739-9a0c-c93ba560daee  802-3-ethernet 
eth2 
eth1              32ccd8a8-a6fb-4516-94da-48d394c338d8  802-3-ethernet 
eth1 
eth0              90e23a1d-6f6e-461d-b2a3-15eab49cf43b  802-3-ethernet 
eth0 
vlan-VLAN10      4dc6d0e3-154c-0cff-3dd4-adef4ad932f1  vlan           
—   
bond-slave-eth2  c06c8a6e-7ea9-410b-a5f0-dd2337bbd0ba  802-3-ethernet 
— 
[root@centos7 ~]#nmcli connection up “bond-slave-eth1” 
#启动bond-slave-eth1
Connection successfully activated (D-Bus active path:
/org/freedesktop/NetworkManager/ActiveConnection/29)
[root@centos7 ~]#nmcli connection up “bond-slave-eth2” 
#启动bond-slave-eth2
Connection successfully activated (D-Bus active path: 
/org/freedesktop/NetworkManager/ActiveConnection/30)
[root@centos7 ~]#nmcli connection show           
#查看验证发现所有端口正常
NAME            UUID                                  TYPE           
DEVICE
bond-slave-eth1  aa3662d9-0382-4640-ab56-fb40d8da41de  802-3-ethernet 
eth1 
bond0            882245a8-c2f8-4fbd-9fbe-69769367bc43  bond           
bond0 
eth2            9b2cd717-a336-4739-9a0c-c93ba560daee  802-3-ethernet 
—     
eth1              32ccd8a8-a6fb-4516-94da-48d394c338d8  802-3-ethernet 
—   
eth0              90e23a1d-6f6e-461d-b2a3-15eab49cf43b  802-3-ethernet 
eth0 
vlan-VLAN10      4dc6d0e3-154c-0cff-3dd4-adef4ad932f1  vlan           
—   
bond-slave-eth2  c06c8a6e-7ea9-410b-a5f0-dd2337bbd0ba  802-3-ethernet 
eth2
[root@centos7 ~]# ip a
13: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500
qdisc  noqueue state UP                                     
#无需重启网络服务,已经自动获取IP地址
    link/ether 00:0c:29:cf:ed:5f  brd ff:ff:ff:ff:ff:ff
    inet 172.18.250.48/16  brd 172.18.255.255 scope global dynamic
bond0
      valid_lft 85630sec  preferred_lft 85630sec
    inet6  fe80::20c:29ff:fecf:ed5f/64 scope link
      valid_lft forever  preferred_lft forever
[root@centos7 ~]#cat /proc/net/bonding/bond0  #查看内存运行状态
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
 
Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth1
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
 
Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:cf:ed:5f
Slave queue ID: 0
 
Slave Interface: eth2
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:cf:ed:69
Slave queue ID: 0
 
如若想参考纯正版的资料可到官网:
https://access.[RedHat](https://www.linuxidc.com/topicnews.aspx?tid=10).com/documentation/en-US/Red\_Hat\_Enterprise\_Linux/7/html/Networking\_Guide/sec-Configure\_802\_1Q\_VLAN\_Tagging\_Using\_the\_Command\_Line.html

    Mode 0 (balance-rr)

或Linux系统本身的man文档:
cat 
/usr/share/doc/kernel-doc-version/Documentation/networking/bonding.txt
https://www.kernel.org/doc/Documentation/networking/bonding.txt

轮转(Round-robin)策略:从头到尾顺序的在每一个slave接口上面发送数据包。本模式提供负载均衡和容错的能力。

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-08/146460.htm

    Mode 1 (active-backuop)

图片 1

活动-备份(主备)策略:在绑定中,只有一个slave被激活,当且仅活动的slave接口失败是才会激活其他的slave。为了避免交换机发生混乱此时绑定的MAC地址只有一个外部端口上可见。

    Mode 3 (broadcast)

广播策略:在所有的slave接口上发送所有的报文。本模式提供容错能力。

二 Bonding配置

1 创建bonding设备的配置文件

[root@CentOS68 ~]# vim /etc/sysconfig/network-scripts/ifcfg-bond0

DEVICE=bond0

BOOTPROTO=none

BONDING_OPTS=”miimon=100 mode=0″

IPADDR=200.200.200.200

PREFIX=24

GATEWAY=200.200.200.1

miimon是设置检测链路的时间,单位为毫秒。如果miimon=100,那么系统每100毫秒检测一侧链路的连接状态,如果有一条线路不通就转入另一条线路。

mode用来指定模式

2 创建slave的配置文件

既就是要加入的网卡的配置文件,这里使用eth1、eth2两块网卡,下来修改两块网卡的配置

[root@CentOS68 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1

DEVICE=eth1

TYPE=Ethernet

BOOTPROTO=none

MASTER=bond0

SLAVE=yes

USERCTL=0

MASTER指定测网卡属于哪个bond。SLAVE指定从属关系。USERCTL指定用户是否有控制权限。

eth2和eth1配置类似

[root@CentOS68 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth2

DEVICE=eth2

TYPE=Ethernet

BOOTPROTO=none

MASTER=bond0

SLAVE=yes

USERCTL=0

3 重启网络服务

[root@CentOS68 ~]# service NetworkManager stop #centos6上不稳定,建议关闭
[root@CentOS68 ~]# service network restart

Shutting down interface bond0: [ OK ]

Shutting down interface eth0: [ OK ]

Shutting down loopback interface: [ OK ]

Bringing up loopback interface: [ OK ]

Bringing up interface bond0: Determining if ip address 200.200.200.200
is already in use for device bond0…

[ OK ]

Bringing up interface eth0: Determining if ip address 192.168.29.68 is
already in use for device eth0…

[ OK ]

 

可以看到已经启动成功,再来看下IP地址和MAC

[root@CentOS68 ~]# ip a

[……]

7: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast master bond0 state UP qlen 1000

link/ether 00:0c:29:74:74:45 brd ff:ff:ff:ff:ff:ff

8: eth2: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast master bond0 state UP qlen 1000

link/ether 00:0c:29:74:74:45 brd ff:ff:ff:ff:ff:ff

9: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc
noqueue state UP

link/ether 00:0c:29:74:74:45 brd ff:ff:ff:ff:ff:ff

inet 200.200.200.200/24 brd 200.200.200.255 scope global bond0

inet6 fe80::20c:29ff:fe74:7445/64 scope link tentative dadfailed

valid_lft forever preferred_lft forever

可以看到IP地址已经生效,并且MAC地址都相同。

4 查看bond0的状态

[root@CentOS68 ~]# cat /proc/net/bonding/bond0

Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

 Bonding Mode: load balancing (round-robin)

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0

Slave Interface: eth1

MII Status: up

Speed: 1000 Mbps

Duplex: full

Link Failure Count: 0

Permanent HW addr: 00:0c:29:74:74:45

Slave queue ID: 0

Slave Interface: eth2

MII Status: up

Speed: 1000 Mbps

Duplex: full

Link Failure Count: 0

Permanent HW addr: 00:0c:29:74:74:4f

Slave queue ID: 0

好了,一切都很顺利

5 删除bond

[root@CentOS68 ~]# ifconfig bond0 down

[root@CentOS68 ~]# rmmod bonding

看下是否删除成功,

[root@CentOS68 ~]# ifconfig bond0

bond0: error fetching interface information: Device not found

已经找不到bond0,说明删除成功。

详细帮助:/etc/share/doc/kernel-doc-version/Documentation/networking/bonding.txt

 

三 nmcli实现bonding

 1 添加bonding接口

[root@centos7 ~]# nmcli connection add type bond con-name mybond0 ifname bond0 mode active-backup

Connection ‘mybond0’ (cc101e79-3dcb-49ee-8164-89c9e877c315) successfully
added.

2添加从属接口

[root@centos7 ~]# nmcli connection add type bond-slave ifname ens34 master mybond0

Connection ‘bond-slave-ens34’ (9c8ff11a-72ef-4d63-a950-2ab777d2a1ea)
successfully added.

[root@centos7 ~]# nmcli connection add type bond-slave ifname ens38 master mybond0

Connection ‘bond-slave-ens38’ (c40b3e6f-32f4-4d1a-a323-aed2519cf865)
successfully added.

注意:如没有指定从属接口连接名,则该名称是接口名称加类型构成

3 启动从属接口

[root@centos7 ~]# nmcli connection up bond-slave-ens34

Connection successfully activated (D-Bus active path:
/org/freedesktop/NetworkManager/ActiveConnection/40)

[root@centos7 ~]# nmcli connection up bond-slave-ens38

Connection successfully activated (D-Bus active path:
/org/freedesktop/NetworkManager/ActiveConnection/41)

4 启动bonding 绑定

[root@centos7 ~]# nmcli connection up mybond0

Connection successfully activated (master waiting for slaves) (D-Bus
active path: /org/freedesktop/NetworkManager/ActiveConnection/48)

5 查看绑定

[root@centos7 ~]# nmcli connection show

NAME UUID TYPE DEVICE

bond-slave-ens34 9c8ff11a-72ef-4d63-a950-2ab777d2a1ea 802-3-ethernet
ens34

bond-slave-ens38 c40b3e6f-32f4-4d1a-a323-aed2519cf865 802-3-ethernet
ens38

mybond0 cc101e79-3dcb-49ee-8164-89c9e877c315 bond bond0

6 添加IP地址

[root@centos7 ~]# nmcli connection modify mybond0 ipv4.method manual ipv4.addresses 200.1.1.1/24

7 删除bond绑定

(1)关闭bond

[root@centos7 ~]# nmcli connection down mybond0

Connection ‘mybond0’ successfully deactivated (D-Bus active path:
/org/freedesktop/NetworkManager/ActiveConnection/18)

[root@centos7 ~]#

(2)删除bond

[root@centos7 ~]# nmcli connection delete mybond0

Connection ‘mybond0’ (9ed2f027-bbb1-44d1-87fc-7f7275d7fe70) successfully
deleted.

(3)删除从属接口

[root@centos7 ~]# nmcli connection delete bond-slave-ens34

Connection ‘bond-slave-ens34’ (a46ad620-d4b5-43f9-a73a-447785504a17)
successfully deleted.

[root@centos7 ~]# nmcli connection delete bond-slave-ens38

Connection ‘bond-slave-ens38’ (66b1f44d-ad71-4576-8061-f6cba2b976e8)
successfully deleted.

 

好了