引言:AlmaLinux 性能优化的重要性

AlmaLinux 作为 CentOS 的替代者,继承了 RHEL 的稳定性和安全性,广泛应用于服务器环境。然而,随着业务负载的增加,系统性能可能成为瓶颈。本文将深入探讨提升 AlmaLinux 系统性能的实用技巧与优化策略,涵盖内核参数调优、文件系统优化、资源监控、服务管理等多个方面,帮助您构建高性能的 AlmaLinux 服务器环境。

一、系统监控与性能分析工具

在进行性能优化之前,首先需要了解系统当前的性能状态。AlmaLinux 提供了丰富的监控工具,帮助我们识别瓶颈。

1.1 使用 tophtop 实时监控进程

top 是 Linux 系统自带的实时监控工具,可以查看 CPU、内存、进程等信息。htoptop 的增强版,界面更友好,操作更直观。

安装 htop:

sudo dnf install htop -y

使用示例:

htop

htop 界面中,您可以使用 F6 键排序,F9 键终止进程,F10 键退出。通过观察 CPU 和内存使用率,可以快速定位资源消耗过高的进程。

1.2 使用 vmstatiostat 分析系统资源

vmstat 用于报告虚拟内存统计信息,iostat 用于监控磁盘 I/O 统计信息。

安装 sysstat 包:

sudo dnf install sysstat -y

使用 vmstat:

vmstat 1 5

输出示例:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 123456  78910 456789    0    0     0     0  100  200  5  2 93  0  0
 0  0      0 123456  78910 456789    0    0     0     0  100  200  5  2 93  0  0
  • r:运行队列长度(CPU 负载)
  • si/so:交换区输入/输出(内存不足时会增加)
  • bi/bo:块 I/O(磁盘读写)

使用 iostat:

iostat -x 1 5

输出示例:

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           5.00    0.00    2.00    0.50    0.00   92.50

Device            r/s     w/s     rkB/s     wkB/s   rrqm/s   wrqm/s  %rrqm  %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm  %util
sda              0.50    2.00      2.00      8.00     0.00     0.00   0.00   0.00    0.50    1.00  0.01     4.00     4.00  0.50   0.05
  • %util:磁盘利用率(接近 100% 表示磁盘瓶颈)
  • await:平均 I/O 等待时间(过高表示磁盘响应慢)

1.3 使用 sar 进行长期性能分析

sar 可以收集、报告和保存系统活动信息,适合长期监控。

启用 sar:

sudo dnf install sysstat -y
sudo systemctl enable --now sysstat

查看历史数据:

sar -u 1 10  # CPU 使用率
sar -r 1 10  # 内存使用率
sar -d 1 10  # 磁盘 I/O

二、内核参数调优

内核参数直接影响系统性能,特别是网络和内存管理方面。通过调整 /etc/sysctl.conf 文件,可以优化系统行为。

2.1 网络性能优化

对于高并发网络服务器,调整 TCP 参数可以显著提升性能。

编辑 sysctl.conf:

sudo vi /etc/sysctl.conf

添加以下参数:

# 增加 TCP 连接队列长度
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535

# 启用 TCP 快速打开
net.ipv4.tcp_fastopen = 3

# 增加可用端口范围
net.ipv4.ip_local_port_range = 1024 65535

# 减少 TIME_WAIT 状态连接
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1  # 注意:在 NAT 环境下可能有问题,谨慎使用

# 增加 TCP 缓冲区大小
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216

# 减少 TCP 重试次数
net.ipv4.tcp_retries2 = 5

# 启用 TCP 拥塞控制算法(推荐 BBR)
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr

应用配置:

sudo sysctl -p

验证 BBR 是否启用:

sysctl net.ipv4.tcp_congestion_control

输出应为:net.ipv4.tcp_congestion_control = bbr

2.2 内存管理优化

调整内存管理参数可以减少交换(swap)使用,提高响应速度。

编辑 sysctl.conf:

sudo vi /etc/sysctl.conf

添加以下参数:

# 减少交换倾向,优先使用物理内存
vm.swappiness = 10  # 默认值为 60,降低到 10 表示尽量少用 swap

# 增加内存分配策略的灵活性
vm.overcommit_memory = 0  # 0: 启发式过量使用,1: 总是允许,2: 不允许超过物理内存+交换空间

