Linux虚拟网络接口-Bonding 配置

一、bonding 的定义
     bonding是通过将同一设备的多个物理网卡绑定到一个虚拟网卡上,再对外提供连接。对于外端来说,多个物理网卡共享虚拟网卡的IP和mac地址,也就表现为一个网卡设备。通过bonding技术可以实现高可用或者负载均衡。
     bonding有7种工作模式:
     Mode 0 (balance- rr)轮转(Round- robin)策略:从头到尾顺序的在每一个slave接口上面发送数据包。本模式提供负载均衡和容错的能力
     Mode 1 (active-backup)活动-备份(主备)策略:只有一个slave被激活,当且仅当活动的slave接口失败时才会激活其他slave。 为了避免交换机发生混乱此时绑定的MAC地址只有一个外部端口上可见
     Mode 3 (broadcast)广播策略:在所有的slave接口上传送所有的报文,提供容错能力
     Mode 2 (balance-xor)XOR policy(平衡策略); Mode 4 Dynamic link aggregation(IEEE802.3ad 动态链接聚合);Mode 5 (balance-tlb)Adaptive transmit load balancing(适配器传输负载均衡);Mode 6 (balance-alb)Adaptive load balancing(适配器适应性负载均衡)具体请参照内核源码包文件:/usr/share/doc/kernel-doc-version(ex 3.10.0)/Documentation/networking/bonding.txt (需要安装kernel-doc包)

二、Centos6 bonding的配置

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

#vi /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=band0
BONDING_OPTS="mode=1 miimon=100"                                    <-- 工作模式为1;参考1)
BOOTPROTO=none
IPADDR=192.168.192.100                                              <-- 指定bond0的IP
PREFIX=24
GATEWAY=172.17.0.1

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

2.修改物理网卡配置文件

#vi ifcfg-eth0
DEVICE=eth0
MASTER=bond0
SLAVE=yes
#vi ifcfg-eth1
DEVICE=eth1
MASTER=bond0
SLAVE=yes

3.重启网络服务

#service network restart                                            <-- 注意在centos6中要先关闭NetworkManager服务

4.查看bond0状态

[root@centos6 ~]#cat /proc/net/bonding/bond0 
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
Bonding Mode: fault-tolerance (active-backup)                        <-- mode
Primary Slave: None
Currently Active Slave: eth0                                         <-- 当前工作的物理网卡
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:e2:f9:b2
Slave queue ID: 0

Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:50:56:28:0b:0c
Slave queue ID: 0
[root@centos6 ~]#ifconfig 
bond0     Link encap:Ethernet  HWaddr 00:0C:29:E2:F9:B2                                  <--mac地址一致
          inet addr:192.168.196.100  Bcast:192.168.196.255  Mask:255.255.255.0           <--IP唯一
          inet6 addr: fe80::20c:29ff:fee2:f9b2/64 Scope:Link
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:111 errors:0 dropped:0 overruns:0 frame:0
          TX packets:85 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:12290 (12.0 KiB)  TX bytes:12131 (11.8 KiB)

eth0      Link encap:Ethernet  HWaddr 00:0C:29:E2:F9:B2                              
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:1375 errors:0 dropped:0 overruns:0 frame:0
          TX packets:819 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:139581 (136.3 KiB)  TX bytes:142252 (138.9 KiB)

eth1      Link encap:Ethernet  HWaddr 00:0C:29:E2:F9:B2  
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:353 errors:0 dropped:0 overruns:0 frame:0
          TX packets:70 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:28577 (27.9 KiB)  TX bytes:8274 (8.0 KiB)
[root@centos6 ~]#ping 192.162.196.100                                                 <--另一个主机可以ping通
PING 192.168.196.100 (192.168.196.100) 56(84) bytes of data.
64 bytes from 192.168.196.100: icmp_seq=1 ttl=64 time=0.842 ms
64 bytes from 192.168.196.100: icmp_seq=2 ttl=64 time=1.82 ms
64 bytes from 192.168.196.100: icmp_seq=3 ttl=64 time=0.423 ms

5. 更改bond0工作模式

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

6.如何删除bond0

