22
Linux Bonding [email protected] 2016-05

Linux bonding

  • Upload
    hubugui

  • View
    121

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Linux bonding

Linux [email protected]

2016-05

Page 2: Linux bonding

目录•介绍•工作模式

– mode=5 balance-tlb– balance-tlb's transmit policy– mode=6 balance-alb

•测试balance-alb-输出流量– 准备– 例:笔记本-含WiFi网卡– 例:服务器-多以太网卡– 结论– 局限

• Bonding不足 2

Page 3: Linux bonding

介绍• 背景

– 利用多网卡提升传输速度和容错– Sun和Cisco早已存在,Linux 2.4.x内核引入

• 原理– 使网卡工作于混杂模式– 虚拟多网卡到1个IP地址,利用"ARP欺骗"实现单IP地址多、MAC地址– 提供链路监测,即主机到交换机是否接通。如果仅交换机对外链路down掉了,bonding会认为没有故障

• 常用模式– 多网卡负载均衡、容错(故障切换)

• 对于同一连接从不同的接口发出的包,中途传输过程中再经过不同的链接,在客户端很有可能会出现数据包无序到达的问题,而无序到达的数据包需要重新要求被发送,这样网络的吞吐量就会下降。

• 1次TCP send调用,对应2个IP包及2个以太网包,1个网卡送出还是2个?• 同一个TCP连接的流量是否经过不同网卡,待验证(iftop或tcpdump)

– 主备模式,仅1块网卡工作3

Page 4: Linux bonding

工作模式• mode=0 (balance-rr)

– 负载均衡+容错– 需交換機支持

• mode=1 (active-backup)• mode=2 (balance-xor)

– 需交換機支持• mode=3 (broadcast)• mode=4 (balance-802.3ad)

– 需交換機支持• mode=5 (balance-tlb)

– 容错+负载均衡– 输入流量固定在某slave,输出流量负载均衡– 要求驱动支持ethtool命令来获取网卡速率等信息

• mode=6 (balance-alb)– 容错+负载均衡(3张或以上网卡才能激活)– tlb的增强版,支持输入流量负载均衡– 除ethtool外,还要求驱动支持修改MAC地址 4

Page 5: Linux bonding

• Adaptive transmit load balancing: channel bonding that does not require any special switch support. The outgoing traffic is distributed according to the current load (computed relative to the speed) on each slave. Incoming traffic is received by the current slave. If the receiving slave fails, another slave takes over the MAC address of the failed receiving slave.

mode=5 (balance-tlb)

• Bonding响应对方ARP寻址广播时始终返回MAC A地址,使得输入流量一直在MAC A• 輸出流量分配到2块网卡,可能有的交換機(EMC)不支持同一源IP地址,不同源MAC地址 5

Page 6: Linux bonding

balance-tlb's transmit policy

• 最优网卡=MIN(网卡每秒速度 - 每秒发送bit数量(最近10秒平均))

Linux Source

6

Page 7: Linux bonding

• Adaptive load balancing: includes balance-tlb plus receive load balancing (rlb) for IPV4 traffic, and does not require any special switch support. The receive load balancing is achieved by ARP negotiation. The bonding driver intercepts the ARP Replies sent by the local system on their way out and overwrites the source hardware address with the unique hardware address of one of the slaves in the bond such that different peers use different hardware addresses for the server.

mode=6 (balance-alb)

1. Linux kernel module used for bonding replies with the first MAC address for some ARP requests, and the second MAC address for other ARP requests.

2. One additional item to note – if most or all of the inbound traffic to a server is coming from a single device on the subnet (the default gateway for instance).the receive load balancing won’t be overly effective. All traffic from that device will be sent over the same link to the server.

3. "3层交换机或路由器的ARP缓存可能会直接发包,从而Bonding没有"欺骗ARP回复"的机会,导致ARP缓存过期时间内,输入流量始终在一张网卡上" 7

Page 8: Linux bonding

测试balance-alb-输出流量

8

Page 9: Linux bonding

• Ubuntu Bonding配置, 12.04/14.04• UbuntuBonding• Linux Bonding• 提供Web视频服务

• 网卡流量监视工具• sudo apt-get install bmon• sudo apt-get install iftop

• HTTP下载工具• sudo apt-get install wget axel

准备

9

Page 10: Linux bonding

• 服务器/Ubuntu14.04• 服务器192.168.10.109上提供HTTP访问MP4• 服务器上安装bmon查看网卡带宽占用• 各个设备接入路由器• 服务器Bonding工作模式是mode=6(balance-alb)

• Lenovo Y430P/Ubuntu 14.04/1张有线网卡,1张WIFI网卡,失败鸟...• 没启动Bonding时无线网卡可以连接无线路由器,启动Bonding后不行,sudo ifup wlan0或

dhclient wlan0始终无法获取IP地址,同时/var/log/syslog有一些wl_错误.和驱动有关• sudo ethtool wlan0,仅返回"No data available"• sudo ethtool eth0,返回网卡速率等信息