# 调整脏页比例和刷新频率
vm.dirty_ratio = 15  # 系统内存中脏页占比超过 15% 时,开始同步写回磁盘
vm.dirty_background_ratio = 5  # 系统内存中脏页占比超过 5% 时,后台开始异步写回磁盘
vm.dirty_expire_centisecs = 3000  # 脏页过期时间(30秒)

# 调整内存回收机制
vm.vfs_cache_pressure = 100  # 控制内核回收 inode 和 dentry 缓存的倾向,默认 100,降低该值可减少回收

应用配置:

sudo sysctl -p

2.3 文件系统优化

文件系统参数调整可以提升磁盘 I/O 性能。

编辑 sysctl.conf:

sudo vi /etc/sysctl.conf

添加以下参数:

# 增加文件句柄数限制
fs.file-max = 2097152

# 增加进程文件句柄数限制
fs.nr_open = 2097152

# 调整目录项缓存
fs.dir-notify-enable = 0  # 禁用目录通知,减少开销(根据场景选择)

同时,需要调整用户级别的文件句柄数限制: 编辑 /etc/security/limits.conf

sudo vi /etc/security/limits.conf

添加以下内容:

* soft nofile 1048576
* hard nofile 1048576
root soft nofile 1048576
root hard nofile 1048576

应用配置:

sudo sysctl -p
# 需要重新登录或重启系统使 limits.conf 生效

三、文件系统与磁盘优化

3.1 选择合适的文件系统

AlmaLinux 默认使用 XFS 文件系统,它在处理大文件和大目录方面表现优异。对于特定场景,可以考虑 ext4。

查看当前文件系统类型:

df -T

格式化磁盘为 XFS(以 /dev/sdb 为例):

sudo mkfs.xfs /dev/sdb

挂载时优化参数: 编辑 /etc/fstab

sudo vi /etc/fstab

添加或修改挂载选项:

/dev/sdb1 /data xfs defaults,noatime,nodiratime 0 0
  • noatime:禁止更新访问时间,减少磁盘写入
  • nodiratime:禁止更新目录访问时间

3.2 启用 TRIM(针对 SSD)

如果使用 SSD,启用 TRIM 可以保持 SSD 长期性能。

检查是否支持 TRIM:

sudo lsblk -D

查看 DISC-GRANDISC-MAX 列,如果非 0,则支持。

手动执行 TRIM:

sudo fstrim -v /data

设置定时任务:

sudo systemctl enable fstrim.timer
sudo systemctl start fstrim.timer

3.3 使用 LVM 优化磁盘管理

LVM(逻辑卷管理)提供灵活的磁盘管理能力,可以在线调整卷大小。

安装 LVM 工具(通常已预装):

sudo dnf install lvm2 -y

创建逻辑卷示例:

# 创建物理卷
sudo pvcreate /dev/sdb

# 创建卷组
sudo vgcreate vg_data /dev/sdb

# 创建逻辑卷(分配 50% 空间)
sudo lvcreate -l 50%FREE -n lv_data vg_data

# 格式化并挂载
sudo mkfs.xfs /dev/vg_data/lv_data
sudo mkdir /data
sudo mount /dev/vg_data/lv_data /data

在线扩展逻辑卷:

# 扩展逻辑卷(增加 10G)
sudo lvextend -L +10G /dev/vg_data/lv_data

# 扩展文件系统(XFS)
sudo xfs_growfs /data

四、服务与进程管理优化

4.1 禁用不必要的服务

减少运行的服务可以释放系统资源。

查看所有服务:

systemctl list-unit-files --type=service

禁用不必要的服务(例如 bluetooth):

sudo systemctl disable bluetooth
sudo systemctl stop bluetooth

查看服务资源占用:

systemd-cgtop

4.2 使用 cgroups 限制资源

cgroups 可以限制进程组的资源使用,防止单个进程耗尽系统资源。

安装 libcgroup-tools:

sudo dnf install libcgroup-tools -y

创建 cgroup 限制 CPU 使用:

# 创建 cgroup
sudo cgcreate -g cpu:/limited

# 设置 CPU 限制(50% 单核)
sudo cgset -r cpu.cfs_period_us=100000 cpu.cfs_quota_us=50000 limited

