1. RHEL9.7生产环境部署前的系统评估
在开始优化前,我们需要对基础系统进行全面的评估。RHEL9.7作为Red Hat Enterprise Linux的最新稳定版本,采用了多项新技术:
- 内核版本默认升级至5.14.x系列,支持更新的硬件和文件系统特性
- 默认使用Wayland显示协议替代传统的X11
- 软件包管理全面转向dnf,yum作为兼容层保留
- 安全模块默认启用SELinux和fapolicyd
重要提示:生产环境部署前务必进行硬件兼容性检查,特别是使用较新CPU或NVMe存储设备时。建议运行
lshw -json > hardware_report.json生成详细硬件报告。
1.1 最小化安装原则
生产服务器应采用最小化安装模式:
bash复制# 安装时选择"Minimal Install"基础环境
dnf groupinstall "Minimal Install" --setopt=group_package_types=mandatory
安装后立即删除不必要的软件包:
bash复制# 移除图形界面相关组件(即使最小化安装也可能包含)
dnf remove xorg-x11* gnome* wayland* -y
# 清理孤儿依赖
dnf autoremove -y
1.2 系统分区方案优化
推荐的生产环境分区方案(以1TB SSD为例):
| 挂载点 | 大小 | 文件系统 | 优化参数 |
|---|---|---|---|
| /boot | 1GB | xfs | -f -i size=512 |
| / | 50GB | xfs | -f -d agcount=16 |
| /var | 100GB | xfs | -f -s size=4096 |
| /home | 50GB | xfs | -f |
| /opt | 根据应用需求 | xfs | -f |
| swap | 内存≤64GB时=内存大小,>64GB时=32GB | - | - |
| 剩余空间 | - | 留作LVM卷组 | - |
格式化命令示例:
bash复制mkfs.xfs -f -d agcount=16 /dev/sda2
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 内核参数与系统基础优化
2.1 sysctl调优
编辑/etc/sysctl.d/99-production.conf:
conf复制# 网络栈优化
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_sack = 1
# 内存管理
vm.swappiness = 10
vm.dirty_ratio = 20
vm.dirty_background_ratio = 10
vm.overcommit_memory = 1
# 文件系统
fs.file-max = 65535
fs.inotify.max_user_watches = 524288
# 安全相关
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
kernel.kptr_restrict = 2
应用配置:sysctl -p /etc/sysctl.d/99-production.conf
2.2 服务管理优化
禁用不必要的系统服务:
bash复制systemctl disable --now avahi-daemon cups bluetooth
关键服务推荐配置:
bash复制# 调整journald日志设置
mkdir -p /etc/systemd/journald.conf.d/
cat > /etc/systemd/journald.conf.d/99-production.conf <<EOF
[Journal]
SystemMaxUse=1G
RuntimeMaxUse=200M
MaxFileSec=1month
EOF
# 重启服务
systemctl restart systemd-journald
3. 安全加固与访问控制
3.1 SELinux策略定制
生产环境应保持SELinux强制模式:
bash复制# 检查当前状态
getenforce
# 永久启用
sed -i 's/SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
常见问题处理:
bash复制# 查看拒绝日志
ausearch -m avc -ts recent
# 生成自定义策略模块
audit2allow -a -M mypolicy
semodule -i mypolicy.pp
3.2 防火墙高级配置
使用firewalld构建分层防御:
bash复制# 创建生产环境专用zone
firewall-cmd --permanent --new-zone=production
# 设置默认规则
firewall-cmd --permanent --zone=production --set-target=DROP
firewall-cmd --permanent --zone=production --add-service=ssh
firewall-cmd --permanent --zone=production --add-service=https
# 应用配置
firewall-cmd --reload
3.3 SSH安全加固
编辑/etc/ssh/sshd_config:
conf复制Port 22222
Protocol 2
PermitRootLogin no
MaxAuthTries 3
LoginGraceTime 60
ClientAliveInterval 300
ClientAliveCountMax 2
UsePAM yes
X11Forwarding no
AllowTcpForwarding no
PermitTunnel no
AllowAgentForwarding no
重启服务前测试配置:sshd -t
4. 存储与文件系统优化
4.1 XFS高级参数调整
针对数据库等IO密集型应用:
bash复制# 查看当前文件系统参数
xfs_info /dev/sda1
# 挂载参数优化
vim /etc/fstab
UUID=xxx /var xfs defaults,noatime,nodiratime,logbsize=256k,logbufs=8 0 0
4.2 LVM缓存配置
使用SSD加速机械硬盘:
bash复制# 创建缓存池
pvcreate /dev/nvme0n1p1
vgextend rhel /dev/nvme0n1p1
lvcreate -L 100G -n cachepool rhel /dev/nvme0n1p1
lvconvert --type cache-pool --poolmetadata rhel/cachepool rhel/cachepool
# 将缓存附加到数据卷
lvconvert --type cache --cachepool rhel/cachepool rhel/data
4.3 定期文件系统维护
设置每周维护任务:
bash复制cat > /etc/cron.weekly/xfs_maintenance <<EOF
#!/bin/bash
xfs_fsr -v /var
xfs_db -c frag -r /var
EOF
chmod +x /etc/cron.weekly/xfs_maintenance
5. 性能监控与调优工具
5.1 使用tuned进行性能优化
选择适合的profile:
bash复制# 列出可用profile
tuned-adm list
# 数据库服务器推荐
tuned-adm profile throughput-performance
# 自定义profile
mkdir -p /etc/tuned/myprofile
cat > /etc/tuned/myprofile/tuned.conf <<EOF
[main]
include=throughput-performance
[cpu]
force_latency=1
[vm]
transparent_hugepages=always
EOF
5.2 使用bpftrace进行深度监控
安装bpftrace工具集:
bash复制dnf install bpftrace bpftrace-tools -y
实用示例:
bash复制# 跟踪块设备IO延迟
bpftrace -e 'tracepoint:block:block_rq_complete {
@usecs = hist(args->duration / 1000);
@bytes = hist(args->nr_bytes);
} interval:s:5 { print(@usecs); print(@bytes); clear(@usecs); clear(@bytes); }'
5.3 使用Performance Co-Pilot
安装PCP套件:
bash复制dnf install pcp pcp-system-tools -y
systemctl enable --now pmcd pmlogger
常用命令:
bash复制# 实时监控
pmstat -t 2
# 历史数据分析
pmlogsummary -S @00:00 -T @23:59 /var/log/pcp/pmlogger/$(hostname)/YYYYMMDD
6. 编译环境与开发工具优化
6.1 编译器优化选项
针对特定CPU架构优化:
bash复制# 查看CPU支持的指令集
cat /proc/cpuinfo | grep flags
# GCC编译优化示例
CFLAGS="-O3 -march=native -mtune=native -pipe"
CXXFLAGS="${CFLAGS}"
MAKEFLAGS="-j$(nproc)"
6.2 内核实时补丁(可选)
对于需要低延迟的环境:
bash复制# 安装实时内核
dnf install kernel-rt -y
# 验证安装
uname -a | grep rt
6.3 调试工具配置
优化gdb体验:
bash复制cat > ~/.gdbinit <<EOF
set pagination off
set history save on
set history filename ~/.gdb_history
set print pretty on
EOF
7. 自动化维护与更新策略
7.1 智能化的yum/dnf更新
创建自动更新策略:
bash复制cat > /etc/dnf/automatic.conf <<EOF
[commands]
upgrade_type = security
random_sleep = 3600
download_updates = yes
apply_updates = no
[emitters]
emit_via = stdio
[email]
email_from = root@$(hostname)
email_to = admin@example.com
email_host = localhost
EOF
7.2 自动化日志轮转
定制logrotate配置:
bash复制cat > /etc/logrotate.d/myapp <<EOF
/var/log/myapp/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 0640 appuser appgroup
sharedscripts
postrotate
/bin/kill -HUP $(cat /var/run/myapp.pid 2>/dev/null) 2>/dev/null || true
endscript
}
EOF
7.3 系统健康检查脚本
创建每日健康检查:
bash复制cat > /usr/local/bin/health_check <<'EOF'
#!/bin/bash
REPORT="/var/log/health/$(date +%Y%m%d).log"
mkdir -p $(dirname $REPORT)
{
echo "===== $(date) ====="
echo "## Memory ##"
free -h
echo -e "\n## Disk ##"
df -h
echo -e "\n## Top Processes ##"
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -n 10
echo -e "\n## Network ##"
ss -s
} > $REPORT
EOF
chmod +x /usr/local/bin/health_check
8. 容器化环境优化
8.1 Podman性能调优
配置容器存储:
bash复制# 使用overlay2驱动
sed -i 's/driver = ".*"/driver = "overlay"/' /etc/containers/storage.conf
# 限制容器日志大小
mkdir -p /etc/containers/registries.conf.d/
cat > /etc/containers/registries.conf.d/99-production.conf <<EOF
[containers]
log_size_max = "10M"
EOF
8.2 容器网络优化
创建高性能网络:
bash复制podman network create --subnet 10.88.100.0/24 --opt mtu=9000 prod-net
8.3 资源限制配置
限制容器资源使用:
bash复制podman run -d --name myapp \
--memory=2g \
--cpus=2 \
--cpu-shares=512 \
--ulimit nofile=1024:1024 \
myapp:latest
9. 数据库服务器专项优化
9.1 PostgreSQL优化示例
调整postgresql.conf:
conf复制# 内存设置
shared_buffers = 4GB
effective_cache_size = 12GB
work_mem = 32MB
maintenance_work_mem = 1GB
# WAL设置
wal_level = replica
wal_buffers = 16MB
checkpoint_completion_target = 0.9
# 并行查询
max_worker_processes = 8
max_parallel_workers_per_gather = 4
max_parallel_workers = 8
9.2 MySQL/MariaDB优化示例
配置my.cnf:
conf复制[mysqld]
innodb_buffer_pool_size = 4G
innodb_log_file_size = 1G
innodb_flush_method = O_DIRECT
innodb_flush_neighbors = 0
innodb_read_io_threads = 8
innodb_write_io_threads = 4
query_cache_type = 0
table_open_cache = 4000
10. 高可用性配置
10.1 Pacemaker集群基础
安装集群组件:
bash复制dnf install pacemaker pcs fence-agents-all -y
初始化集群:
bash复制pcs cluster auth node1 node2 -u hacluster -p password
pcs cluster setup --name mycluster node1 node2
pcs cluster start --all
10.2 配置VIP资源
bash复制pcs resource create ClusterIP ocf:heartbeat:IPaddr2 \
ip=192.168.1.100 cidr_netmask=24 \
op monitor interval=30s
10.3 配置DRBD资源
创建DRBD资源配置:
conf复制resource r0 {
protocol C;
device /dev/drbd0;
disk /dev/sdb1;
meta-disk internal;
on node1 {
address 192.168.1.101:7788;
}
on node2 {
address 192.168.1.102:7788;
}
}
初始化资源:
bash复制drbdadm create-md r0
drbdadm up r0
