首页 > 代码库 > linux系统配置网卡绑定
linux系统配置网卡绑定
linux系统配置网卡绑定
一、centos6下配置网卡绑定(bonding)
1、bonding概述
(1)作用:就是将多块网卡绑定同一IP地址对外提供服务,可以实现高可用或者负载均衡。当然,直接给两块网卡设置同一IP地址是不可能的。通过bonding,虚拟一块网卡对外提供连接,物理网卡的被修改为相同的MAC地址。
(2)Bonding的工作模式
1)Mode 0 (balance-rr)轮转(Round-robin)策略:从头到尾顺序的在每一个slave接口上面发送数据包。本模式提供负载均衡和容错的能力
2)Mode 1 (active-backup)活动-备份(主备)策略:在绑定中,只有一个slave被激活。当且仅当活动的slave接口失败时才会激活其他slave。为了避免交换机发生混乱此时绑定的MAC地址只有一个外部端口上可见
3)Mode 3 (broadcast)广播策略:在所有的slave接口上传送所有的报文。本模式提供容错能力。
2、bonding配置(详细配置文档:/usr/share/doc/kerneldocversion/Documentation/networking
/bonding.txt)
(1)准备工作:需要关闭NetworkManager
[root@dayi123-6 ~]# serviceNetworkManager stop
(2)创建bonding配置文件(miimon是用来进行链路监测的。如果miimon=100,那么系统每100ms 监测一次链路连接状态,如果有一条线路不通就转入另一条线路,mode用来指定bonding的工作模式)
[root@dayi123-6 ~]# cd/etc/sysconfig/network-scripts/
[root@dayi123-6 network-scripts]#cat ifcfg-bond0 #bonding的配置
DEVICE=bond0 #设备名称
BOOTPROTO=dhcp #IP地址获取方式
BONDING_OPTS="miimon=100mode=1"
(3)配置需要bonding的网卡
[root@dayi123-6 network-scripts]#cat ifcfg-eth1 ifcfg-eth0 #物理网卡的配置
DEVICE=eth1
BOOTPROTO=dhcp
MASTER=bond0 #指定主设备为bond0
SLAVE=yes #将当前设备设置为从设备
USERCTL=no
DEVICE=eth0
BOOTPROTO=dhcp
MASTER=bond0
SLAVE=yes
USERCTL=no
(4)使配置生效(配置完成后需要重启网卡时配置生效,生效后查看网卡信息时,只有bonding设备有IP地址,物理网卡没有IP地址)
[root@dayi123-6 network-scripts]#service network restart #重启网卡使配置的bonding生效
[root@dayi123-6 network-scripts]#ifconfig
bond0 Link encap:Ethernet HWaddr 00:0C:29:D3:7B:9B
inet addr:172.16.254.250 Bcast:172.16.255.255 Mask:255.255.0.0
inet6 addr:fe80::20c:29ff:fed3:7b9b/64 Scope:Link
UP BROADCAST RUNNING MASTERMULTICAST MTU:1500 Metric:1
RX packets:862 errors:0 dropped:0overruns:0 frame:0
TX packets:23 errors:0 dropped:0overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:92932 (90.7 KiB) TX bytes:3326 (3.2 KiB)
eth0 Link encap:Ethernet HWaddr 00:0C:29:D3:7B:9B
UP BROADCAST RUNNING SLAVEMULTICAST MTU:1500 Metric:1
RX packets:298656 errors:0 dropped:0overruns:0 frame:0
TX packets:9015 errors:0 dropped:0overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:33161613 (31.6 MiB) TX bytes:1608752 (1.5 MiB)
eth1 Link encap:Ethernet HWaddr 00:0C:29:D3:7B:9B
UP BROADCAST RUNNING SLAVEMULTICAST MTU:1500 Metric:1
RX packets:425 errors:0 dropped:0overruns:0 frame:0
TX packets:0 errors:0 dropped:0overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:45922 (44.8 KiB) TX bytes:0 (0.0 b)
(5)查看bonding的信息(通过查看/proc/net/bonding/bond0能够查看网卡的的状态,在主备模式下那块网卡处于激活状态)
[root@dayi123-6 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: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
3、删除bonding
[root@dayi123-6 network-scripts]#ifconfig bond0 down #停掉bonding
[root@dayi123-6 network-scripts]#rm ifcfg-bond0 #删除bonding配置文件
二、centos7下配置网卡绑定(bonding)
1、网卡命名
(1)网卡命名机制,systemd对网络设备的命名方式
1) 如果Firmware或BIOS为主板上集成的设备提供的索引信息可用,且可预测则根据此索引进行命名,例如eno1
2) 如果Firmware或BIOS为PCI-E扩展槽所提供的索引信息可用,且可预测,则根据此索引进行命名,例如ens1
3) 如果硬件接口的物理位置信息可用,则根据此信息进行命名,例如enp2s0
4) 如果用户显式启动,也可根据MAC地址进行命名,enx2387a1dc56
5) 上述均不可用时,则使用传统命名机制
(2) 名称组成格式
en: Ethernet 有线局域网
wl: wlan无线局域网
ww: wwan无线广域网
名称类型:
o<index>: 集成设备的设备索引号
s<slot>: 扩展槽的索引号
x<MAC>: 基于MAC地址的命名
p<bus>s<slot>:enp2s1
(3)网卡设备的命名过程
第一步:udev, 辅助工具程序/lib/udev/rename_device/usr/lib/udev/rules.d/60-net.rules
第二步:biosdevname会根据/usr/lib/udev/rules.d/71-biosdevname.rules
(4)更改centos7网卡名称
CentOS从7.0版本以后,网卡的命名自动基于固件及拓扑结构和位置信息来固定。不会自动生成eth0等网卡名称,为了方便KVM的部署。此处,我们我们将网卡名称修改为eth0。
方法一:在安装系统的过程中修改网卡名称为eth0(在安装系统的选择界面,选择“install centOS 7” 按下”Tab”键在最下面加入”net.ifnames=0 biosdevname=0”如下图所示)
方法二:编辑/etc/grub2.cfg配置文件:(加入net.ifnames=0)
[root@dayi123 ~]# cat -n/etc/grub2.cfg | grep biosdevname
99 linux16 /vmlinuz-3.10.0-327.el7.x86_64root=UUID=90b71a77-dd23-4528-8338-d7e466927721 ro crashkernel=autobiosdevname=0 net.ifnames=0 rhgbquiet.UTF-8
或修改/etc/default/grub配置文件:(在下述行加入net.ifnames=0)
[root@dayi123 ~]#cat /etc/default/grub
……
GRUB_CMDLINE_LINUX="crashkernel=autobiosdevname=0 net.ifnames=0 rhgbquiet"
方法三:为grub2生成其配置文件
[root@dayi123 ~]#grub2-mkconfig-o /etc/grub2.cfg
注:通过方法二和方法三修改后需要重启系统是配置生效,同时也需要修改网卡配置文件中的网卡名称
2、nmcli命令的使用
(1)作用:地址配置工具
(2)用法:nmcli[ OPTIONS ] OBJECT{ COMMAND | help }(具体用法可以在使用时按Tab键补全)
选项:device
connection
[root@dayi123 network-scripts]#nmcli device show eth1 #查看网卡eth1的详细信息
GENERAL.DEVICE: eth1
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:0C:29:ED:0F:1B
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: Wired connection 1
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/6
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 10.0.0.3/24
IP4.GATEWAY: 10.0.0.2
IP4.DNS[1]: 10.0.0.2
IP4.DOMAIN[1]: localdomain
(3)通过nmcli修改网卡配置文件名称
[root@dayi123 network-scripts]#nmcli connection show #查看当前系统有多少套配置文件
NAME UUID TYPE DEVICE
virbr0 084f00d4-652e-44c8-83b7-70b469f89f31 bridge virbr0
eth0 d50e8c71-6416-4ab6-97ca-ad2002cd3353 802-3-ethernet eth0
Wired connection 1 370c3d11-868f-4efc-95bc-630169b62ef1 802-3-ethernet eth1
[root@dayi123 network-scripts]#nmcli connection modify Wired\ connection\ 1 connection.id eth1 #修改网卡配置文件名称
[root@dayi123 network-scripts]#nmcli connection show #查看修改后的名称
NAME UUID TYPE DEVICE
virbr0 084f00d4-652e-44c8-83b7-70b469f89f31 bridge virbr0
eth0 d50e8c71-6416-4ab6-97ca-ad2002cd3353 802-3-ethernet eth0
eth1 370c3d11-868f-4efc-95bc-630169b62ef1 802-3-ethernet eth1
(4)通过nmcli命令为网卡在不同的环境创建不同的配置文件
1)创建网卡配置文件
[root@dayi123 network-scripts]#nmcli connection add ifname eth1 type ethernet con-name eth1-home ip4192.168.1.2/24 gw4 192.168.1.1 #通过命令创建一份网卡配置文件
Connection ‘eth1-home‘(4e73fce1-800e-420d-902f-48b19c8dfc07) successfully added.
[root@dayi123 network-scripts]#nmcli connection show
……
eth1-home 4e73fce1-800e-420d-902f-48b19c8dfc07 802-3-ethernet --
[root@dayi123 network-scripts]#cp ifcfg-eth1-home ifcfg-eth1-office #通过复制创建网卡配置
[root@dayi123 network-scripts]#nmcli connection reload #加载复制的网卡配置
[root@dayi123 network-scripts]#nmcli connection show #查看生效后的配置
……
eth1-office 1bbd1be4-e93e-4b29-0ced-eaffbef36326 802-3-ethernet --
eth1-home 4e73fce1-800e-420d-902f-48b19c8dfc07 802-3-ethernet --
2)使创建的网卡配置文件生效
[root@dayi123 network-scripts]#nmcli connection up eth1-home #使配置eth1-home生效
Connection successfully activated(D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/9)
[root@dayi123 network-scripts]#nmcli connection show #查看当前生效的配置
NAME UUID TYPE DEVICE
…….
eth1-home 4e73fce1-800e-420d-902f-48b19c8dfc07 802-3-ethernet eth1
[root@dayi123 network-scripts]#ifconfig eth1 #查看eth1的IP地址
eth1:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
[root@dayi123 network-scripts]#nmcli device status #查看设备的状态
DEVICE TYPE STATE CONNECTION
virbr0 bridge connected virbr0
eth0 ethernet connected eth0
eth1 ethernet connected eth1-home
lo loopback unmanaged --
virbr0-nic tap unmanaged --
3)删除网卡配置文件
[root@dayi123 network-scripts]#nmcli connection delete eth1-home
Connection ‘eth1-home‘(4e73fce1-800e-420d-902f-48b19c8dfc07) successfully deleted.
[root@dayi123 network-scripts]#nmcli connection show #查看删除后可用的配置文件
(4)通过nmcli网络设置
1)用法:nmcliconnection modify IFACE[+|-]setting.propertyvalue
2)常用参数
Nmcli 修改参数 | 对应网卡配置文件选项 |
ipv4.method manual | BOOTPROTO=none |
ipv4.method auto | BOOTPROTO=dhcp |
ipv4.addresses “192.0.2.1/24 192.0.2.254” | IPADDR0=192.0.2.1 PREFIX0=24 GATEWAY0=192.0.2.254 |
ipv4.dns 8.8.8.8 | DNS0=8.8.8.8 |
ipv4.dns-search example.com | DOMAIN=example.com |
ipv4.ignore-auto-dns true | PEERDNS=no |
connection.autoconnectyes | ONBOOT=yes |
connection.id eth0 | NAME=eth0 |
connection.interface-name eth0 | DEVICE=eth0 |
802-3-ethernet.mac-address . . . | HWADDR= . . . |
#将网卡配置文件中的ONBOOT设置为no
[root@dayi123 network-scripts]# nmcliconnection modify eth1-home connection.autoconnect no
#设置dns
[root@dayi123 network-scripts]#nmcli connection modify eth1-home ipv4.dns 114.114.114.114
#为网卡再添加一个IP地址
[root@dayi123 network-scripts]#nmcli connection modify eth1-home +ipv4.addresses 192.168.1.100/24
[root@dayi123 network-scripts]#cat ifcfg-eth1-home | grep -E "IPADDR|DNS"
DNS1=114.114.114.114
IPADDR=192.168.1.2
IPADDR1=192.168.1.100
[root@dayi123 network-scripts]#nmcli connection modify eth1-home -ipv4.addresses 192.168.1.100/24 #删除设置的一个IP地址
3、通过nmcli命令实现bonding
(1)添加bonding接口
[root@dayi123 network-scripts]#nmcli connection add type bond con-name bond0 ifname bond0 mode active-backup #创建bonding接口,模式为主从模式
Connection ‘bond0‘(da41db3d-b705-4eca-8e0a-06e7c59a95eb) successfully added.
[root@dayi123 network-scripts]#nmcli connection show
……
bond0 da41db3d-b705-4eca-8e0a-06e7c59a95eb bond bond0
(2)为bonding接口添加从属接口
[root@dayi123 network-scripts]#nmcli connection add type bond-slave ifname eth0 master bond0 #将eth0接口添加到bond0的从属接口,添加后会生成新的配置文件bond-slave-eth0
Connection ‘bond-slave-eth0‘(681e4abb-f538-4756-b0bd-cd5a51eb0cf6) successfully added.
[root@dayi123 network-scripts]#nmcli connection add type bond-slave ifname eth1 master bond0 #将eth1接口添加到bond0的从属接口,添加后会生成新的配置文件bond-slave-eth1
Connection ‘bond-slave-eth1‘(0a1708cc-e9fc-48d6-b6c2-6349b14d06e0) successfully added.
(3)启动绑定
[root@dayi123 network-scripts]#nmcli connection up bond-slave-eth0 #启动绑定从属接口0
Connection successfully activated(D-Bus active path: /org/freedesktop/NetworkManager/Active
Connection/13)
[root@dayi123 network-scripts]#nmcli connection up bond-slave-eth1 #启动绑定从属接口1
Connection successfully activated(D-Bus active path: /org/freedesktop/NetworkManager/Active
Connection/14)
[root@dayi123 network-scripts]#nmcli connection up bond0 #启动绑定
Connection successfully activated(master waiting for slaves) (D-Bus active path: /org/freedesktop/
NetworkManager/ActiveConnection/15)
(4)查看bonding状态及信息
[root@dayi123 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: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
……
(5)修改bongding的工作模式
[root@dayi123 network-scripts]#cat ifcfg-bond0
…….
BONDING_OPTS=mode=broadcast #将主从模式修改广播模式
……
[root@dayi123 network-scripts]#nmcli connection up bond0 #使修改后的配置生效
Connection successfully activated(master waiting for slaves) (D-Bus active path:/org/freedesktop/NetworkManager/ActiveConnection/18)
[root@dayi123 network-scripts]#cat /proc/net/bonding/bond0 #查看状态
Ethernet Channel Bonding Driver:v3.7.1 (April 27, 2011)
Bonding Mode: fault-tolerance(broadcast)
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
(6)删除bonding
[root@dayi123 network-scripts]#nmcli connection delete bond0 #删除bonding配置文件
[root@dayi123 ~]# nmcliconnection delete bond-slave-eth0 #删除bonding从属接口配置文件
[root@dayi123 ~]# nmcliconnection delete bond-slave-eth1
三、centos7下通过网络组Network Teaming绑定
1、网络组概述
(1)网络组作用:是将多个网卡聚合在一起方法,从而实现冗错和提高吞吐量,提供更好的性能和扩展性
(2)网络组由内核驱动和teamd守护进程实现
(3)支持的方式:broadcast、roundrobin、activebackup、loadbalance、lacp(implements the 802.3ad LinkAggregation Control Protocol)
2、创建网络组(即创建banding)
(1)创建网络组接口
1)创建命令:nmclicon add type team con-nameCNAMEifnameINAME[configJSON]
2)参数说明:CNAME连接名,INAME接口名
JSON指定runner方式
格式:‘{"runner": {"name":"METHOD"}}‘
METHOD可以是broadcast,roundrobin,activebackup,loadbalance,lacp
[root@dayi123 ~]# nmcliconnection add type team con-name team0 ifname team0 config‘{"runner":{"name":"loadbalance"}}‘ #创建网络组接口,创建后会自动生成网络组配置文件
[root@dayi123 network-scripts]#ls ifcfg-team0 #生成的网络组配置文件
ifcfg-team0
#为网络组接口配置IP地址
[root@dayi123 network-scripts]#nmcli connection modify team0 ipv4.addresses 172.16.1.252/16
#指定网络接口组IP地址类型为手工指定
[root@dayi123 network-scripts]#nmcli connection modify team0 ipv4.method manual
#为网络组接口设置DNS和网关
[root@dayi123 network-scripts]#nmcli connection modify team0 ipv4.gateway 172.16.0.1
[root@dayi123 network-scripts]#nmcli connection modify team0 ipv4.dns 172.16.0.1
(2)创建port接口(即为网络组添加物理网卡)
1)用法:nmclicon add type team-slave con-nameCNAMEifnameINAMEmaster TEAM
2)参数:CNAME连接名
INAME网络接口名
TEAM网络组接口名
[root@dayi123 network-scripts]#nmcli connection add con-name team0-eth0 type team-slave ifname eth0 master team0 #将eth0添加到网络组,会生成新的配置文件team0-eth0
Connection ‘team0-eth0‘ (26f78262-827e-4576-a6dd-ffa633710cad)successfully added.
[root@dayi123 network-scripts]#nmcli connection add con-name team0-eth1 type team-slave ifname eth1 master team0 #将eth1添加到网络组,会生成新的配置文件team0-eth1
Connection ‘team0-eth1‘ (520a97b9-22ce-4e26-9c7f-ff2470260b11)successfully added.
[root@dayi123 network-scripts]#nmcli connection show #查看网络配置文件
……
team0 f6ad9858-fa1c-447c-b05c-d64026722648 team team0
team0-eth1 520a97b9-22ce-4e26-9c7f-ff2470260b11 802-3-ethernet --
team0-eth0 26f78262-827e-4576-a6dd-ffa633710cad 802-3-ethernet --
(3)启动网络组
1)启动网络组接口不会自动启动网络组中的port接口
2)启动网络组接口中的port接口总会自动启动网络组接口
3)禁用网络组接口会自动禁用网络组中的port接口
4)没有port接口的网络组接口可以启动静态IP连接
5)启用DHCP连接时,没有port接口的网络组会等待port接口的加入
[root@dayi123 network-scripts]#nmcli connection up team0 #启动网络组
Connection successfully activated(master waiting for slaves) (D-Bus active path:/org/freedesktop/NetworkManager/ActiveConnection/24)
#没有启动网络组接口静态IP可以生效,并可以ping通(前提示和物理网卡同一网段)
[root@dayi123 network-scripts]#ifconfig team0
team0:flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.16.1.252 netmask 255.255.0.0 broadcast 172.16.255.255
ether 4a:12:68:43:5b:74 txqueuelen 0 (Ethernet)
[root@dayi123 network-scripts]#nmcli connection show #启动网络组并不会启动网络组中接口
NAME UUID TYPE DEVICE
team0 f6ad9858-fa1c-447c-b05c-d64026722648 team team0
……
team0-eth1 520a97b9-22ce-4e26-9c7f-ff2470260b11 802-3-ethernet --
team0-eth0 26f78262-827e-4576-a6dd-ffa633710cad 802-3-ethernet --
#启动网络组中接口,启动网络组接口后,网络组接口ip地址才会失效
[root@dayi123 network-scripts]#nmcli connection up team0-eth0
[root@dayi123 network-scripts]#nmcli connection up team0-eth1
(4)查看网络组状态信息
[root@dayi123 ~]# teamdctl team0state
setup:
runner: loadbalance
ports:
……
(5)删除网络组
[root@dayi123 ~]# nmcliconnection down team0 #停掉网络组,停掉后物理网络会自动开启
#删除网络组port配置信息
[root@dayi123 ~]# nmcliconnection delete team0-eth0
Connection ‘team0-eth0‘(26f78262-827e-4576-a6dd-ffa633710cad) successfully deleted.
[root@dayi123 ~]# nmcliconnection delete team0-eth1
Connection ‘team0-eth1‘(520a97b9-22ce-4e26-9c7f-ff2470260b11) successfully deleted.
[root@dayi123 ~]# nmcliconnection delete team0 #删除网络组
Connection ‘team0‘(f6ad9858-fa1c-447c-b05c-d64026722648) successfully deleted.
[root@dayi123 ~]# nmcliconnection show #查看配置信息时,没有了网络组配置信息
NAME UUID TYPE DEVICE
virbr0 084f00d4-652e-44c8-83b7-70b469f89f31 bridge virbr0
eth0 d50e8c71-6416-4ab6-97ca-ad2002cd3353 802-3-ethernet eth0
eth1 370c3d11-868f-4efc-95bc-630169b62ef1 802-3-ethernet eth1
本文出自 “dayi123” 博客,请务必保留此出处http://dayi123.blog.51cto.com/12064061/1922759
linux系统配置网卡绑定