- 前言
- 读者对象
- 如何阅读本书
- 勘误和支持
- 致谢
- 第 1 篇 高性能网站构建
- 第 1 章 深入理解 DNS 原理与部署 BIND
- 第 2 章 全面解析 CDN 技术与实战
- 第 3 章 负载均衡和高可用技术
- 第 4 章 配置及调优 LVS
- 第 5 章 使用 HAProxy 实现 4 层和 7 层代理
- 第 6 章 实践 Nginx 的反向代理和负载均衡
- 第 7 章 部署商业负载均衡设备 NetScaler
- 第 8 章 配置高性能网站
- 第 9 章 优化 MySQL 数据库
- 第 2 篇 服务器安全和监控
- 第 10 章 构建企业级虚拟专用网络
- 第 11 章 实施 Linux 系统安全策略与入侵检测
- 第 12 章 实践 Zabbix 自定义模板技术
- 第 13 章 服务器硬件监控
- 第 3 篇 网络分析技术
- 第 14 章 使用 tcpdump 与 Wireshark 解决疑难问题
- 第 15 章 分析与解决运营商劫持问题
- 第 16 章 深度实践 iptables
- 第 4 篇 运维自动化和游戏运维
- 第 17 章 使用 Kickstart 完成批量系统安装
- 第 18 章 利用 Perl 编程实施高效运维
- 第 19 章 精通 Ansible 实现运维自动化
- 第 20 章 掌握端游运维的技术要点
- 第 21 章 精通手游运维的架构体系
最佳实践 14:数据链路层负载均衡
通过表 3-1 可以看到数据链路层位于第二层,它的下层是物理层,上层是网络层。
数据链路层在物理层提供的服务的基础上向网络层提供服务,其最基本的服务是将源自网络层的数据可靠地传输到相邻节点的目标机网络层。
为达到这一目的,数据链路层必须具备一系列相应的功能,主要有以下几种。
- 将数据组合成数据块。在数据链路层中称这种数据块为帧(Frame),帧是数据链路层的传送单位。
- 控制帧在物理信道上的传输,包括如何处理传输差错。
- 调节发送速率与接收方相匹配。
- 在两个网络实体之间提供数据链路通路的建立、维持和释放的管理。
链路层负载均衡的必要性
目前主流的服务器一般配备 2 个或 4 个吞吐量均为 1Gbps 的网卡,10Gbps 网卡因价格原因和对交换机模块的要求较高还未成为服务器的主流配置。在这种情况下,在以下的应用中需要对数据链路层进行负载均衡设计。
- 某些单个应用需要超过 1Gbps 的吞吐量。在某款手游测试阶段进行容量评估和规划时,笔者所在的运维团队发现,单台 Memcached 服务器的吞吐量可能达到 1.3Gbps 左右,明显超过了单个网卡的处理能力;此时又不可能替换成 10Gbps 网卡(因网络架构和成本限制)。数据链路层负载均衡成为唯一可以采用的方案。
- 对网卡功能高可用性的要求。在某些关键应用中,对网卡的可用性要求较高,业务需要在单网卡故障的情况下保证连续性,数据链路层负载均衡和高可用成为必须要考虑的实现方案。
对于 Linux 系统来说,数据链路层的负载均衡实现方案是实施双网卡绑定(Bonding),在思科(Cisco)交换机上这一技术被称作 EtherChannel。
Linux Bonding 配置过程
Linux Bonding 中服务器和交换机的架构如图 3-1 所示。
图 3-1 Linux Bonding 架构演示图
本配置中的基本信息如表 3-2 所示。
表 3-2 Linux Bonding 基本信息
Linux Bonding 的配置步骤如下。
步骤 1 配置交换机。使用的命令如下:
interface Port-channel3 switchport access vlan 9 switchport mode access #access 模式 spanning-tree portfast spanning-tree bpduguard enable interface GigabitEthernet1/0/24 switchport access vlan 9 switchport mode access spanning-tree portfast spanning-tree bpduguard enable channel-protocol lacp #使用协议 IEEE 802.3ad Dynamic link aggregation channel-group 3 mode active ! interface GigabitEthernet1/0/25 switchport access vlan 9 switchport mode access spanning-tree portfast spanning-tree bpduguard enable channel-protocol lacp #使用协议 IEEE 802.3ad Dynamic link aggregation channel-group 3 mode active !
步骤 2 配置 Linux。
配置网卡 em3 使用的命令如下:
# cat /etc/sysconfig/network-scripts/ifcfg-em3 DEVICE=em3 BOOTPROTO=none ONBOOT=yes USERCTL=no MASTER=bond0 #属于 bond0 的成员 SLAVE=yes
配置网卡 em4 使用的命令如下:
# cat /etc/sysconfig/network-scripts/ifcfg-em4 DEVICE=em4 BOOTPROTO=none ONBOOT=yes USERCTL=no MASTER=bond0 #属于 bond0 的成员 SLAVE=yes
配置 bond0 使用的命令如下:
# cat /etc/sysconfig/network-scripts/ifcfg-bond0 DEVICE=bond0 IPADDR=192.168.9.100 NETMASK=255.255.0.0 GATEWAY=192.168.9.5 ONBOOT=yes TYPE=Ethernet BOOTPROTO=static BONDING_OPTS="miimon=100 mode=4" #注意,此处 mode 必须选为 4
重启网卡生效。
注意
(1)步骤 2 的配置错误可能导致无法远程登录服务器。
建议:使用带外管理(Out-of-band Management)(例如 DELL iDRAC)或者在机房本地配置。
(2)在配置过程中,Linux 的 Bonding 模式必须选择为 4,即 IEEE 802.3ad Dynamic link aggregation。否则,Linux 服务器和交换机协商不成功。
步骤 3 在交换机上确认。命令如下:
#show etherchannel summary Flags: D - down P - bundled in port-channel I - stand-alone s - suspended H - Hot-standby (LACP only) R - Layer3 S - Layer2 U - in use f - failed to allocate aggregator M - not in use, minimum links not met u - unsuitable for bundling w - waiting to be aggregated d - default port Number of channel-groups in use: 5 Number of aggregators: 5 Group Port-channel Protocol Ports ------+-------------+-----------+----------------------------------------------- 1 Po1(SD) - 2 Po3(SU) LACP Gi1/0/24(P) Gi1/0/25(P)
注意
Po3 后面必须是 SU 状态,如果为 D,则表示协商失败。
步骤 4 在服务器上确认。使用的命令如下:
# ethtool bond0 Settings for bond0: Supported ports: [ ] Supported link modes: Not reported Supported pause frame use: No Supports auto-negotiation: No Advertised link modes: Not reported Advertised pause frame use: No Advertised auto-negotiation: No Speed: 2000Mb/s #此处的 Speed 为 2 个网卡之和 Duplex: Full #全双工 Port: Other PHYAD: 0 Transceiver: internal Auto-negotiation: off Link detected: yes
查看 bond0 状态,使用的命令如下:
# cat /proc/net/bonding/bond0 Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009) Bonding Mode: IEEE 802.3ad Dynamic link aggregation Transmit Hash Policy: layer2 (0) MII Status: up #此处为 up 状态 MII Polling Interval (ms): 100 Up Delay (ms): 0 Down Delay (ms): 0 802.3ad info LACP rate: slow Aggregator selection policy (ad_select): stable Active Aggregator Info: Aggregator ID: 3 Number of ports: 2 Actor Key: 17 Partner Key: 3 Partner Mac Address: 38:20:56:67:bb:00 Slave Interface: em3 MII Status: up Speed: 1000 Mbps Duplex: full Link Failure Count: 0 Permanent HW addr: 44:a8:42:47:f6:bd Aggregator ID: 3 Slave queue ID: 0 Slave Interface: em4 MII Status: up Speed: 1000 Mbps Duplex: full Link Failure Count: 0 Permanent HW addr: 44:a8:42:47:f6:be Aggregator ID: 3 Slave queue ID: 0
步骤 5 流量测试和可用性测试。
1)分别依次拔掉 em3、em4 的网线,使用 ping 观察网络连通情况。需要验证:在任何一根网线拔掉的情况下,网络连通性不受影响,仍然可以访问。
2)从多个服务器上,同时使用 iPerf( https://iperf.fr/ )工具对 192.168.9.100 进行吞吐量测试。目的是测试交换机到服务器 Bonding 的入吞吐量。需要验证:吞吐量达到单网卡约 2 倍,约 1.8Gbps。
3)从 192.168.9.100,使用 iPerf 同时向多个服务器进行吞吐量测试。目的是测试服务器 Bonding 到交换机的出吞吐量。需要验证:吞吐量达到单网卡时的 2 倍左右,即约 1.8Gbps。
该服务器正常业务流量的 Bonding 情况如下:
# ifstat -b em3 em4 bond0 Kbps in Kbps out Kbps in Kbps out Kbps in Kbps out 380.36 211.42 239.04 106.27 619.41 317.70 228.61 124.84 229.04 117.02 457.65 241.87 291.90 157.84 198.16 102.17 490.06 260.01 277.57 329.95 261.42 89.13 538.99 419.08 262.28 194.71 291.02 84.11 553.30 278.82 253.73 175.69 358.40 96.60 612.12 272.28 290.11 203.07 248.56 94.51 538.67 297.58 261.87 165.73 1200.52 114.76 1462.39 280.49 229.15 192.14 413.50 103.48 642.65 295.62 222.54 196.43 341.36 71.52 563.89 267.95 255.00 173.73 271.49 66.76 526.49 240.49
由此可见,bond0 的带宽为 em3、em4 之和,两个网卡进行了负载均衡。
注意
(1)从 Cisco 交换机到服务器的端口负载均衡算法,采用了 Cisco 的私有算法,不可配置。
(2)从服务器到 Cisco 交换机发出数据的端口选择,可以使用 xmit_hash_policy 这个配置项进行调整。如果同网段的多台服务器调用该绑定的服务器,则可以使用默认算法 layer2;对公网多 IP 的来源访问,可以修改为 layer2+3。参见 https://www.kernel.org/doc/Documentation/networking/bonding.txt 。
经过以上 5 个步骤,成功创建了数据链路层的负载均衡,针对前述提到的两个数据链路层需求,可以有效地满足。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论