# 在 cgroup 中运行进程
sudo cgexec -g cpu:limited your_command

4.3 调整进程优先级

使用 nicerenice 调整进程优先级。

启动时设置优先级(-20 到 19,默认 0,值越小优先级越高):

nice -n -10 your_command

调整运行中进程的优先级:

# 查找进程 PID
ps aux | grep your_command

# 调整优先级
sudo renice -n -10 -p <PID>

五、网络性能优化

5.1 网络接口参数调整

调整网卡参数可以提升网络吞吐量。

查看网卡支持的参数:

ethtool eth0

设置网卡多队列(如果支持):

sudo ethtool -L eth0 combined 8  # 设置为 8 队列

调整网卡缓冲区:

sudo ethtool -G eth0 rx 4096 tx 4096

5.2 使用网络团队化(Teamd)

网络团队化可以提供负载均衡和冗余。

安装 teamd:

sudo dnf install teamd -y

创建团队接口配置:

sudo nmcli con add type team con-name team0 ifname team0 config '{"runner": {"name": "activebackup"}}'
sudo nmcli con add type team-slave con-name team0-port1 ifname eth0 master team0
sudo nmcli con add type team-slave con-name team0-port2 ifname eth1 master team0

配置 IP 地址:

sudo nmcli con mod team0 ipv4.addresses 192.168.1.100/24
sudo nmcli con mod team0 ipv4.method manual
sudo nmcli con up team0

六、高级优化策略

6.1 使用 tuned 动态调优

tuned 是 Red Hat 提供的系统调优工具,提供多种预设配置文件。

安装 tuned:

sudo dnf install tuned -y

启动并设置开机自启:

sudo systemctl enable --now tuned

查看可用配置文件:

tuned-adm list

应用配置文件(例如网络服务器优化):

sudo tuned-adm profile network-latency

自定义配置文件:

sudo mkdir -p /etc/tuned/custom-profile
sudo vi /etc/tuned/custom-profile/tuned.conf

添加以下内容:

[main]
include=network-latency

[sysctl]
net.core.somaxconn=65535
vm.swappiness=10

应用自定义配置:

sudo tuned-adm profile custom-profile

6.2 使用 NUMA 优化

对于多 CPU 插槽服务器,NUMA 优化可以减少跨节点内存访问延迟。

安装 numactl:

sudo dnf install numactl -y

查看 NUMA 拓扑:

numactl --hardware

在特定节点上运行进程:

numactl --cpunodebind=0 --membind=0 your_command

6.3 内核参数实时调整

使用 sysctl 实时调整参数,无需重启。

示例:临时增加 TCP 缓冲区:

sudo sysctl -w net.core.rmem_max=16777216
sudo sysctl -w net.core.wmem_max=16777216

持久化调整: 将参数添加到 /etc/sysctl.conf,然后执行 sudo sysctl -p

七、安全与性能平衡

7.1 SELinux 性能影响

SELinux 可能对性能有轻微影响,但不建议禁用。可以通过调整策略减少开销。

查看 SELinux 状态:

sestatus

临时设置为宽松模式(测试用):

sudo setenforce 0

永久设置为宽松模式(不推荐生产环境): 编辑 /etc/selinux/config

SELINUX=permissive

7.2 防火墙优化

防火墙规则过多会影响网络性能。

查看当前规则:

sudo firewall-cmd --list-all

优化规则:

# 删除不必要的规则
sudo firewall-cmd --permanent --remove-service=ssh  # 如果不需要 SSH
sudo firewall-cmd --reload

八、总结

提升 AlmaLinux 系统性能需要综合考虑多个方面,包括监控分析、内核调优、文件系统优化、服务管理、网络优化等。通过本文介绍的技巧和策略,您可以系统地识别和解决性能瓶颈。记住,优化是一个持续的过程,需要根据实际业务负载不断调整和验证。

关键要点:

  1. 监控先行:使用工具识别瓶颈
  2. 渐进式调整:一次只调整一个参数,观察效果
  3. 备份配置:修改重要文件前备份
  4. 测试验证:在生产环境应用前充分测试

通过合理的优化,您的 AlmaLinux 服务器将能够更好地应对高负载挑战,提供更稳定、高效的服务。