整理网卡绑定的两种方法

cnetos 网卡绑定 eth0+eth1做双网卡绑定

​ 需要说明的是如果想做成mode 0的负载均衡,仅仅设置这里options bond0 miimon=100 mode=0是不够的,与网卡相连的交换机必须做特殊配置(这两个端口应该采取聚合方式),因为做bonding的这两块网卡是使用同一个MAC地址.从原理分析一下(bond运行在mode 0下):

​ mode 0下bond所绑定的网卡的IP都被修改成相同的mac地址,如果这些网卡都被接在同一个交换机,那么交换机的arp表里这个mac地址对应的端口就有多 个,那么交换机接受到发往这个mac地址的包应该往哪个端口转发呢?正常情况下mac地址是全球唯一的,一个mac地址对应多个端口肯定使交换机迷惑了。所以 mode0下的bond如果连接到交换机,交换机这几个端口应该采取聚合方式

1、首先得有两张网卡来做实验环境。

[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens34: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP group default qlen 1000
    link/ether 00:0c:29:f3:00:f7 brd ff:ff:ff:ff:ff:ff
3: ens33: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP group default qlen 1000
    link/ether 00:0c:29:f3:00:f7 brd ff:ff:ff:ff:ff:ff

2、/etc/sysconfig/network-scripts/目录下建立ifcfg-bond0文件,内容如下:

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=192.168.2.128
NETMASK=255.255.252.0    
GATEWAY=192.168.2.254  
BONDING_OPTS="mode=0 miimon=200"    # miimon表示链路的检测时间

3、修改相应需要绑定的网卡

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
# Generated by dracut initrd
NAME="ens33"
DEVICE="ens33"
ONBOOT=yes
NETBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no

3、修改第二张网卡

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens34
# Generated by dracut initrd
NAME="ens34"
DEVICE="ens34"
ONBOOT=yes
NETBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no

4、修改/etc/modprobe.d/dist.conf ,最后面加入;做了bond,新建一个配置文件。

[root@localhost ~]# vi /etc/modprobe.d/dist.conf 
alias bond0 bonding

5、先执行下面3个命令

这和上面的NM_CONTROLLED=no有关,但是我在实际配置的过程中感觉影响不大。
service NetworkManager stop     
chkconfig NetworkManager off
modprobe bonding    # 相当于是要告诉内核,做了bond,让刚才的配置生效。

最后重启网络服务,这样就成功的绑定了。

centos7 nmcli工具管理

需要开启NetworkManager服务,转自其它博客亲测可用。

1.创建bond设备

#创建bond设备
[root@liyong: network-scripts]# nmcli con add con-name bond0 ipv4.method static ipv4.addresses 192.168.2.128/24 type bond mode balance-rr
Connection 'bond0' (2d586dd8-1153-425b-9fd6-a4a8eb4722aa) successfully added.

# balance-rr 就是 mode=0

2.查看连接

# 查看连接
[root@liyong: network-scripts]# nmcli con show
NAME                UUID                                  TYPE            DEVICE  
System ens37        4a5516a4-dfa4-24af-b1c4-e843e312e2fd  802-3-ethernet  ens37   
Wired connection 1  9a1b7a19-2e8d-330b-a2ed-b6f3923a8e1e  802-3-ethernet  ens33   
bond0               2d586dd8-1153-425b-9fd6-a4a8eb4722aa  bond            nm-bond 
virbr0              37df32ab-0188-4a57-ad31-b5701f027fc4  bridge          virbr0 

3.添加网关

#添加网关
[root@liyong: network-scripts]# nmcli connection modify bond0 ipv4.gateway 192.168.2.254

4.查看设置

cat ifcfg-bond0 

DEVICE=nm-bond
TYPE=Bond
BONDING_MASTER=yes
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=bond0
UUID=2d586dd8-1153-425b-9fd6-a4a8eb4722aa
ONBOOT=yes
BONDING_OPTS=mode=active-backup
IPADDR=176.30.30.30
#添加slave
nmcli connection add type bond-slave master bond0 ifname ens33
Connection 'bond-slave-ens33' (fbf9a891-e8d5-4ef2-a8d7-fbeb6e2fe4bb) successfully added.

nmcli connection add type bond-slave master bond0 ifname ens37
Connection 'bond-slave-ens37' (443d40ad-bb42-4609-b4cd-7904abfbd2c6) successfully added.

#先启动slave
nmcli connection up bond-slave-ens33
nmcli connection up bond-slave-ens37
#再启动bond
nmcli connection up bond0
cat /proc/net/bonding/nm-bond 
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: ens33
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: ens33
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:21:a9:b3
Slave queue ID: 0

Slave Interface: ens37
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 2
Permanent HW addr: 00:0c:29:21:a9:bd
Slave queue ID: 0
#当断开enss33 时ens37自动启用
cat /proc/net/bonding/nm-bond 
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: ens37
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: ens33
MII Status: down
Speed: Unknown
Duplex: Unknown
Link Failure Count: 1
Permanent HW addr: 00:0c:29:21:a9:b3
Slave queue ID: 0

Slave Interface: ens37
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 3
Permanent HW addr: 00:0c:29:21:a9:bd
Slave queue ID: 0

至此bond功能实现

posted @ 2020-10-21 10:11  Gshelldon  阅读(1149)  评论(0编辑  收藏  举报