• Lenovo X201i/Ubuntu 14.04/1张有线网卡,1张WIFI网卡,失败鸟...• 无线网卡(6Mbps)可连接到无线路由器,配置bond-mode=6无效,改为balance-alb后

/proc/net/bonding/bond0中显示工作模式在balance-rr• 虚拟网卡Bond0的IP地址192.168.10.109,eth0地址192.168.10.103,wlan0地址

192.168.10.102• 路由器管理中可看到有2台设备进入,1个客户端地址,2个服务器地址的MAC一样• 启动一台客户端播放HTTP流MP4同时再执行wget,实际连接到eth0(1000Mbps),带宽约

11MBps,ifdown掉有线后自动切换到wlan0(6Mbps),带宽约580KBps.客户端无不良反应• 再次ifup eth0瞬间发现2张网卡均有输出流量,一旦ifup结束,又只有1张网卡有流量• sudo ethtool wlan0,仅返回"Link detected: yes"• sudo ethtool eth0,返回网卡速率等信息

• 失败原因• 官方解释启用balance-tlb和balance-alb时网卡驱动须支持ethtool,而这2个笔记本无线网卡驱动均不支持

例:笔记本-含WiFi网卡(1/2)

10

Page 11: Linux bonding

auto lo iface lo inet loopback #eth0 iface eth0 inet dhcp bond-master bond0 #bond-primary eth0

#auto wlan0 allow-hotplug wlan0 iface wlan0 inet dhcp wpa-ssid "NETGEAR38-5G-1" wpa-psk "avccdc123" bond-master bond0 bond-primary wlan0

# bond0 auto bond0 iface bond0 inet static address 192.168.10.109 gateway 192.168.10.1 netmask 255.255.255.0 dns-nameservers 192.168.10.1 bond-mode balance-alb bond-miimon 100 bond-slaves none

例:笔记本-含WiFi网卡(2/2)/etc/network/interfaces

11

Page 12: Linux bonding

• 服务器/Ubuntu12.04• 服务器192.168.1.50上启动3张千兆以太网卡,提供HTTP访问• 虚拟网卡Bond0的IP地址是192.168.1.128• 服务器上安装bmon/iftop查看网卡带宽占用• 服务器Bonding工作模式是mode=6(balance-alb)

• 客户端• PC1,Ubuntu14.04/192.168.1.159,千兆网卡• PC2,Ubuntu14.04/192.168.1.149,百兆网卡• PC3,Windows/192.168.1.100,千兆网卡

• 网络• 接入千兆交换机

例:服务器-多以太网卡(1/7)

12

Page 13: Linux bonding

auto eth0iface eth0 inet staticaddress 192.168.1.163netmask 255.255.255.0gateway 192.168.1.254boot-master bond0boot-primary eth0

auto eth1iface eth1 inet staticaddress 192.168.1.50netmask 255.255.255.0gateway 192.168.1.254bond-master bond0

例:服务器-多以太网卡(2/7)

13/etc/network/interfaces

auth eth2iface eth2 inet manualbond-master bond0

auto bond0iface bond0 inet staticaddress 192.168.1.128netmask 255.255.255.0gateway 192.168.1.254#dns-nameservers 192.168.1.254bond-mode balance-albbond-miimon 100bond-slaves eth0 eth1 eth2

Page 14: Linux bonding

Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: adaptive load balancingPrimary Slave: NoneCurrently Active Slave: eth0MII Status: upMII Polling Interval (ms): 100Up Delay (ms): 0Down Delay (ms): 0

Slave Interface: eth1MII Status: upSpeed: 1000 MbpsDuplex: fullLink Failure Count: 2Permanent HW addr: 00:25:90:68:3f:b4Slave queue ID: 0

例:服务器-多以太网卡(3/7)

14cat /proc/net/bonding/bond0

Slave Interface: eth0MII Status: upSpeed: 1000 MbpsDuplex: fullLink Failure Count: 0Permanent HW addr: 00:25:90:91:71:acSlave queue ID: 0

Slave Interface: eth2MII Status: upSpeed: 1000 MbpsDuplex: fullLink Failure Count: 0Permanent HW addr: 00:25:90:91:71:adSlave queue ID: 0

Page 15: Linux bonding

• Bonding服务器流量-空闲• # Interface RX Rate RX # TX Rate TX #• 0 bond0 882.00B 12 684.00B 9• 1 eth0 322.00B 4 191.00B 2• 2 eth1 271.00B 3 300.00B 3• 3 eth2 287.00B 3 191.00B 2• 4 eth3 0.00B 0 0.00B 0

• Bonding服务器流量-PC1(千兆网卡)多线程请求MKV文件• while true; do axel -n 10 http://192.168.1.128/hoc.mkv ; done• # Interface RX Rate RX # TX Rate TX #• 0 bond0 2.03MiB 30387 117.19MiB 81174• 1 eth0 2.03MiB 30380 287.00B 3• 2 eth1 229.00B 1 117.19MiB 81167• 3 eth2 345.00B 2 191.00B 2• 4 eth3 0.00B 0 0.00B 0

