调优只针对系统内核参数,相比程序方面的调优占比也很大,系统瓶颈不存在时,那所有瓶颈都来自程序或数据库
内核参数配置
fs.file-max = 2000000
fs.nr_open = 2000000
fs.aio-max-nr = 1048576
net.ipv4.tcp_max_syn_backlog = 262144
net.core.somaxconn = 1048576
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_abort_on_overflow = 0
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_max_tw_buckets = 1440000
net.core.wmem_max = 16777216
net.core.rmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 87380 16777216
net.core.optmem_max = 40960
net.ipv4.tcp_mem = 786432 1048576 1572864
kernel.msgmnb = 131072
kernel.msgmax = 131072
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_mtu_probing = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_adv_win_scale = 1
net.ipv4.tcp_rfc1337 = 1
net.ipv4.tcp_congestion_control = cubic
net.ipv4.tcp_notsent_lowat = 16384
net.ipv4.ip_forward = 1
net.netfilter.nf_conntrack_max = 3000000
net.netfilter.nf_conntrack_buckets = 750000
net.netfilter.nf_conntrack_tcp_timeout_established = 1800
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 30
net.ipv4.conf.all.rp_filter = 0
vm.swappiness = 10
vm.max_map_count = 262144
kernel.panic = 5
默认对比修改
默认内核参数与优化后参数对比表
此表格展示了Linux系统默认内核参数值与融合优化后的高并发参数值的对比。
文件描述符和端口范围
参数 |
默认值 |
优化值 |
变化比例 |
影响 |
fs.file-max |
~65536 |
2000000 |
30.5倍 |
大幅提高系统可打开的文件描述符总数 |
fs.nr_open |
1048576 |
2000000 |
1.9倍 |
提高单个进程可打开的文件描述符数量 |
fs.aio-max-nr |
65536 |
1048576 |
16倍 |
大幅提高异步I/O请求上限 |
net.ipv4.ip_local_port_range |
32768-60999 |
1024-65535 |
2.1倍 |
显著扩大可用本地端口范围 |
TCP连接建立和释放
参数 |
默认值 |
优化值 |
变化比例 |
影响 |
net.ipv4.tcp_max_syn_backlog |
1024 |
262144 |
256倍 |
极大提高SYN半连接队列容量 |
net.core.somaxconn |
128 |
1048576 |
8192倍 |
极大提高已完成连接队列容量 |
net.ipv4.tcp_fin_timeout |
60 |
15 |
75%减少 |
大幅加速FIN_WAIT连接的释放 |
net.ipv4.tcp_syncookies |
1 |
1 |
不变 |
保持SYN洪水攻击防护 |
net.ipv4.tcp_fastopen |
0 |
3 |
启用 |
加速TCP连接建立过程 |
net.ipv4.tcp_synack_retries |
5 |
2 |
60%减少 |
减少服务器对SYN+ACK的重试 |
net.ipv4.tcp_syn_retries |
6 |
2 |
67%减少 |
减少客户端对SYN的重试 |
net.ipv4.tcp_tw_reuse |
0 |
1 |
启用 |
允许TIME_WAIT套接字重用 |
net.ipv4.tcp_abort_on_overflow |
0 |
0 |
不变 |
连接队列满时不中断连接 |
TCP连接保持和超时
参数 |
默认值 |
优化值 |
变化比例 |
影响 |
net.ipv4.tcp_keepalive_time |
7200 |
600 |
92%减少 |
大幅减少检测失效连接的等待时间 |
net.ipv4.tcp_keepalive_intvl |
75 |
30 |
60%减少 |
加速keepalive探测间隔 |
net.ipv4.tcp_keepalive_probes |
9 |
3 |
67%减少 |
减少确认连接失效前的探测次数 |
net.ipv4.tcp_max_tw_buckets |
180000 |
1440000 |
8倍 |
大幅提高TIME_WAIT状态连接的容量 |
内存和缓冲区设置
参数 |
默认值 |
优化值 |
变化比例 |
影响 |
net.core.wmem_max |
212992 |
16777216 |
78.8倍 |
极大提高写缓冲区上限 |
net.core.rmem_max |
212992 |
16777216 |
78.8倍 |
极大提高读缓冲区上限 |
net.ipv4.tcp_rmem |
4096 87380 6291456 |
4096 87380 16777216 |
最大值增加2.7倍 |
提高TCP读缓冲区最大值 |
net.ipv4.tcp_wmem |
4096 16384 4194304 |
4096 87380 16777216 |
最大值增加4倍 |
提高TCP写缓冲区最大值 |
net.core.optmem_max |
20480 |
40960 |
2倍 |
提高套接字辅助缓冲区上限 |
net.ipv4.tcp_mem |
根据内存自动计算 |
786432 1048576 1572864 |
显式设置 |
精确控制TCP内存使用 |
kernel.msgmnb |
65536 |
131072 |
2倍 |
提高消息队列的最大字节数 |
kernel.msgmax |
65536 |
131072 |
2倍 |
提高单个消息的最大字节数 |
网络性能和拥塞控制
参数 |
默认值 |
优化值 |
变化比例 |
影响 |
net.core.netdev_max_backlog |
1000 |
262144 |
262倍 |
极大提高网络设备接收队列容量 |
net.ipv4.tcp_slow_start_after_idle |
1 |
0 |
禁用 |
防止空闲连接重启时的性能下降 |
net.ipv4.tcp_mtu_probing |
0 |
1 |
启用 |
提高网络路径MTU探测能力 |
net.ipv4.tcp_sack |
1 |
1 |
不变 |
保持选择性确认功能 |
net.ipv4.tcp_window_scaling |
1 |
1 |
不变 |
保持窗口缩放功能 |
net.ipv4.tcp_adv_win_scale |
系统计算 |
1 |
显式设置 |
提供更精确的窗口缩放控制 |
net.ipv4.tcp_rfc1337 |
0 |
1 |
启用 |
防止TIME_WAIT状态的连接被攻击 |
net.ipv4.tcp_congestion_control |
cubic |
cubic |
不变 |
保持默认拥塞控制算法 |
net.ipv4.tcp_notsent_lowat |
未设置 |
16384 |
启用 |
减少内存使用和延迟 |
net.ipv4.ip_forward |
0 |
1 |
启用 |
允许系统转发IP包 |
连接跟踪相关
参数 |
默认值 |
优化值 |
变化比例 |
影响 |
net.netfilter.nf_conntrack_max |
~65536 |
3000000 |
45.8倍 |
极大提高连接跟踪表容量 |
net.netfilter.nf_conntrack_buckets |
自动计算 (max/4) |
750000 |
显式设置 |
优化连接跟踪哈希表性能 |
net.netfilter.nf_conntrack_tcp_timeout_established |
432000 (5天) |
1800 (30分钟) |
99.6%减少 |
大幅减少已建立连接的超时时间 |
net.netfilter.nf_conntrack_tcp_timeout_time_wait |
120 |
30 |
75%减少 |
加速TIME_WAIT状态连接的释放 |
net.ipv4.conf.all.rp_filter |
1 |
0 |
禁用 |
关闭严格的反向路径过滤 |
其他系统限制
参数 |
默认值 |
优化值 |
变化比例 |
影响 |
vm.swappiness |
60 |
10 |
83%减少 |
大幅减少内存交换倾向 |
vm.max_map_count |
65530 |
262144 |
4倍 |
提高内存映射区域数量上限 |
kernel.panic |
0 |
5 |
启用自动重启 |
系统崩溃后5秒自动重启 |
ulimit调整
还需要调优ulimit数量,一般情况下都设置为65535,这里我的服务器基础性能高,所以给更大
vim /etc/security/limits.conf
* soft nofile 1000000
* hard nofile 1000000
root soft nofile 1000000
root hard nofile 1000000