zoukankan      html  css  js  c++  java
  • Linux内核参数优化

    修改操作系统内核参数
    
    编辑/etc/sysctl.conf,增加下列内容
    向内核配置文件中写入:
    echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
    
    
    
    
    kernel.shmmni=1024
    kernel.shmmax=4294967296
    kernel.shmall=2097152
    kernel.sem=250 256000 32 1024
    kernel.msgmni=4096
    kernel.msgmax=65536
    kernel.msgmnb=65536
    vm.dirty_background_ratio = 5
    vm.dirty_ratio = 10
    vm.swappiness = 5
    vm.overcommit_memory = 0
    net.ipv4.tcp_keepalive_time = 800
    net.ipv4.tcp_keepalive_intvl = 30
    net.ipv4.tcp_keepalive_probes = 10
    net.ipv6.conf.all.disable_ipv6 = 1
    net.ipv6.conf.default.disable_ipv6 = 1
    net.ipv6.conf.lo.disable_ipv6 = 1
    net.ipv4.neigh.default.gc_stale_time = 120
    net.ipv4.conf.all.rp_filter = 0
    net.ipv4.conf.default.rp_filter = 0
    net.ipv4.conf.default.arp_announce = 2
    net.ipv4.conf.lo.arp_announce = 2
    net.ipv4.conf.all.arp_announce = 2
    net.ipv4.ip_forward = 1
    net.ipv4.tcp_max_tw_buckets = 5000
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_max_syn_backlog = 8192
    net.ipv4.tcp_synack_retries = 2
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    fs.inotify.max_user_watches=89100
    fs.file-max=102400
    fs.nr_open=102400
    net.bridge.bridge-nf-call-arptables = 1
    
    
    
    修改完毕后使用sysctl -p 生效。
    
    
    
    
    ————————————————————————————————————
    .Linux 下高并发系统内核优化
    1) 优化 Linux 文件打开最大数:
    vi  /etc/security/limits.conf * soft nproc 65535 * hard nproc 65535 * soft nofile 65535 * hard nofile 65535 为了防止失控的进程破坏系统的性能,Unix 和 Linux 跟踪进程使用的大部分资源,允许 用户和系统管理员使用对进程的资源限制,设置的限制有两种: 硬限制和软限制: hard 硬限制是可以在任何时候任何进程中设置  但硬限制只能由超级用户修改。 soft 软限制是内核实际执行的限制,任何进程都可以将软限制设置为任意小于等于对 进程限制的硬限制的值,(noproc)最大线程数和(nofile)文件数。
    2) 内核参数的优化
    vi /etc/sysctl.conf net.ipv4.tcp_max_tw_buckets = 6000 timewait 的数量,默认是 180000。 net.ipv4.ip_local_port_range = 1024    65000 允许系统打开的端口范围。 net.ipv4.tcp_tw_recycle = 1 启用 timewait 快速回收。 net.ipv4.tcp_tw_reuse = 1 开启重用。允许将 TIME-WAIT sockets 重新用于新的 TCP 连接。 net.ipv4.tcp_syncookies = 1 开启 SYN Cookies,当出现 SYN 等待队列溢出时,启用 cookies 来处理。 net.core.somaxconn = 262144 web 应用中 listen 函数的 backlog 默认会给我们内核参数的 net.core.somaxconn 限制到 128,而 nginx 定义的 NGX_LISTEN_BACKLOG 默认为 511,所以有必要调整这个值。 net.core.netdev_max_backlog = 262144 每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最 大数目。 net.ipv4.tcp_max_orphans = 262144 系统中最多有多少个 TCP 套接字不被关联到任何一个用户文件句柄上。如果超过这个数字, 孤儿连接将即刻被复位并打印出警告信息。这个限制仅仅是为了防止简单的 DoS 攻击,不能 过分依靠它或者人为地减小这个值,更应该增加这个值(如果增加了内存之后)。 net.ipv4.tcp_max_syn_backlog = 262144 记录的那些尚未收到客户端确认信息的连接请求的最大值。对于有 128M 内存的系统而言, 缺省值是 1024,小内存的系统则是 128。 net.ipv4.tcp_timestamps = 0 时间戳可以避免序列号的卷绕。一个 1Gbps 的链路肯定会遇到以前用过的序列号。时间戳能 够让内核接受这种“异常”的数据包。这里需要将其关掉。
    net.ipv4.tcp_synack_retries = 1 为了打开对端的连接,内核需要发送一个 SYN 并附带一个回应前面一个 SYN 的 ACK。也就是 所谓三次握手中的第二次握手。这个设置决定了内核放弃连接之前发送 SYN+ACK 包的数量。 net.ipv4.tcp_syn_retries = 1 在内核放弃建立连接之前发送 SYN 包的数量。 net.ipv4.tcp_fin_timeout = 1 如果套接字由本端要求关闭,这个参数决定了它保持在 FIN-WAIT-2 状态的时间。对端可以 出错并永远不关闭连接,甚至意外当机。缺省值是 60 秒。2.2 内核的通常值是 180 秒,你 可以按这个设置,但要记住的是,即使你的机器是一个轻载的 WEB 服务器,也有因为大量的 死套接字而内存溢出的风险,FIN- WAIT-2 的危险性比 FIN-WAIT-1 要小,因为它最多只能 吃掉 1.5K 内存,但是它们的生存期长些。 net.ipv4.tcp_keepalive_time = 30 当 keepalive 起用的时候,TCP 发送 keepalive 消息的频度。缺省是 2 小时。 完整的内核优化脚本:
    net.ipv4.ip_forward = 0
    net.ipv4.conf.default.rp_filter = 1
    net.ipv4.conf.default.accept_source_route = 0
    kernel.sysrq = 0
    kernel.core_uses_pid = 1
    net.ipv4.tcp_syncookies = 1
    kernel.msgmnb = 65536
    kernel.msgmax = 65536
    kernel.shmmax = 68719476736
    kernel.shmall = 4294967296
    net.ipv4.tcp_max_tw_buckets = 10000
    net.ipv4.tcp_sack = 1
    net.ipv4.tcp_window_scaling = 1
    net.ipv4.tcp_rmem = 4096        87380  4194304
    net.ipv4.tcp_wmem = 4096        16384   4194304
    net.core.wmem_default = 8388608
    net.core.rmem_default = 8388608
    net.core.rmem_max = 16777216
    net.core.wmem_max = 16777216
    net.core.netdev_max_backlog = 262144
    net.core.somaxconn = 262144
    net.ipv4.tcp_max_orphans = 3276800
    net.ipv4.tcp_max_syn_backlog = 262144
    net.ipv4.tcp_timestamps = 0
    net.ipv4.tcp_synack_retries = 1
    net.ipv4.tcp_syn_retries = 1
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_mem = 94500000 915000000 927000000
    net.ipv4.tcp_fin_timeout = 1
    net.ipv4.tcp_keepalive_time = 30
    net.ipv4.ip_local_port_range = 1024    65530
    net.ipv4.icmp_echo_ignore_all = 1
    3) 常见内核报错解析
    ➢ net.ipv4.tcp_max_tw_buckets 错误:
    Sep 23 04:45:54 localhost kernel: possible SYN flooding on port 80. Sending cookies. Sep 23 04:45:55 localhost kernel: TCP: time wait bucket table overflow
    Sep 23 04:45:55 localhost kernel: TCP: time wait bucket table overflow
    Sep 23 04:45:55 localhost kernel: TCP: time wait bucket table overflow
    Sep 23 04:45:55 localhost kernel: TCP: time wait bucket table overflow
    Sep 23 04:45:55 localhost kernel: TCP: time wait bucket table overflow
    Sep 23 04:45:55 localhost kernel: TCP: time wait bucket table overflow 如上错误是由于 net.ipv4.tcp_max_tw_buckets 设置过小导致,如果内核有如上错误,我们需要 增加 net.ipv4.tcp_max_tw_buckets 的值。
    ➢ Too many open files 错误:
    如果后台报错,大量的 too many open files 错误,一般主要是 JAVA 应用出现这类错误比较多。我们 需要设置内核打开文件最大数。 ulimit -SHn 51200 临时生效,如果想永久生效,需要写入到系统内核里面: vi  /etc/security/limits.conf * soft nproc 65535 * hard nproc 65535 * soft nofile 65535 * hard nofile 65535 然后 exit 退出,重新登录即生效,也可以写在/etc/profile 文件里。

    作者:tigergao
    功不唐捐 玉汝于成
  • 相关阅读:
    青魔法圣堂法术 Django的技术栈(持续更新)
    青魔法圣堂法术 Django REST framework (DRF) 框架(持续更新)
    Python无法卸载的解决办法
    Django开发social-auth-app-django 第三方登陆
    【转载】青魔法圣堂法术Django项目知识点汇总
    基于session 的springMvc 国际化
    java导出生成csv文件
    mybatis + log4j 打印mybatis的sql
    spring Mvc + Mybatis 中使用junit
    spring官网项目
  • 原文地址:https://www.cnblogs.com/tigergaonotes/p/14483335.html
Copyright © 2011-2022 走看看