zoukankan      html  css  js  c++  java
  • linux网络基础-网卡bonding技术

    1、bonding
    bonding(绑定)是一种linux系统下的网卡绑定技术,可以把服务器上n个物理网卡在系统内部抽象(绑定)成一个逻辑上的网卡,实现本地网卡的冗余,带宽扩容和负载均衡。在应用部署中是一种常用的技术。
    bonding技术是linux系统内核层面实现的,它是一个内核模块(驱动)。使用它需要系统有这个模块。我们可以用 modinfo bonding 命令查看下这个模块的信息, 一般来说都支持。

    2、bonding的七种模式
    bonding技术提供了七种工作模式,在使用的时候需要指定一种,每种有各自的优缺点。
    balance-rr (mode=0) 默认, 有高可用 (容错) 和负载均衡的功能, 需要交换机的配置,每块网卡轮询发包 (流量分发比较均衡).
    active-backup (mode=1) 主备模式,只有高可用 (容错) 功能, 不需要交换机配置, 这种模式只有一块网卡工作, 对外只有一个mac地址。缺点是端口利用率比较低
    balance-xor (mode=2) 不常用
    broadcast (mode=3) 不常用
    802.3ad (mode=4) IEEE 802.3ad 动态链路聚合,需要交换机配置,没用过
    balance-tlb (mode=5) 不常用
    balance-alb (mode=6) 有高可用 ( 容错 )和负载均衡的功能,不需要交换机配置 (流量分发到每个接口不是特别均衡
    5和6不需要交换机端的设置,网卡能自动聚合。4需要支持802.3ad。0,2和3理论上需要静态聚合方式
    但实测中0可以通过mac地址欺骗的方式在交换机不设置的情况下不太均衡地进行接收。
    常用的模式有三种
    mode=0:平衡负载模式,有自动备援,但需要”Switch”支援及设定。
    mode=1:自动备援模式,其中一条线若断线,其他线路将会自动备援。
    mode=6:平衡负载模式,有自动备援,不必”Switch”支援及设定。

    需要说明的是如果想做成mode 0的负载均衡,仅仅设置这里options 参数为 miimon=100 mode=0是不够的,
    与网卡相连的交换机必须做特殊配置(这两个端口应该采取聚合方式),因为做bonding的这两块网卡是使用同一个MAC地址.
    mode6模式下无需配置交换机,因为做bonding的这两块网卡是使用不同的MAC地址

    3、在centos7上配置bonding
    环境:
    系统: Centos7
    网卡: em1、em2
    bond0:172.16.0.183
    负载模式: mode6(adaptive load balancing)
    服务器上两张物理网卡em1和em2, 通过绑定成一个逻辑网卡bond0,bonding模式选择mode6
    注: ip地址配置在bond0上, 物理网卡不需要配置ip地址

    1)关闭和停止NetworkManager服务

    systemctl stop NetworkManager.service     # 停止NetworkManager服务
    systemctl disable NetworkManager.service  # 禁止开机启动NetworkManager服务

    ps: 一定要关闭,不关会对做bonding有干扰

    2)加载bonding模块

    modprobe --first-time bonding
    没有提示说明加载成功, 如果出现modprobe: ERROR: could not insert 'bonding': Module already in kernel说明你已经加载了这个模块, 就不用管了
    可以使用lsmod | grep bonding查看模块是否被加载
    lsmod | grep bonding
    bonding               136705  0 

    3)创建基于bond0接口的配置文件

    vim /etc/sysconfig/network-scripts/ifcfg-bond0
    DEVICE=bond0
    TYPE=Bond
    IPADDR=172.16.0.183
    NETMASK=255.255.255.0
    GATEWAY=172.16.0.1
    DNS1=114.114.114.114
    USERCTL=no
    BOOTPROTO=none
    ONBOOT=yes
    BONDING_MASTER=yes
    BONDING_OPTS="mode=6 miimon=100"
    上面的BONDING_OPTS="mode=6 miimon=100" 表示这里配置的工作模式是mode6(adaptive load balancing), 
    miimon表示监视网络链接的频度 (毫秒), 我们设置的是100毫秒, 根据你的需求也可以指定mode成其它的负载模式。

    4)修改em1接口的配置文件

    vim /etc/sysconfig/network-scripts/ifcfg-em1
    DEVICE=em1
    USERCTL=no
    ONBOOT=yes
    MASTER=bond0                  # 需要和上面的ifcfg-bond0配置文件中的DEVICE的值对应
    SLAVE=yes
    BOOTPROTO=none
    5)修改em2接口的配置文件
    vim /etc/sysconfig/network-scripts/ifcfg-em2
    DEVICE=em2
    USERCTL=no
    ONBOOT=yes
    MASTER=bond0                 # 需要和上的ifcfg-bond0配置文件中的DEVICE的值对应
    SLAVE=yes
    BOOTPROTO=none

    5)重启网络服务

    systemctl restart network
    查看bond0的接口状态信息 ( 如果报错说明没做成功,很有可能是bond0接口没起来)

    # cat /proc/net/bonding/bond0
    
    Bonding Mode: adaptive load balancing   // 绑定模式: 当前是ald模式(mode 6), 也就是高可用和负载均衡模式
    Primary Slave: None
    Currently Active Slave: em1
    MII Status: up                           // 接口状态: up(MII是Media Independent Interface简称, 接口的意思)
    MII Polling Interval (ms): 100           // 接口轮询的时间隔(这里是100ms)
    Up Delay (ms): 0
    Down Delay (ms): 0
    
    Slave Interface: em1                     // 备接口: em0
    MII Status: up                           // 接口状态: up(MII是Media Independent Interface简称, 接口的意思)
    Speed: 1000 Mbps                         // 端口的速率是1000 Mpbs
    Duplex: full                             // 全双工
    Link Failure Count: 0                    // 链接失败次数: 0 
    Permanent HW addr: 84:2b:2b:6a:76:d4      // 永久的MAC地址
    Slave queue ID: 0
    
    Slave Interface: em1                     // 备接口: em1
    MII Status: up                           // 接口状态: up(MII是Media Independent Interface简称, 接口的意思)
    Speed: 1000 Mbps
    Duplex: full                             // 全双工
    Link Failure Count: 0                    // 链接失败次数: 0
    Permanent HW addr: 84:2b:2b:6a:76:d5     // 永久的MAC地址
    Slave queue ID: 0

    通过ifconfig命令查看下网络的接口信息

    # ifconfig
    
    bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
            inet 172.16.0.183  netmask 255.255.255.0  broadcast 172.16.0.255
            inet6 fe80::862b:2bff:fe6a:76d4  prefixlen 64  scopeid 0x20<link>
            ether 84:2b:2b:6a:76:d4  txqueuelen 0  (Ethernet)
            RX packets 11183  bytes 1050708 (1.0 MiB)
            RX errors 0  dropped 5152  overruns 0  frame 0
            TX packets 5329  bytes 452979 (442.3 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    em1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
            ether 84:2b:2b:6a:76:d4  txqueuelen 1000  (Ethernet)
            RX packets 3505  bytes 335210 (327.3 KiB)
            RX errors 0  dropped 1  overruns 0  frame 0
            TX packets 2852  bytes 259910 (253.8 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    em2: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
            ether 84:2b:2b:6a:76:d5  txqueuelen 1000  (Ethernet)
            RX packets 5356  bytes 495583 (483.9 KiB)
            RX errors 0  dropped 4390  overruns 0  frame 0
            TX packets 1546  bytes 110385 (107.7 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 0  (Local Loopback)
            RX packets 17  bytes 2196 (2.1 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 17  bytes 2196 (2.1 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    mode6这种负载模式除了故障恢复的时候有丢包之外,其它都挺好的,如果能够忽略这点的话可以用这种模式;
    而mode1故障的切换和恢复都很快,基本没丢包和延时。但端口利用率比较低,因为这种主备的模式只有一张网卡在工作.

    4、Centos6配置bondin
    系统: Centos6
    网卡: em1、em2
    bond0:172.16.0.183
    负载模式: mode1(adaptive load balancing) # 这里的负载模式为1,也就是主备模式.
    1)关闭和停止NetworkManager服务
    service NetworkManager stop
    chkconfig NetworkManager off
    ps: 如果有装的话关闭它,如果报错说明没有装这个,那就不用管
    2)加载bonding模块
    modprobe --first-time bonding

    3)创建基于bond0接口的配置文件

    vim /etc/sysconfig/network-scripts/ifcfg-bond0
    DEVICE=bond0
    TYPE=Bond
    BOOTPROTO=none
    ONBOOT=yes
    IPADDR=172.16.0.183
    NETMASK=255.255.255.0
    GATEWAY=172.16.0.1
    DNS1=114.114.114.114
    USERCTL=no
    BONDING_OPTS="mode=1 miimon=100"

    4)加载bond0接口到内核
    vi /etc/modprobe.d/bonding.conf
    alias bond0 bonding
    options bonding mode=0 miimon=200---这个在bond的配置文件中写也是可以的
    如果我们要设置多个bond口,比如物理网口eth0和eth1组成bond0,eth2和eth3组成bond1,
    那么网口配置文件的设置方法相同,/etc/modprobe.d/bonding.conf的设定有两种方法
    第一种,你可以看到,这种方式的话,多个bond口的模式就只能设成相同的了:
    alias bond0 bonding
    alias bond1 bonding
    options bonding max_bonds=2 miimon=200 mode=1

    第二种,这种方式,不同的bond口的mode可以设成不一样:
    alias bond0 bonding
    options bond0 miimon=100 mode=1
    install bond1 /sbin/modprobe bonding -o bond1 miimon=200 mode=0

    5)编辑em1、em2的接口文件

    vim /etc/sysconfig/network-scripts/ifcfg-em1
    DEVICE=em1
    MASTER=bond0
    SLAVE=yes
    USERCTL=no
    ONBOOT=yes
    BOOTPROTO=none
    vim /etc/sysconfig/network-scripts/ifcfg-em2
    DEVICE=em2
    MASTER=bond0
    SLAVE=yes
    USERCTL=no
    ONBOOT=yes
    BOOTPROTO=none

    6)加载模块、重启网络与测试
    modprobe bonding
    service network restart
    查看bondo接口的状态

    cat /proc/net/bonding/bond0

    Bonding Mode: fault-tolerance (active-backup)  # bond0接口当前的负载模式是主备模式
    Primary Slave: None
    Currently Active Slave: em2
    MII Status: up
    MII Polling Interval (ms): 100
    Up Delay (ms): 0
    Down Delay (ms): 0
    
    Slave Interface: em1
    MII Status: up
    Speed: 1000 Mbps
    Duplex: full
    Link Failure Count: 2
    Permanent HW addr: 84:2b:2b:6a:76:d4
    Slave queue ID: 0
    
    Slave Interface: em2
    MII Status: up
    Speed: 1000 Mbps
    Duplex: full
    Link Failure Count: 0
    Permanent HW addr: 84:2b:2b:6a:76:d5
    Slave queue ID: 0

    通过ifconfig命令查看下接口的状态,你会发现mode=1模式下所有的mac地址都是一致的,说明对外逻辑就是一个mac地址

    ifconfig 
    bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
            inet6 fe80::862b:2bff:fe6a:76d4  prefixlen 64  scopeid 0x20<link>
            ether 84:2b:2b:6a:76:d4  txqueuelen 0  (Ethernet)
            RX packets 147436  bytes 14519215 (13.8 MiB)
            RX errors 0  dropped 70285  overruns 0  frame 0
            TX packets 10344  bytes 970333 (947.5 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    em1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
            ether 84:2b:2b:6a:76:d4  txqueuelen 1000  (Ethernet)
            RX packets 63702  bytes 6302768 (6.0 MiB)
            RX errors 0  dropped 64285  overruns 0  frame 0
            TX packets 344  bytes 35116 (34.2 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    em2: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
            ether 84:2b:2b:6a:76:d4  txqueuelen 1000  (Ethernet)
            RX packets 65658  bytes 6508173 (6.2 MiB)
            RX errors 0  dropped 6001  overruns 0  frame 0
            TX packets 1708  bytes 187627 (183.2 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 0  (Local Loopback)
            RX packets 31  bytes 3126 (3.0 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 31  bytes 3126 (3.0 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    备注:在CentOS系统上面默认没有ifconfig命令,可以通过yum安装
    yum install net-tools -y

  • 相关阅读:
    Python 函数与函数式编程
    Python 字符编码与转码
    Python 读写txt文件操作
    两阶段事务总结
    MPPDB集群高可用设计
    MPPDB中的各个组件
    IntelliJ IDEA2016学习小结
    mysql免安装版配置
    理想的智能机
    java对象的大小
  • 原文地址:https://www.cnblogs.com/potato-chip/p/9125840.html
Copyright © 2011-2022 走看看