[root@centos6 ~]#ifconfig bond0 down
[root@centos6 ~]#rm -f ifcfg-bond0 
删除第2步物理网卡配置文件中的MASTER&SLAVE条目
[root@centos6 ~]#rmmod bonding

三、Centos7 bonding的配置

1) 使用nmcli管理工具

1.创建逻辑网卡bond0

[root@centos7 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 相当与自动生成配置文件
[root@centos7 network-scripts]#nmcli con modify bond0 ipv4.addresses 192.168.196.100/24 ipv4.method manual  <--设定bond0 ip

2. 添加从属网络接口

[root@centos7 network-scripts]#nmcli con ad type bond-slave ifname eth0 master bond0             <--绑定物理网卡
Connection 'bond-slave-eth0' (92332d4d-5d26-430f-843a-2403326e1861) successfully added.
[root@centos7 network-scripts]#nmcli con ad type bond-slave ifname eth1 master bond0             <--绑定物理网卡
Connection 'bond-slave-eth1' (5f66a399-098d-4fde-9828-5f4461ea2196) successfully added.

3. 关联物理网卡

[root@centos7 network-scripts]#nmcli connection show                                             <--查看当前链接状态 
NAME             UUID                                  TYPE            DEVICE 
bond0            c505387c-6f85-4af5-bf50-7958d8cf39ef  bond            bond0  
eth0             c01fcf0b-866d-3df4-84be-67e72dc6821f  802-3-ethernet  eth0   
eth1             fcaec399-151c-3b67-8cd1-498a17e3fab9  802-3-ethernet  eth1   
virbr0           761fc9c3-2c1a-43fc-9181-978bfe2e9cb1  bridge          virbr0 
bond-slave-eth0  92332d4d-5d26-430f-843a-2403326e1861  802-3-ethernet  --                         <--配置要关联物理网卡
bond-slave-eth1  5f66a399-098d-4fde-9828-5f4461ea2196  802-3-ethernet  --                         <--配置要关联物理网卡
[root@centos7 network-scripts]#nmcli con up bond-slave-eth0                                               
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
[root@centos7 network-scripts]#nmcli con up bond-slave-eth1                                                 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8)
[root@centos7 network-scripts]#nmcli connection show
NAME             UUID                                  TYPE            DEVICE 
bond-slave-eth0  92332d4d-5d26-430f-843a-2403326e1861  802-3-ethernet  eth0   
bond-slave-eth1  5f66a399-098d-4fde-9828-5f4461ea2196  802-3-ethernet  eth1   
bond0            c505387c-6f85-4af5-bf50-7958d8cf39ef  bond            bond0  
virbr0           761fc9c3-2c1a-43fc-9181-978bfe2e9cb1  bridge          virbr0 
eth0             c01fcf0b-866d-3df4-84be-67e72dc6821f  802-3-ethernet  --     
eth1             fcaec399-151c-3b67-8cd1-498a17e3fab9  802-3-ethernet  --   
[root@centos7 network-scripts]#nmcli con up bond0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/9)

4.查看bond0状态 参考上文Centos6中步骤4

5.更改bond0模式

[root@centos7 network-scripts]#nmcli con modify bond0 mode broadcast
[root@centos7 network-scripts]#nmcli con up bond0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/12)

6.删除bond0

方法1

[root@centos7 network-scripts]#nmcli con down bond0                            <--相当于删除配置文件并生效
Connection 'bond0' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/12)   
[root@centos7 network-scripts]#nmcli con del bond-slave-eth0
Connection 'bond-slave-eth0' (92332d4d-5d26-430f-843a-2403326e1861) successfully deleted.
[root@centos7 network-scripts]#nmcli con del bond-slave-eth1
Connection 'bond-slave-eth1' (5f66a399-098d-4fde-9828-5f4461ea2196) successfully deleted.

方法2

[root@centos7 network-scripts]rm -f ifcfg-bond*                                <--直接删除配置文件
[root@centos7 network-scripts]nmcli con reload 

2)创建网络组Networking Teaming实现类似功能

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

同样利用nmcli工具,其步骤与创建bonding设备类似
1.创建网络组team0

