linux虚拟网络接口---bonding配置

Posted 拼命姑娘fh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux虚拟网络接口---bonding配置相关的知识,希望对你有一定的参考价值。

 在日常生活中,大家会遇到这种情况,在家里可以自动获取ip地址,上班的时候公司可能会禁网,所以会提供一个私网地址供大家使用,但是需要大家手连接网络,比较麻烦。这就需要为设备配置两套方案,设置一个网络接口来回切换,就会很方便了。

一、首先了解一下bonding的定义

        bonding是通过将同一设备的多个物理网卡绑定到一个虚拟网卡上,再对外提供连接。对于外端来说,多个物理网卡共享虚拟网卡的IP和mac地址,也就表现为一个网卡设备。通过bonding技术可以实现高可用或者负载均衡。

bonding的工作模式:

       Mode 0    轮转策略:从头到尾顺序的在每一个slave接口上面发送数据包。本模式提供负载均衡和容错的能力
       Mode 1 (active-backup)活动-备份(主备)策略:只有一个slave被激活,当且仅当活动的slave接口失败时才会激活其他slave。 为了避免交换机发生混乱此时绑定的MAC地址只有一个外部端口上可见
       Mode 3 (broadcast)广播策略:在所有的slave接口上传送所有的报文,提供容错能力

二、Bonding的配置(centos6)

1.手工添加2块网卡,并设置到同一个vmnet中(vmnet3)。用ifconfig查看两个网卡是否加入进去

[[email protected] network-scripts]#ifconfig

eth2      Link encap:Ethernet  HWaddr 00:0C:29:8F:26:2A  
           UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
           RX packets:546 errors:0 dropped:0 overruns:0 frame:0
           TX packets:13 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:164116 (160.2 KiB)  TX bytes:938 (938.0 b)

eth3      Link encap:Ethernet  HWaddr 00:0C:29:8F:26:2A  
           UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
           RX packets:552 errors:0 dropped:0 overruns:0 frame:0
           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:164476 (160.6 KiB)  TX bytes:0 (0.0 b)

lo        Link encap:Local Loopback  
           inet addr:127.0.0.1  Mask:255.0.0.0
           inet6 addr: ::1/128 Scope:Host
           UP LOOPBACK RUNNING  MTU:65536  Metric:1
           RX packets:4 errors:0 dropped:0 overruns:0 frame:0
           TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0 
           RX bytes:240 (240.0 b)  TX bytes:240 (240.0 b)

2.创建bonding设备bond0的配置文件

[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[root@centos6 network-scripts]# cp ifcfg-eth0 ifcfg-bond0
[[email protected] network-scripts]# vim ifcfg-bond0 
 DEVICE=bond0
 ONBOOT=yes
 BONDING_OPTS="miimon=100 mode=1"
 IPADDR=10.8.8.8
 NETMASK=255.255.255.0
 NM_CONTROLLED=no

      miimon 是用来进行链路监测的。如果miimon=100,那么系统每100ms 监测一次链路连接状态,如果有一条线路不通就转入另一条线路

      mode 是bonding的模式

3.配置slave的配置文件

 [[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-eth2
 [[email protected] network-scripts]# vim ifcfg-eth2 
 DEVICE=eth2
 TYPE=Ethernet
 ONBOOT=yes
 SLAVE=yes
 MASTER=bond0
 NM_CONTROLLED=no

[root@centos6 network-scripts]# cp ifcfg-eth0 ifcfg-eth3

[root@centos6 network-scripts]# vim ifcfg-eth3 
 DEVICE=eth3
 TYPE=Ethernet
 ONBOOT=yes
 SLAVE=yes
 MASTER=bond0
 NM_CONTROLLED=no

4.重启网络服务

[[email protected] network-scripts]# service network restart

5.查看bonding的状态

[[email protected] 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: eth2
 MII Status: up
 MII Polling Interval (ms): 100
 Up Delay (ms): 0
 Down Delay (ms): 0

Slave Interface: eth2
MII Status: up
 Speed: 1000 Mbps
 Duplex: full
 Link Failure Count: 0
 Permanent HW addr: 00:0c:29:8f:26:2a
 Slave queue ID: 0

Slave Interface: eth3
 MII Status: up
 Speed: 1000 Mbps
 Duplex: full
 Link Failure Count: 0
 Permanent HW addr: 00:0c:29:8f:26:20
 Slave 

5. 更改bond0工作模式

    直接修改bond0配置文件中的mode,重启网络服务即可

6.如何删除bond0(先关再删)

[[email protected] network-scripts]#ifconfig bond0 down
 [[email protected] network-scripts]#rm –f  ifcfg-bond0

  删除eth2里面的master,及slave

三、centos7的bonding配置(centos7中的补齐命令比centos6更强)

1.加2个网卡,在同一个vmnet中。

2.使用nmcli管理工具

1)创建逻辑网卡bond0

[[email protected] network-scripts]#nmcli con add type bond con-name bond0 ifname bond0 mode active-backup

Connection ‘bond0‘ (c505387c-6f85-4af5-bf50-7958d8cf39ef) successfully added.

#add 相当与自动生成配置文件

[[email protected] network-scripts]#nmcli con modify bond0 ipv4.addresses 20.20.20.20/24 ipv4.dns 8.8.8.8 ipv4.gateway 20.20.20.1 ipv4.method manual  connection.autoconnect yes;--设定bond0 ip

2)添加从属网路接口(增加slave的方案给两块新加的网卡)

[[email protected] network-scripts]# nmcli connection add con-name slave-38 ifname ens38 type ethernet master bond0 
 Connection ‘slave-38‘ (97708606-56ac-48aa-9014-c23a376c2379) successfully added.
 [root@centos7 network-scripts]# nmcli connection add con-name slave-39 ifname ens39 type ethernet master bond0 
 Connection ‘slave-39‘ (608dc1a0-9407-45be-a720-4c415b4b8488) successfully added.

3.直接激活bond0

[[email protected]  network-scripts]# nmcli connection up bond0

4.更改bond0模式

[[email protected]]# nmcli connection modify bond0 mode broadcast

5.删除同centos6

四、创建网络组Networking Teaming实现类似功能

网络组:是将多个网卡聚合在一起方法,从而实现冗错和提高吞吐量。但网络组不同于旧版中bonding技术,提供更好的性能和扩展性。

1.创建网络组team0

[[email protected] network-scripts]# nmcli connection add type team con-name team0 ifname team0 config ‘{"runner": {"name": "loadbalance"}}‘
 Connection ‘team0‘ (9a5ab96b-3cd3-412d-9532-1327c035367c) successfully added.

2.添加从属网路接口

[[email protected] network-scripts]# nmcli connection add con-name slave-38 ifname ens38 type team-slave master team0
 Connection ‘slave-38‘ (e5182e70-86a6-480f-bc14-2d2055bfab9f) successfully added.

[root@centos7 network-scripts]# nmcli connection add con-name slave-39 ifname ens39 type team-slave master team0
 Connection ‘slave-39‘ (e31b4880-0095-4322-af49-63094bbe873d) successfully added.

3.设置team0的ip

[[email protected] network-scripts]# nmcli connection modify team0 ipv4.method manual ipv4.addresses 20.20.20.20/24 connection.autoconnect yes 

4.激活team0及slave

[[email protected] network-scripts]# nmcli connection up team0
 Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/13)

[root@centos7 network-scripts]# nmcli connection up slave-38
 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/16)
 [root@centos7 network-scripts]# nmcli connection up slave-39
 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/18)

