

CentOS7重新编译内核优化TIME_WAIT数量
source link: https://zhuanlan.zhihu.com/p/32386693?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

CentOS7重新编译内核优化TIME_WAIT数量
上一篇写了LB方案,上线后发现四层Nginx的TIME_WAIT居高不下,本地端口很快被用完,用完后本地将无法像后端机器建立TCP连接。一般处理TIME_WAIT数量过多的方法主要有以下几种:
- 增加local_port_range ,增大可用端口数量
- 限制tw_buckets的最大数量
- 打开recycle,此选项慎重
- 重新编译内核,修改TIME_WAIT值为5秒
本文主要来说一下最后一种方法是如何操作及效果,以CentOS 7.4为例:
1.安装所需要的模块:
yum install rpm-build.x86_64 \
redhat-rpm-config.noarch \
patchutils.x86_64 \
elfutils-libelf-devel.x86_64 \
binutils-devel.x86_64 \
hmaccalc.x86_64 \
rng-tools.x86_64
2.下载src.rpm包
wget http://vault.centos.org/7.4.1708/os/Source/SPackages/kernel-3.10.0-693.el7.src.rpm
rpm -ivh kernel-2.6.32-71.29.1.el6.src.rpm
3.创建补丁
cd ~/rpmbuild/SPECS
rpmbuild -bp kernel.spec
cd ~/rpmbuild/BUILD
cp -r kernel-3.10.0-693.el7 kernel-3.10.0-693.el7.org
cp -r kernel-3.10.0-693.el7 kernel-3.10.0-693.el7.new
#创建pgp密钥
rngd -r /dev/urandom
gpg --homedir . --batch --gen-key /root/rpmbuild/SOURCES/genkey
#修改tcp.h文件
vim kernel-3.10.0-693.el7.new/linux-3.10.0-693.el7.centos.x86_64/include/net/tcp.h
修正前
#define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
修正後
#define TCP_TIMEWAIT_LEN (5*HZ) /* how long to wait to destroy TIME-WAIT
#创建一个补丁
diff -uNrp kernel-3.10.0-693.el7.org/ kernel-3.10.0-693.el7.new/ > ../SOURCES/tcph.patch
cd ~/rpmbuild/SPECS
vi kernel.spec
修改前
# %define buildid .local
修改后
%define buildid .tcp5
# empty final patch file to facilitate testing of kernel patches
Patch00: tcph.patch ←追加
Patch999999: linux-kernel-test.patch
ApplyOptionalPatch tcph.patch ←追加
ApplyOptionalPatch linux-kernel-test.patch
5.构建新内核
rpmbuild -ba kernel.spec
6.内核更新
cd ~/rpmbuild/RPMS/x86_64
rpm -Uvh --force kernel-3.10.0-693.el7.centos.tcp5.x86_64.rpm
7. TIME_WAIT确认
1秒间隔
watch -n 1 "netstat -a | grep http"
5秒后断开
8.效果监控图

Recommend
-
78
查看所有模块:lsmod查看指定模块的详细信息:modinfo模块名动态加载模块:modprobe模块名动态卸载模块:modprobe-r模块名
-
56
好几个月之间其实已经配置过LAMPLNMP等等一些配置,以前配置都是按照晚上抄的,基本都能配置出来,现重头学想自己配置下,但是发现好多配置都忘了,中间踩了几个坑,记录下,也更彻底的学习下....../configure--prefix=/usr/local/php7--enable-fpm以fpm模式安装,...
-
47
$uname -r 输出如下: 3.10.0-514.26.2.el7.x86_64 说明当前的内核为 3.10.0 安装
-
42
CentOS7升级内核以开启TCP BBR拥塞控制算法 2019-07-31 | 环境搭建|工具使用通过ELRepo RPM repo升级内核
-
55
该文章正在审核中 如有...
-
41
本文首发于: https://blog.frytea.com/archives/546/ libvirt 是目前使用最为广泛的对 KVM 虚拟机进行管理的工具和 API 。 Libvirtd
-
3
centos7下编译时提示需要 libclang3.9 or later 解决办法 方伟的博客 j2ee技术、网络、web等,同名的人真多,我的QQ是20025404
-
9
centos7安装高版本内核 发表于 2022-02-22 更新于 2022-02-24 分类于 linux
-
5
Centos7编译安装gcc8.3.0 发表于 2020-04-23 ...
-
5
CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代码所编译而成。由于出自同样的源代码,因此有些要求高度稳定性的服务器以CentOS替代商业版的Red Hat Enterprise Linux使用。
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK