centos7配置网卡绑定

要求:将两张网卡绑定为一张网卡,em1为主,em2为备

一、将原来的两张网卡的配置文件备份

[root@localhost network-scripts]# mv ifcfg-em1 ifcfg-em1.bak
[root@localhost network-scripts]# mv ifcfg-em2 ifcfg-em2.bak

二、新建两张网卡新配置文件

[root@localhost network-scripts]# vim ifcfg-em1
TYPE=Ethernet
BOOTPROTO=none
USERCTL=no
DEVICE=em1
ONBOOT=yes
MASTER=bond0
SLAVE=yes
[root@localhost network-scripts]# vim ifcfg-em2
TYPE=Ethernet
BOOTPROTO=none
USERCTL=no
DEVICE=em2
ONBOOT=yes
MASTER=bond0
SLAVE=yes

三、新建ifcfg-bond0文件

[root@localhost network-scripts]# vim ifcfg-bond0
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
USERCTL=no
DEVICE=bond0
IPADDR=34.34.30.21
NETMASK=255.255.255.0
GATEWAY=34.31.30.1
DNS1=8.8.8.8
DNS2=202.106.0.20
NM_CONTROLLED=no

四、新建/etc/modprobe.d/bonding.conf文件

[root@localhost network-scripts]# vim /etc/modprobe.d/bonding.conf
alias bond0 bonding
options bond0 miimon=100 mode=1

设置开机使网卡绑定生效

[root@localhost network-scripts]# vim /etc/rc.d/rc.local

在文件最后加入一行

ifenslave bond0 em1 em2

五、停止系统NetworkManager服务

[root@localhost network-scripts]# systemctl stop NetworkManager
[root@localhost network-scripts]# systemctl disable NetworkManager

六、重启network服务

[root@localhost network-scripts]# systemctl restart network

状态检查

[root@localhost network-scripts]# ifconfig -a
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 172.28.5.124  netmask 255.255.255.128  broadcast 172.28.5.127
        inet6 fe80::529a:4cff:fe8b:8b6a  prefixlen 64  scopeid 0x20<link>
        ether 50:9a:4c:8b:8b:6a  txqueuelen 1000  (Ethernet)
        RX packets 4936  bytes 360383 (351.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 199  bytes 47662 (46.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

em1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 50:9a:4c:8b:8b:6a  txqueuelen 1000  (Ethernet)
        RX packets 82685  bytes 79127730 (75.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 41753  bytes 4315831 (4.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 16  

em2: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 50:9a:4c:8b:8b:6a  txqueuelen 1000  (Ethernet)
        RX packets 24229  bytes 1699544 (1.6 MiB)
        RX errors 1  dropped 0  overruns 0  frame 1
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 17  

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 150  bytes 13786 (13.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 150  bytes 13786 (13.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

可以发现bond0、em1 、em2三个网卡的MAC地址是一样的

[root@localhost network-scripts]# 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: em1
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: em1
MII Status: up
Speed: 100 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 50:9a:4c:8b:8b:6a
Slave queue ID: 0

Slave Interface: em2
MII Status: up
Speed: 100 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 50:9a:4c:8b:8b:6b
Slave queue ID: 0
[root@localhost network-scripts]# 

可以看到当前主网卡为em1

七、测试

将网卡em1网线拨出,再次执行

[root@localhost network-scripts]# 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: em2
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: em1
MII Status: up
Speed: 100 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 50:9a:4c:8b:8b:6a
Slave queue ID: 0

Slave Interface: em2
MII Status: up
Speed: 100 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 50:9a:4c:8b:8b:6b
Slave queue ID: 0
[root@localhost network-scripts]# 

当前活动网卡变成em2了

这里需要注意:如果使用了keepalived做高可用架构,需要修改keepalived的配置文件,将vip绑定到bond0网卡上,而不是之前的em1

vrrp_instance VI_2 { #配置虚拟路由实
  state BACKUP #定义该节点为MASTER节点
  interface bond0 #定义VIP绑定的物理网卡
  virtual_router_id 60 #设置虚路由拟路由id,同一集群的节点群必须相同
  priority 95 #设定优先级
......

并且防火墙需要放行bond网卡的vrrp数据包,之前是配置放行em1的vrrp数据包

[root@localhost network-scripts]# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface bond0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT 
[root@localhost network-scripts]# firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --in-interface bond0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
[root@localhost network-scripts]# firewall-cmd --reload

八、解绑

[root@localhost network-scripts]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# mv ifcfg-bond0 ifcfg-bond0.bak
[root@localhost network-scripts]# mv ifcfg-em1 ifcfg-em1-bond.bak 
[root@localhost network-scripts]# mv ifcfg-em2 ifcfg-em2-bond.bak 

将原来备份的网卡配置文件恢复

[root@localhost network-scripts]# mv ifcfg-em1.bak ifcfg-em1
[root@localhost network-scripts]# mv ifcfg-em2.bak ifcfg-em2

更名bonding.conf 以及卸载bonding驱动

[root@localhost network-scripts]# mv /etc/modprobe.d/bonding.conf /etc/modprobe.d/bonding.conf.bak
[root@localhost network-scripts]# rmmod bonding

重启network服务

[root@localhost network-scripts]# systemctl restart network

启动NetworkManager服务

[root@localhost network-scripts]# systemctl start NetworkManager
[root@localhost ~]# systemctl enable NetworkManager

 删除或者注释掉开机启动文件中的配置

[root@localhost network-scripts]# vim /etc/rc.d/rc.local 
touch /var/lock/subsys/local
#ifenslave bond0 em1 em2
原文地址:https://www.cnblogs.com/sky-cheng/p/14838788.html