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

相关推荐

  • CentOS6系统启动流程分析

    Linux系统组成        从动态视角看:由内核+根文件系统组成        从静态视角看:由磁盘分区及相关文件组成 内核设计流派        单内核:所有内核功能集中于同一程序;   &n…

    Linux干货 2016-09-09
  • keepalived——高可用集群

    HA Cluster 集群类型:LB、HA、HP 系统可用性的公式:A=MTBF/(MTBF+MTTR) (0,1) 几个9: 99%, …, 99.999%     建议使用3个9的系统可用性 如何降低MTTR:冗余(redundant) active/passive active –>…

    Linux干货 2016-11-01
  • 文件系统自动挂载

    什么是文件系统自动挂载?          利用mount在shell终端进行挂载的时,开机自动启动文件文件系统不会自动挂载,我们需要开机的时候自动挂载文件系统,称为文件系统自动挂载 实现Linux自动挂载文件:/etc/fstab Linux中挂载注意事项:  &nb…

    Linux干货 2016-08-29
  • linux进程管理

    进程概念 内核的功用:进程管理、文件系统、网络功能、内存管理、驱动程序、 安全功能等 Process:  运行中的程序的一个 副本,是被 载入内存的一个指令集合     进程ID (Process ID,PID)号码被用来标记各个进程  &…

    Linux干货 2016-09-12
  • 20171123 Shell编程

    Shell编程的基本知识点

    2017-11-23
  • 初学Linux之 vim 文本编辑器

    vim 的模式切换;命令模式相关操作,拓展命令模式的相关操作;可视化和多窗口;定制vim的工作特性;vim帮助

    2017-12-31