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)
ffuffu
上一篇 2017-07-02
下一篇 2017-07-02

相关推荐

  • Shell脚本编程之循环(for、while、until)

    Shell脚本编程之循环(for、while、until)   一、循环语句的对比: for语句 while语句 until语句 执行机制: 依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中的元素耗尽,循环结束 CONDITION:循环控制条件;进入循环之前,先做一次判断;每一次循环之后会再次做判断;条件为“true”…

    Linux干货 2016-08-18
  • Linux基础 & bash相关

    Q1:Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 文件管理类命令: cd, ls, touch, cp, mv, rm, cat, tac, more, less, tail, head; 详细介绍以上命令: cd: 在Linux文件系统上,可以使用切换目录命令cd将shell会话切换到另一个目录。 命令格式: ~]#&nbsp…

    Linux干货 2016-11-06
  • 【yum安装程序】Centos7.4使用yum光盘安装httpd

    举例:Centos7.4使用yum光盘安装httpd

    Linux干货 2018-03-17
  • 文本处理工具三剑客之awk

    文本处理工具:grep,sed,awk awk:报告生成器,格式化文本输出 AWK: Aho ,Weinberger,Kernighan gawk:GNU awk gawk – pattren  scanning  and  processing  language 基本语法:gawk  [opt…

    Linux干货 2016-09-22
  • 关于 进程和性能监控

            Linux系统状态的查看及管理工具:     pstree, ps, pidof, pgrep, top, htop, glance,pmap, vmstat, dstat, kill, pkill, job, bg, fg, nohup pstree命…

    系统运维 2016-09-11
  • 网络26期 第五周作业

    1. 显示当前系统上root、fedora或user1用户的默认shell egrep -o “^(root|fedora|user1)\>.*[^:]+$” /etc/passwd | cut -d: -f1,7 2. 找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello(…

    2017-03-13