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

相关推荐

  • exit和退出码

    用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行。   常用参数 格式:exitn 退出。设置退出码为n。(Causethe shell to exit with a status of n.)   格式:exit 退出。退出码不变,即为最后一个命令的退出码。(Ifn is omitted, the e…

    Linux干货 2016-01-14
  • diy自己的linux

    准备一台liunx的机器,我用的是liunx虚拟机,Centos6.8的镜像。 单独加载一块硬盘,我加载了一块20G的硬盘,然后用fdisk /dev/sdb进行分区,sdb1用来作为将来系统的/boot分区,所有100M就可以了,sdb2用来作为将来系统的/分区,我给了10G。然后将两个分区格式化为ext4系统。 mkdir /mnt/boot mkdir…

    Linux干货 2016-09-13
  • 马哥教育网络班21期+第1周课程练习

    1.描述计算机的组成及其功能。   计算机主要由运算器,控制器,存储器,输入设备,输出设备组成   运算器用来做计算,用来做二进制运算(加法运算)和逻辑运算   控制器用来控制计算机各部件之间的协调,例如运算器想做运算从哪里读入加数和被加数,寄存在哪里   存储器分为内存储器和外存储器,用来存放数据 内存储器用于存放计…

    Linux干货 2016-07-12
  • N26 – 第三周作业

    1、列出当前系统上所有已登录的用户的用户名,同一用户登录多次只显示一次     ~]# who -u |cut -d ' ' -f1|uniq -u 2、最后登录到当前系统的用户的相关信息     ~]#id $(last | h…

    Linux干货 2017-01-03
  • 正则表达式 小结

    听老师和学姐都说,正则表达式很重要,所以这次我总结一下,同时加强一下记忆。 目前我们学的正则表达式有:字符匹配;匹配字数;位置锚定。 :. 匹配任意单个字符 [] 匹配指定范围内的任意单个字符 [^] 匹配指定范围外的任意单个字符 [:alnum:] 或 [0-9a-zA-Z] [:alpha:] 或 [a-zA-Z]…

    2017-07-31
  • 三次握手和四次挥手

    今天来聊一下事实标准协议TCP/IP中传输层里TCP协议中,主机与服务器建立连接时的三次握手,和断开连接时的四次挥手。 本博文分两部分介绍,    一:状态详解    二:三次握手和四次挥手状态介绍 这里总共涉及到十种状态,其实总共有十一种状态,接下来分别介绍一下它们; 一:状态详解 CLOSED:关闭—&…

    2017-09-01