linux 内核参数|调整linux内核参数,让服务器承受更高的负载

时间:2019-11-17  来源:linux  阅读:

linux内核的一些参数是优化linux性能必不可少的,管理linux服务器必须得学会呀。


ulimit设置

ulimit -n 要调整为100000甚至更大。 命令行下执行 ulimit -n 100000即可修改。如果不能修改,需要设置 /etc/security/limits.conf,加入

* soft nofile 262140
* hard nofile 262140
root soft nofile 262140
root hard nofile 262140
* soft core unlimited
* hard core unlimited
root soft core unlimited
root hard core unlimited

内核设置
net.unix.max_dgram_qlen = 100
swoole使用unix socket dgram来做进程间通信,如果请求量很大,需要调整此参数。系统默认为10,可以设置为100或者更大。
或者增加worker进程的数量,减少单个worker进程分配的请求量。

net.core.wmem_max
修改此参数增加socket缓存区的内存大小

net.ipv4.tcp_mem  =   379008       505344  758016
net.ipv4.tcp_wmem = 4096        16384   4194304
net.ipv4.tcp_rmem = 4096          87380   4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_tw_reuse
是否socket reuse,此函数的作用是Server重启时可以快速重新使用监听的端口。如果没有设置此参数,会导致server重启时发生端口未及时释放而启动失败

net.ipv4.tcp_tw_recycle
使用socket快速回收,短连接Server需要开启此参数
消息队列设置

当使用消息队列作为进程间通信方式时,需要调整此内核参数

    kernel.msgmnb = 4203520,消息队列的最大字节数
    kernel.msgmni = 64,最多允许创建多少个消息队列
    kernel.msgmax = 8192,消息队列单条数据最大的长度

FreeBSD/MacOS

    sysctl -w net.local.dgram.maxdgram=8192
    sysctl -w net.local.dgram.recvspace=200000 修改Unix Socket的buffer区尺寸

开启CoreDump

设置内核参数
kernel.core_pattern = /data/core_files/core-%e-%p-%t
通过ulimit -c命令查看当前coredump文件的限制
ulimit -c
如果为0,需要修改/etc/security/limits.conf,进行limit设置。

    开启core-dump后,一旦程序发生异常,会将进程导出到文件。对于调查程序问题有很大的帮助

其他重要配置

    net.ipv4.tcp_syncookies=1
    net.ipv4.tcp_max_syn_backlog=81920
    net.ipv4.tcp_synack_retries=3
    net.ipv4.tcp_syn_retries=3
    net.ipv4.tcp_fin_timeout = 30
    net.ipv4.tcp_keepalive_time = 300
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.ip_local_port_range = 20000 65000
    net.ipv4.tcp_max_tw_buckets = 200000
    net.ipv4.route.max_size = 5242880

查看配置是否生效

如:修改net.unix.max_dgram_qlen = 100后,通过

cat /proc/sys/net/unix/max_dgram_qlen

如果修改成功,这里是新设置的值。


补充 高负载linux服务器的内核调优 及参数解释

高负载linux服务器的内核调优
vi /etc/sysctl.conf,修改内核参数:
kernel.shmall = 268435456
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000
net.ipv4.tcp_mem = 786432 1048576 1572864
net.core.wmem_max = 873200
net.core.rmem_max = 873200
net.ipv4.tcp_wmem = 8192 436600 873200
net.ipv4.tcp_rmem = 32768 436600 873200
net.core.somaxconn = 256
net.core.netdev_max_backlog = 1000
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_retries2 = 5
net.ipv4.tcp_keepalive_time = 500
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.conf.lo.arp_ignore = 0
net.ipv4.conf.lo.arp_announce = 0
net.ipv4.conf.all.arp_ignore = 0
net.ipv4.conf.all.arp_announce = 0

几个解释:
net.ipv4.tcp_syncookies = 1
#表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1
#表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1
#表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
net.ipv4.tcp_fin_timeout = 30
#表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。
net.ipv4.tcp_keepalive_time = 1200
#表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,改为20分钟。
net.ipv4.ip_local_port_range = 1024    65000
#表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为1024到65000。
net.ipv4.tcp_max_tw_buckets = 5000
#表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,
#TIME_WAIT套接字将立刻被清除并打印警告信息。默认为180000,改为5000。
#对于Apache、Nginx等服务器,上几行的参数可以很好地减少TIME_WAIT套接字数量,
#但是对于Squid,效果却不大。此项参数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死


修改完成后运行以下命令使参数生效
/sbin/sysctl -p

linux 内核参数|调整linux内核参数,让服务器承受更高的负载

http://m.bbyears.com/caozuoxitong/79830.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新