内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
风哥提示:
本文档介绍大规模环境的性能优化方案。
Part01-系统性能分析
1.1 CPU性能分析
[root@perf-server ~]# top -bn1 | head -20
top – 16:30:00 up 10 days, 5:30, 2 users, load average: 2.50, 2.30, 2.10
Tasks: 200 total, 2 running, 198 sleeping, 0 stopped, 0 zombie
%Cpu(s): 25.0 us, 10.0 sy, 0.0 ni, 60.0 id, 5.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 32000.0 total, 10000.0 free, 20000.0 used, 2000.0 buff/cache
MiB Swap: 8000.0 total, 8000.0 free, 0.0 used. 10000.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 mysql 20 0 10.0g 5.0g 100m S 50.0 16.0 1000:00 mysqld
2345 nginx 20 0 500m 200m 50m S 20.0 0.6 500:00 nginx
3456 java 20 0 8.0g 4.0g 200m S 15.0 12.0 800:00 java
4567 redis 20 0 500m 300m 50m S 10.0 0.9 200:00 redis-server
# 查看CPU详细信息
[root@perf-server ~]# mpstat -P ALL 1 5
Linux 5.14.0-284.11.1.el9_2.x86_64 (perf-server) 04/04/2026 _x86_64_ (64 CPU)
16:30:00 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
16:30:01 all 25.00 0.00 10.00 5.00 0.00 0.00 0.00 0.00 0.00 60.00
16:30:01 0 30.00 0.00 15.00 5.00 0.00 0.00 0.00 0.00 0.00 50.00
16:30:01 1 28.00 0.00 12.00 6.00 0.00 0.00 0.00 0.00 0.00 54.00
16:30:01 2 25.00 0.00 10.00 5.00 0.00 0.00 0.00 0.00 0.00 60.00
16:30:01 3 22.00 0.00 8.00 4.00 0.00 0.00 0.00 0.00 0.00 66.00
Average: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
Average: all 25.00 0.00 10.00 5.00 0.00 0.00 0.00 0.00 0.00 60.00
# 分析CPU瓶颈
[root@perf-server ~]# perf top -g
Samples: 10K of event ‘cpu-clock’, Event count (approx.): 2500000000
Overhead Shared Object Symbol
15.00% [kernel] [k] native_write_p5
12.00% mysqld [.] my_malloc
10.00% [kernel] [k] copy_user_generic_string
8.00% libpthread-2.28.so [.] pthread_mutex_lock
6.00% [kernel] [k] system_call_fastpath
# 查看进程CPU使用
[root@perf-server ~]# pidstat -p 1234 1 5
Linux 5.14.0-284.11.1.el9_2.x86_64 (perf-server) 04/04/2026 _x86_64_ (64 CPU)
16:30:00 UID PID %usr %system %guest %wait %CPU CPU Command
16:30:01 0 1234 45.00 10.00 0.00 5.00 55.00 0 mysqld
16:30:02 0 1234 48.00 12.00 0.00 4.00 60.00 1 mysqld
16:30:03 0 1234 42.00 8.00 0.00 6.00 50.00 2 mysqld
16:30:04 0 1234 50.00 15.00 0.00 3.00 65.00 3 mysqld
16:30:05 0 1234 46.00 11.00 0.00 5.00 57.00 0 mysqld
Average: 0 1234 46.20 11.20 0.00 4.60 57.40 – mysqld
1.2 内存性能分析
[root@perf-server ~]# free -h
total used free shared buff/cache available
Mem: 31Gi 20Gi 10Gi 2.0Gi 2.0Gi 10Gi
Swap: 7.8Gi 0B 7.8Gi
# 查看内存详细信息
[root@perf-server ~]# 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
2 0 0 10485760 2097152 2097152 0 0 10 20 1000 2000 25 10 60 5 0
3 0 0 10484736 2097152 2097152 0 0 15 25 1050 2100 26 11 58 5 0
2 0 0 10483712 2097152 2097152 0 0 12 22 1020 2050 24 10 61 5 0
3 0 0 10482688 2097152 2097152 0 0 18 28 1080 2150 27 12 56 5 0
2 0 0 10481664 2097152 2097152 0 0 14 24 1040 2080 25 11 59 5 0
# 查看进程内存使用
[root@perf-server ~]# ps aux –sort=-%mem | head -10
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
mysql 1234 50.0 16.0 10485760 5242880 ? Ssl Apr01 1000:00 /usr/sbin/mysfrom PG视频:www.itpux.comqld
java 3456 15.0 12.0 8388608 4194304 ? Sl Apr01 800:00 /usr/bin/java
redis 4567 10.0 0.9 524288 314572 ? Ssl Apr01 200:00 /usr/bin/redis-server
nginx 2345 20.0 0.6 524288 209715 ? S Apr01 500:00 nginx: worker process
# 查看内存映射
[root@perf-server ~]# pmap -x 1234 | head -30
1234: /usr/sbin/mysqld
Address Kbytes RSS Dirty Mode Mapping
0000555555554000 4096 4096 4096 r-x– mysqld
0000555555954000 4096 4096 4096 r—- mysqld
0000555555955000 8192 8192 8192 rw— mysqld
0000555555955000 256 0 0 rw— [ anon ]
0000555555b55000 4096 4096 4096 rw— [ anon ]
00007fff00000000 131072 131072 131072 rw— [ anon ]
00007fff00800000 131072 131072 131072 rw— [ anon ]
00007fff01000000 131072 131072 131072 rw— [ anon ]
00007fff01800000 131072 131072 131072 rw— [ anon ]
00007fff02000000 131072 131072 131072 rw— [ anon ]
—————- ——- ——- ——-
total kB 10485760 5242880 5242880
# 查看slab内存
[root@perf-server ~]# slabtop -o
Active / Total Objects (% used) : 12345678 / 15000000 (82.3%)
Active / Total Slabs (% used) : 1234567 / 1500000 (82.3%)
Active / Total Caches (% used) : 123 / 150 (82.0%)
Active / Total Size (% used) : 2048.00M / 2500.00M (81.9%)
Minimum / Average / Maximum Object : 0.01K / 0.19K / 8.00K
OBJS ACTIVE USE OBJ SIZE SLABS OBJ/SLAB CACHE SIZE NAME
1234567 1234567 100% 0.19K 123457 10 493828K dentry
1000000 1000000 100% 0.10K 50000 20 200000K buffer_head
500000 500000 100% 1.00K 125000 4 500000K ext4_inode_cache
200000 200000 100% 0.50K 25000 8 100000K radix_tree_node
Part02-系统优化配置
2.1 内核参数优化
[root@perf-server ~]# cat > /etc/sysctl.d/99-performance.conf << 'EOF' # 网络优化 net.core.somaxconn = 65535 net.core.netdev_max_backlog = 65535 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_max_syn_backlog = 65535 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 300 net.ipv4.tcp_keepalive_probes = 5 net.ipv4.tcp_keepalive_intvl = 15 net.ipv4.tcp_max_tw_buckets = 65535 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_syncookies = 1 # 内存优化 vm.swappiness = 10 vm.dirty_ratio = 80 vm.dirty_background_ratio = 5 vm.overcommit_memory = 1 vm.max_map_count = 262144 # 文件系统优化 fs.file-max = 2097152 fs.nr_open = 2097152 # 内核优化 kernel.pid_max = 4194303 kernel.threads-max = 4194303 EOF # 应用内核参数 [root@perf-server ~]# sysctl -p /etc/sysctl.d/99-performance.conf net.core.somaxconn = 65535 net.core.netdev_max_backlog = 65535 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_max_syn_backlog = 65535 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 300 net.ipv4.tcp_keepalive_probes = 5 net.ipv4.tcp_keepalive_intvl = 15 net.ipv4.tcp_max_tw_buckets = 65535 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_syncookies = 1 vm.swappiness = 10 vm.dirty_ratio = 80 vm.dirty_background_ratio = 5 vm.overcommit_memory = 1 vm.max_map_count = 262144 fs.file-max = 2097152 fs.nr_open = 2097152 kernel.pid_max = 4194303 kernel.threads-max = 4194303 # 验证参数 [root@perf-server ~]# sysctl -a | grep -E "somaxconn|swappiness|file-max" fs.file-max = 2097152 net.core.somaxconn = 65535 vm.swappiness = 10 # 配置资源限制 [root@perf-server ~]# cat > /etc/security/limits.d/99-performance.conf << 'EOF' * soft nofile 65535 * hard nofile 65535 * soft nproc 65535 * hard nproc 65535 * soft memlock unlimited * hard memlock unlimited root soft nofile 65535 root hard nofile 65535 root soft nproc 65535 root hard nproc 65535 EOF # 验证限制 [root@perf-server ~]# ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 65535 max locked memory (kbytes, -l) unlimited max memory size (kbytes, -m) unlimited open files (-n) 65535 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 65535 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
2.2 磁盘I/O优化
[root@perf-server ~]# cat /sys/block/sda/queue/scheduler
[mq-deadline] none
# 修改I/O调度器
[root@perf-server ~]# echo none > /sys/block/sda/queue/scheduler
[root@perf-server ~]# cat /sys/block/sda/queue/scheduler
[none] mq-deadline
# 查看磁盘I/O统计
[root@perf-server ~]# iostat -x 1 5
Linux 5.14.0-284.11.1.el9_2.x86_64 (perf-server) 04/04/2026 _x86_64_ (64 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
25.00 0.00 10.00 5.00 0.00 60.00
Device r/s w/s rMB/s wMB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util
sda 100.00 200.00 5.00 10.00 10.00 20.00 9.09 9.09 5.00 10.00 2.50 51200 51200 3.33 100.00
sdb 50.00 100.00 2.50 5.00 5.00 10.00 9.09 9.09 5.00 10.00 1.25 51200 51200 6.67 100.00
# 配置磁盘参数
[root@perf-server ~]# cat > /etc/udev/rules.d/99-disk.rules << 'EOF'
ACTION=="add|change", KERNEL=="sda", ATTR{queue/scheduler}="none"
ACTION=="add|change", KERNEL=="sdb", ATTR{queue/scheduler}="none"
ACTION=="add|change", KERNEL=="sda", ATTR{queue/nr_requests}="256"
ACTION=="add|change", KERNEL=="sda", ATTR{queue/read_ahead_kb}="4096"
EOF
# 应用udev规则
[root@perf-server ~]# udevadm control --reload-rules
[root@perf-server ~]# udevadm trigger
# 配置文件系统挂载选项
[root@perf-server ~]# cat /etc/fstab
/dev/mapper/vg_root-lv_root / xfs defaults,noatime,nodiratime 0 0
UUID=12345678-90ab-cdef-1234-567890abcdef /boot xfs defaults,noatime,nodiratime 0 0
/dev/mapper/vg_root-lv_var /var xfs defaults,noatime,nodiratime 0 0
/dev/mapper/vg_data-lv_data /data xfs defaults,noatime,nodiratime,logbufs=8,logbsize=256k 0 0
# 重新挂载文件系统
[root@perf-server ~]# mount -o remount /
[root@perf-server ~]# mount -o remount /var
[root@perf-server ~]# mount -o remount /data
# 验证挂载选项
[root@perf-server ~]# mount | grep -E "/ |/var |/data "
/dev/mapper/vg_root-lv_root on / type xfs (rw,noatime,学习交流加群风哥微信: itpux-comnodiratime,attr2,inode64,logbufs=8,logbsize=256k)
/dev/mapper/vg_root-lv_var on /var type xfs (rw,noatime,nodiratime,attr2,inode64,logbufs=8,logbsize=256k)
/dev/mapper/vg_data-lv_data on /data type xfs (rw,noatime,nodi学习交流加群风哥QQ113257174ratime,attr2,inode64,logbufs=8,logbsize=256k)
Part03-应用性能优化
3.1 数据库优化
[root@perf-server ~]# cat > /etc/my.cnf.d/performance.cnf << 'EOF' [mysqld] # 缓冲池配置 innodb_buffer_pool_size = 4G innodb_buffer_pool_instances = 4 innodb_log_file_size = 1G innodb_log_buffer_size = 256M # I/O配置 innodb_io_capacity = 2000 innodb_io_capacity_max = 4000 innodb_flush_method = O_DIRECT innodb_flush_log_at_trx_commit = 2 # 并发配置 innodb_thread_concurrency = 0 innodb_read_io_threads = 8 innodb_write_io_threads = 8 innodb_purge_threads = 4 # 查询缓存 query_cache_type = 0 query_cache_size = 0 # 连接配置 max_connections = 1000 max_connect_errors = 1000 wait_timeout = 600 interactive_timeout = 600 # 临时表配置 tmp_table_size = 256M max_heap_table_size = 256M # 排序配置 sort_buffer_size = 4M join_buffer_size = 4M read_buffer_size = 2M read_rnd_buffer_size = 2M # 慢查询日志 slow_query_log = 1 slow_query_log_file = /var/log/mysql/slow.log long_query_time = 2 log_queries_not_using_indexes = 1 EOF # 重启MySQL [root@perf-server ~]# systemctl restart mysqld # 验证配置 [root@perf-server ~]# mysql -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';" +-------------------------+------------+ | Variable_name | Value | +-------------------------+------------+ | innodb_buffer_pool_size | 4294967296 | +-------------------------+------------+ # 分析慢查询 [root@perf-server ~]# mysqldumpslow -s t /var/log/mysql/slow.log | head -10 Reading mysql slow query log from /var/log/mysql/slow.log Count: 100 Time=5.00s (500s) Lock=0.00s (0s) Rows=1000.0 (100000), root[root]@localhost SELECT * FROM users WHERE name LIKE 'S' Count: 50 Time=3.更多视频教程www.fgedu.net.cn00s (150s) Lock=0.00s (0s) Rows=500.0 (25000), root[root]@localhost SELECT * FROM orders WHERE status = 'S' # 优化查询 [root@perf-server ~]# mysql -e "EXPLAIN SELECT * FROM users WHERE name LIKE 'John%';" +----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+ | 1 | SIMPLE | users | NULL | ALL | NULL | NULL | NULL | NULL | 100000 | 11.11 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+ # 创建索引 [root@perf-server ~]# mysql -e "CREATE INDEX idx_name ON users(name);" Query OK, 0 rows affected (5.00 sec) Records: 0 Duplicates: 0 Warnings: 0 # 验证优化效果 [root@perf-server ~]# mysql -e "EXPLAIN SELECT * FROM users WHERE name LIKE 'John%';" +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+ | 1 | SIMPLE | users | NULL | range | idx_name | idx_name | 202 | NULL | 10 | 100.00 | Using index condition | +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
- 分析系统瓶颈
- 优化内核参数
- 调整磁盘I/O配置
- 优化应用配置
- 持续监控性能
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