5.删除team0

[[email protected] network-scripts]# nmcli connection down team0
 Connection ‘team0‘ successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/13)
 [root@centos7 network-scripts]# nmcli connection down slave-38
 Connection ‘slave-38‘ successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/16)
 [root@centos7 network-scripts]# nmcli connection down slave-39
 Connection ‘slave-39‘ successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/19)

[root@centos7 network-scripts]# rm -f ifcfg-slave-3*
 [[email protected] network-scripts]# rm -f ifcfg-team0 

实验:使用nmcli为网卡配置2套方案,一个Dhcp一个静态获取

1.改旧方案的名称(非必须)

[[email protected] network-scripts]# nmcli connection modify ens34 connection.id home

2.将home的方案设置的自动获取ipv4地址

[[email protected]]#nmcli connection modify home ipv4.method auto

3.使方案生效

[[email protected] network-scripts]#nmcli connection up home

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

查看方案

[[email protected] network-scripts]# nmcli connection show 
NAME     UUID                                  TYPE      DEVICE 
ens33    dadb8960-c959-47f5-addb-13f8e439d72d  ethernet  ens33
home  482d5ab5-ac7d-4467-ac09-65440b174331  ethernet  ens34  
virbr0   1eee137a-f1ea-4fe8-b387-5d0561d76aca  bridge    virbr0 

4.新增方案名为office

[[email protected] network-scripts]# nmcli connection add con-name office type ethernet ifname ens34

5.为新增的方案进行配置静态地址

[[email protected] network-scripts]# nmcli connection modify office ipv4.method manual connection.autoconnect yes ipv4.addresses 8.8.8.8/24 ipv4.gateway 8.8.8.1 ipv4.dns 4.4.4.4

6.切换到新方案,使之生效

[[email protected] network-scripts]# nmcli connection up office

查看方案

[[email protected] network-scripts]# nmcli connection show 
NAME     UUID                                  TYPE      DEVICE 
ens33    dadb8960-c959-47f5-addb-13f8e439d72d  ethernet  ens33
office  482d5ab5-ac7d-4467-ac09-65440b174331  ethernet  ens34  
virbr0   1eee137a-f1ea-4fe8-b387-5d0561d76aca  bridge    virbr0 
home2ae365b6-ab9a-4882-8831-e38a362881e2  ethernet  --     

如果手工修改了配置文件,如下

[[email protected] network-scripts]# vim /etc/sysconfig/network-scripts/ifcfg-office
IPADDR=8.8.8.8
PREFIX=24
GATEWAY=8.8.8.1
DNS1=4.4.4.4

则需要使用nmcli connection reload来重读后才能够nmcli connection up

删除方案

[[email protected] network-scripts]# nmcli connection delete office

 

 

 

 

 

 



以上是关于linux虚拟网络接口---bonding配置的主要内容,如果未能解决你的问题,请参考以下文章

Linux如何配置bond

linux如何配置bond?

linux网络配置之bond-team-bridge

Linux中使用nmcli配置网络

网络接口配置bonding

网络管理和nmcli命令的使用——网络接口配置-bonding实验步骤