Skip to content

第 13 章 性能调优

13.1 性能瓶颈分析思路

调优前先定位瓶颈。常见瓶颈来源:

  1. CPU:加密、压缩、哈希计算
  2. 内存:缓存不足
  3. 磁盘:IOPS 或吞吐达到上限
  4. 网络:带宽或延迟
  5. 软件配置:线程数、缓存大小、超时时间

常用监控工具

工具用途
top/htopCPU、内存
iostat磁盘 IO
iftop/nload网络流量
sar综合性能
fio存储基准测试

13.2 GlusterFS 性能参数调优

写入性能

bash
# IO 线程数
gluster volume set gv0 performance.io-thread-count 32

# 写缓存大小
gluster volume set gv0 performance.cache-size 512MB

# 写合并
gluster volume set gv0 performance.write-behind-window-size 4MB

读取性能

bash
# 预读大小
gluster volume set gv0 performance.read-ahead on
gluster volume set gv0 performance.read-ahead-page-count 4

# 目录缓存
gluster volume set gv0 performance.md-cache-statfs on

元数据性能

bash
# 启用目录索引
gluster volume set gv0 performance.readdir-ahead on

# 客户端缓存元数据
gluster volume set gv0 performance.stat-prefetch on

网络参数

bash
# 超时时间
gluster volume set gv0 network.ping-timeout 5

# 启用快速失败
gluster volume set gv0 cluster.server-quorum-type server

13.3 NFS-Ganesha 性能参数

配置文件中调整

conf
EXPORT {
    ...
    Attr_Expiration_Time = 60;
    NFS_Commit = true;
    MaxRead = 1048576;
    MaxWrite = 1048576;
    PrefRead = 1048576;
    PrefWrite = 1048576;
}

参数说明

参数说明
Attr_Expiration_Time属性缓存时间
MaxRead/MaxWrite单次读写最大字节数
PrefRead/PrefWrite建议读写大小

13.4 网络与内核参数优化

网络调优

编辑 /etc/sysctl.conf

conf
net.core.rmem_max = 134217728
net.core.wmem_max = 134217728
net.ipv4.tcp_rmem = 4096 87380 134217728
net.ipv4.tcp_wmem = 4096 65536 134217728
net.ipv4.tcp_congestion_control = bbr

应用:

bash
sysctl -p

NFS 客户端挂载参数

bash
mount -t nfs -o vers=4.1,hard,intr,rsize=1048576,wsize=1048576,timeo=600,retrans=3 10.0.0.100:/gv0 /mnt/nfs-ha

13.5 测试工具与基准测试

fio 测试

安装:

bash
apt install -y fio

顺序写测试:

bash
fio -directory=/mnt/nfs-ha -direct=1 -rw=write -ioengine=libaio \
    -bs=1M -size=1G -numjobs=4 -runtime=60 -group_reporting \
    -name=seq-write-test

随机读测试:

bash
fio -directory=/mnt/nfs-ha -direct=1 -rw=randread -ioengine=libaio \
    -bs=4k -size=1G -numjobs=8 -runtime=60 -group_reporting \
    -name=rand-read-test

关键指标

指标说明
IOPS每秒 IO 操作数
Bandwidth吞吐(MB/s)
Latency延迟

13.6 本章小结

  • 性能调优前先定位瓶颈
  • GlusterFS 提供丰富的性能参数
  • NFS-Ganesha 可调整读写块大小和缓存
  • 网络和内核参数对 NFS 性能影响很大
  • 使用 fio 进行基准测试

练习题

  1. 如何定位 NFS 存储的性能瓶颈?
  2. performance.io-thread-count 参数影响什么?
  3. 为什么 TCP 缓冲区大小会影响 NFS 性能?
  4. 设计一个 fio 测试命令,测试随机写性能。