• PC1输入带宽和服务器网卡eth1的输出带宽已接近网卡和交换机端口上限

例:服务器-多以太网卡(4/7)

15

Page 16: Linux bonding

• Bonding服务器流量-PC2(百兆网卡)多线程请求MKV文件• axel -n 10 http://192.168.1.128/hoc.mkv• # Interface RX Rate RX # TX Rate TX #• 0 bond0 269.22KiB 4136 11.68MiB 8098• 1 eth0 336.00B 3 410.00B 4• 2 eth1 268.63KiB 4129 11.67MiB 8088• 3 eth2 269.00B 2 191.00B 2• 4 eth3 0.00B 0 0.00B 0• PC2输入带宽已接近网卡上限,服务器eth1负载不高

• Bonding服务器流量-PC3(千兆网卡)单线程请求MKV文件• wget http://192.168.1.128/hoc.mkv• # Interface RX Rate RX # TX Rate TX #• 0 bond0 112.43KiB 1798 17.80MiB 12339• 1 eth0 112.12KiB 1793 346.00B 3• 2 eth1 119.00B 1 17.80MiB 12331• 3 eth2 162.00B 2 191.00B 2• 4 eth3 0.00B 0 0.00B 0• PC3输入带宽已接近网卡上限,服务器eth1负载不高

例:服务器-多以太网卡(5/7)

16

Page 17: Linux bonding

• Bonding服务器流量-PC1/PC2/PC3并发• PC1(千兆网卡) axel -n 2 http://192.168.1.128/hoc.mkv,2个线程不会占满带宽• PC2(百兆网卡) axel -n 10 http://192.168.1.128/hoc.mkv• PC3(千兆网卡) wget http://192.168.1.128/hoc.mkv

• # Interface RX Rate RX # TX Rate TX #• 0 bond0 669.34KiB 10130 105.78MiB 73107• 1 eth0 397.54KiB 5958 72.06MiB 49781• 2 eth1 271.55KiB 4171 15.28MiB 10587• 3 eth2 344.00B 2 18.47MiB 12763• 4 eth3 0.00B 0 0.00B 0

• 该测试情况没有占满服务器的每个网卡。

例:服务器-多以太网卡(6/7)

17

Page 18: Linux bonding

• 常用命令1. ifconfig eth0 down2. ifconfig eth0 up3. ifenslave bond0 eth0 eth1 eth24. iwconfig wlan05. sudo dhclient eth06. sudo /etc/init.d/networking restart

• QA• Q1. 某个slave设备MII Status: down或者没有出现在cat

/proce/net/bonding/bon0的slave中• A1. 试试上述1,2,3命令.多关闭/启动网卡几次,甚至重启系统.如果配置非静态IP地址,可能会需要点DHCP时间

• Q2. 配置顺利吗?• A2. Ubuntu官方例子不能一步到位.参考A1,保持耐心

例:服务器-多以太网卡(7/7)

18

Page 19: Linux bonding

• Bonding可用作负载均衡的生产环境,客户端只需访问虚拟网卡bond0的IP地址,Bonding分流到多张网卡

• 系统实际输出带宽约等于3张网卡之和,107.78MiB

结论

19

Page 20: Linux bonding

• 笔记本环境下验证了拔掉网线后的容错,服务器上没有测试• 3个客户端正好对应3张网卡,没验证再多些客户端是否会均衡网卡流量?

• 1个TCP连接会在多张网卡上分担流量吗?• 没抓包,不清楚各个情况下的"ARP欺骗"细节• TCP重传时走的网卡会不会和首次传输时不同?• 没有测试Bonding输入流量

局限

20

Page 21: Linux bonding

•只检测和交换机之间链路,也就是整个网络链条中的最后一跳,可能会出现外网已断开,Bonding毫不知请,使得没有容错更谈不上均衡

•网卡支持有所限制• balance-alb发送均衡只考虑网卡发送数据量,不考量数据包路由后的拥塞,丢失,虽然TCP数据重传事件一定程度上会反映到发送数据量,但仍是粗粒度均衡

Bonding不足

21

Page 22: Linux bonding

• Linux Bonding• UbuntuBonding• Ubuntu 14.04 Restore Networking• Data Center Server Access Topologies: Part 1, Logical Interface Config on

Servers• Interface/Bonding• linux bonding 详解• http://www.linux-magazin.de/Ausgaben/2014/07/Bonding• http://www.lemmster.de/network-bonding-wlan-eth-on-linux-ubuntu.html• https://github.com/zphj1987/myalldoc/blob/master/linux/%E7%BD%91%E7

%BB%9C%E7%9B%B8%E5%85%B3/ubuntu%E9%85%8D%E7%BD%AEbonding.md

参考

22