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 15:03
下一篇 2017-07-02 16:40

相关推荐

  • 进程的简单概念

    什么是进程?进程是载入内存的一个指令集合。 PID代表进程号, centos7之前的第一个进程都为inet。centos7的第一个位systemd 进程优先级分为系统优先级和实时优先级,系统优先级0到139表示,值越大优先级越低。centos6用0到98表示值越大优先级越低 实时优先级用99到0表示,值越大优先级越高。 进程类型:守护进程和前台进程。(俩者可…

    Linux干货 2017-05-08
  • Linux运维学习历程-第四天-Linux的使用帮助和快捷键

    本章内容     命令存放位置     命令帮助      help     man帮助     其它帮助        命令历史     快捷键 一、命令存放位置   &nbsp…

    Linux干货 2016-08-03
  • RPM软件包管理器及前端工具yum

    目前在linux系统之上软件安装方式最常见的有两种,分别是: dpkg:这个机制最早是由Debian Linux社区所开发出来的,透过dpkg的机制,Debian提供的软件就能够简单的安装起来,同时还能提供安装后的软件资讯,实在非常不错。只要是衍生于Debian的其他linux发行版,大多使用dpkg这个机制来管理软件的,包括B2D、Ubuntu等等。 RP…

    Linux干货 2016-08-20
  • HAProxy

    HAProxy简介 HAProxy是免费、极速且可靠的用于为TCP和基于HTTP应用程序提供高可用、负载均衡和代理服务的解决方案,尤其适用于高负载且需要持久连接或7层处理机制的web站点。HAProxy还可以将后端的服务器与网络隔离,起到保护后端服务器的作用。HAProxy的负载均衡能力虽不如LVS,但也是相当不错,而且由于其工作在7层,可以对http请求报…

    2017-05-17
  • 流编辑器Sed(Stream Edit)详解

    流编辑器Sed(Stream Edit)详解       Grep 、sed 和awk并称为文本三剑客。使用Grep就可以很好的实现文本查找的功能,而且简单有效。然而其却无法直接对其进行编辑,sed的出现便解决了这一问题。 Sed被称为流编辑器,它是一种新型的非交互式的文本编辑器,它逐行处理文件或输入,并将结…

    Linux干货 2016-08-22
  • Linux各文件颜色

    Linux系统中默认将不同的文件类型以不同的颜色加以区分: 1、普通文件   白色 2、目录文件  蓝色 3、链接文件  青色 4、套接字文件  粉红色 5、可执行文件  绿色 6、管道文件  7、块文件   黄色 8、压缩文件  红色 默认文件显示颜色可以在…

    Linux干货 2016-10-19