[root@centos7 network-scripts]#nmcli con add type team con-name team0 ifname team0 config '{"runner":{"name":"activebackup"}}' ipv4.method manual ipv4.addresses 192.168.196.100/24
Connection 'team0' (83620f8e-aad2-4de9-b793-2f11faf9ca7a) successfully added.

2. 添加从属网络接口

[root@centos7 network-scripts]#nmcli con add type team-slave ifname eth0 master team0
Connection 'team-slave-eth0' (370f85d6-a68c-4d5c-b994-202effada86a) successfully added.
[root@centos7 network-scripts]#nmcli con add type team-slave ifname eth1 master team0

3. 关联物理网卡

[root@centos7 network-scripts]#nmcli con up team-slave-eth1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/19)
[root@centos7 network-scripts]#nmcli con up team-slave-eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/18)

4.查看team0状态

[root@centos7 network-scripts]#teamdctl team0 stat
setup:
  runner: activebackup
ports:
  eth0
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
  eth1
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
        down count: 0
runner:
  active port: eth0

5.更改team0模式

[root@centos7 network-scripts]#nmcli con mod team0 config '{"runner":{"name":"broadcast"}}'
[root@centos7 network-scripts]#nmcli con up team0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/35)

6.删除team0 –> 参考上文Centos7中第6步

网络组还有几个特点:

启动网络组接口不会自动启动网络组中的port接口
启动网络组接口中的port接口总会自动启动网络组接口
禁用网络组接口会自动禁用网络组中的port接口
没有port接口的网络组接口可以启动静态IP连接                                –>在没有关联网卡的情况下
启用DHCP连接时,没有port接口的网络组会等待port接口的加入

原创文章,作者:ffu,如若转载,请注明出处:http://www.178linux.com/79092

(0)
上一篇 2017-07-02 15:03
下一篇 2017-07-02 16:40

相关推荐

  • N25 第五周作业

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; 3、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行; 4、添加用户bash, testbash, basher, nol…

    Linux干货 2017-01-08
  • Linux用户和组管理常用命令

    Linux用户和组管理常用命令 1、useradd:创建用户   useradd [options] LOGIN     -u UID: [UID_MIN, UID_MAX]指定uid,(默认500|1000开头)定义在/etc/login.defs     -o 配合-u 选项, 不检查…

    Linux干货 2017-04-04
  • MySQL高可用架构之Galera Cluster

    MySQL高可用架构之Galera Cluster 1、实验准备及拓扑 至少需要三个节点 node1 192.168.150.137 node2 192.168.150.138 node3 192.168.150.139 mariadb版本为mariadb的支持galera cluster的分支版本 MariaDB-Galera-server-5.5.46 …

    Linux干货 2017-03-31
  • N25_第一周

    计算机组成 计算机通常由硬件和软件组成1.硬件 CPU CPU通常由控制器和运算器组成。控制器:是整个计算机的中枢神经,其功能是对程序规定的控制信息进行解释,根据其要求进行控制,调度程序、数据、地址,协调计算机各部分工作及内存与外设的访问等。运算器:是对数据进行各种算术运算和逻辑运算,即对数据进行加工处理。 存储 存储器的功能是存储程序、…

    Linux干货 2016-12-03
  • 学习宣言

    现在,青春是用来奋斗的;将来,青春是用来回忆的。   人生之路,有坦途也有陡坡,有平川也有险滩,有直道也有弯路。青年面临的选择很多,关键是要以正确的世界观、人生观、价值观来指导自己的选择。无数人生成功的事实表明,青年时代,选择吃苦也就选择了收获,选择奉献也就选择了高尚。青年时期多经历一点摔打、挫折、考验,有利于走好一生的路。要历练宠辱不惊…

    Linux干货 2016-12-29
  • grub启动

    grub: GRandUnified Bootloader(统一的引导模式分为3个阶段) grub 0.97: grub legacy grub 2.x: grub2 grub legacy: stage1: mbr stage1_5: mbr之后的扇区,让stage1中的bootloader能识别stage2所在的分区上的文件系统 stage2:磁盘分区(…

    Linux干货 2017-05-15