Linux网卡调优篇-禁用ipv6与优化socket缓冲区大小node
做者:尹正杰缓存
版权声明:原创做品,谢绝转载!不然将追究法律责任。服务器
通常在内网环境中,咱们几乎是用不到IPV6,所以咱们没有必要把多不用的功能开起来从而浪费没必要要的资源。默认状况下,系统内核没有针对快速的的大流量网络传输进行优化,因此对于应用程序来讲,通常须要对Linux系统的网络栈进行调优,以实现对大流量的支持。网络
一.禁用IPv6图解app
1>.使用ifconfig查看网卡信息(若是没有该工具,直接yum按照响应的工具包便可:[root@node118 ~]# yum -y install net-tools )socket
2>. 禁用IPV6的步骤tcp
[root@node118 ~]# echo "net.ipv6.conf.all.disable_ipv6=1" >> /etc/sysctl.conf [root@node118 ~]# [root@node118 ~]# echo "NETWORKING_IPV6=no" >> /etc/sysconfig/network [root@node118 ~]# [root@node118 ~]# echo NETWORKING_IPV6=no >> /etc/sysconfig/network-scripts/ifcfg-bond0 [root@node118 ~]# [root@node118 ~]# sysctl -p net.ipv6.conf.all.disable_ipv6 = 1 [root@node118 ~]#
3>.执行上述命令后,再次查看bond0的网卡配置信息 工具
二.socket读写缓冲区调优性能
实际上,调整Kafka的网络配置与其余的配置是同样的。首先能够对分配给socket读写缓冲区的内存大小做出调整,这样能够显著提高网络的传输性能。优化
1>.默认的TCP数据发送窗口大小(字节)。
[root@yinzhengjie ~]# cat /proc/sys/net/core/wmem_default #对应net.core.wmem_default这个参数 212992 [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.core.wmem_default net.core.wmem_default = 212992 [root@yinzhengjie ~]# [root@yinzhengjie ~]# [root@yinzhengjie ~]# echo "net.core.wmem_default=256960" >> /etc/sysctl.conf #我这里设置发送大小为256KB [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -p vm.swappiness = 1 vm.dirty_background_ratio = 5 vm.dirty_ratio = 80 net.ipv6.conf.all.disable_ipv6 = 1 net.core.wmem_default = 256960 [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.core.wmem_default net.core.wmem_default = 256960 [root@yinzhengjie ~]# [root@yinzhengjie ~]#
2>.默认的TCP数据接收窗口大小(字节)。
[root@yinzhengjie ~]# cat /proc/sys/net/core/rmem_default #对应的参数是net.core.rmem_default 212992 [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.core.rmem_default net.core.rmem_default = 212992 [root@yinzhengjie ~]# [root@yinzhengjie ~]# echo "net.core.rmem_default=256960" >> /etc/sysctl.conf #我这里接受大小设置的是256KB [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -p vm.swappiness = 1 vm.dirty_background_ratio = 5 vm.dirty_ratio = 80 net.ipv6.conf.all.disable_ipv6 = 1 net.core.wmem_default = 256960 net.core.rmem_default = 256960 [root@yinzhengjie ~]# [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.core.rmem_default net.core.rmem_default = 256960 [root@yinzhengjie ~]#
3>.最大的TCP数据发送窗口(字节)。
[root@yinzhengjie ~]# cat /proc/sys/net/core/wmem_max #对应的参数是:net.core.wmem_max 212992 [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.core.wmem_max net.core.wmem_max = 212992 [root@yinzhengjie ~]# [root@yinzhengjie ~]# [root@yinzhengjie ~]# echo "net.core.wmem_max=2097152" >> /etc/sysctl.conf #我这里将最大的TCP数据发送缓冲区最大值为2M [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -p vm.swappiness = 1 vm.dirty_background_ratio = 5 vm.dirty_ratio = 80 net.ipv6.conf.all.disable_ipv6 = 1 net.core.wmem_default = 256960 net.core.rmem_default = 256960 net.core.wmem_max = 2097152 [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.core.wmem_max net.core.wmem_max = 2097152 [root@yinzhengjie ~]#
4>.最大的TCP数据接收窗口(字节)。
[root@yinzhengjie ~]# cat /proc/sys/net/core/rmem_max #对应参数是:net.core.rmem_max 212992 [root@yinzhengjie ~]# [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.core.rmem_max net.core.rmem_max = 212992 [root@yinzhengjie ~]# [root@yinzhengjie ~]# [root@yinzhengjie ~]# echo "net.core.rmem_max=2097152" >> /etc/sysctl.conf #我这里将TCP数据接受缓冲区的最大值设置为2M [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -p vm.swappiness = 1 vm.dirty_background_ratio = 5 vm.dirty_ratio = 80 net.ipv6.conf.all.disable_ipv6 = 1 net.core.wmem_default = 256960 net.core.rmem_default = 256960 net.core.wmem_max = 2097152 net.core.rmem_max = 2097152 [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.core.rmem_max net.core.rmem_max = 2097152 [root@yinzhengjie ~]#
舒适提示:最大值并不意味着每一个socket必定要有这么大大缓冲空间,只是说在必要大状况下才会达到这个值。
三.TCP socket的读写缓冲区调优
除了设置socket外,咱们还须要设置TCP socket的读写缓冲区。
1>.为自动调优定义socket使用的发送内存(写)。
[root@yinzhengjie ~]# cat /proc/sys/net/ipv4/tcp_wmem #对应参数:net.ipv4.tcp_wmem 4096 16384 4194304 [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.ipv4.tcp_wmem net.ipv4.tcp_wmem = 4096 16384 4194304 #第一个值是为socket发送缓冲区分配的最少字节数;第二个值是默认值(该值会被wmem_default覆盖),缓冲区在系统负载不重的状况下能够增加到这个值;第三个值是发送缓冲区空间的最大字节数(该值会被wmem_max覆盖)。 [root@yinzhengjie ~]# [root@yinzhengjie ~]# [root@yinzhengjie ~]# echo "net.ipv4.tcp_wmem=8760 256960 4088000" >> /etc/sysctl.conf #根据服务接受的实际状况,可能须要设置更高的最大值,为网络连接提供更大的缓存空间。 [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -p vm.swappiness = 1 vm.dirty_background_ratio = 5 vm.dirty_ratio = 80 net.ipv6.conf.all.disable_ipv6 = 1 net.core.wmem_default = 256960 net.core.rmem_default = 256960 net.core.wmem_max = 2097152 net.core.rmem_max = 2097152 net.ipv4.tcp_wmem = 8760 256960 4088000 [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.ipv4.tcp_wmem net.ipv4.tcp_wmem = 8760 256960 4088000 [root@yinzhengjie ~]#
2>. 为自动调优定义socket使用的接受内存(读)。
[root@yinzhengjie ~]# cat /proc/sys/net/ipv4/tcp_rmem #对应参数为:net.ipv4.tcp_rmem 4096 87380 6291456 [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.ipv4.tcp_rmem net.ipv4.tcp_rmem = 4096 87380 6291456 #第一个值是为socket接收缓冲区分配的最少字节数;第二个值是默认值(该值会被rmem_default覆盖),缓冲区在系统负载不重的状况下能够增加到这个值;第三个值是接收缓冲区空间的最大字节数(该值会被rmem_max覆盖)。 [root@yinzhengjie ~]# [root@yinzhengjie ~]# echo "net.ipv4.tcp_rmem=8760 256960 4088000" >> /etc/sysctl.conf [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -p vm.swappiness = 1 vm.dirty_background_ratio = 5 vm.dirty_ratio = 80 net.ipv6.conf.all.disable_ipv6 = 1 net.core.wmem_default = 256960 net.core.rmem_default = 256960 net.core.wmem_max = 2097152 net.core.rmem_max = 2097152 net.ipv4.tcp_wmem = 8760 256960 4088000 net.ipv4.tcp_rmem = 8760 256960 4088000 [root@yinzhengjie ~]# [root@yinzhengjie ~]# sysctl -q net.ipv4.tcp_rmem net.ipv4.tcp_rmem = 8760 256960 4088000 [root@yinzhengjie ~]#
根据kafka服务器接受不了流量的实际状况,可能须要设置更高的最大值,为网络连接提供更大的缓冲空间。
四.其余网络参数调优
[root@yinzhengjie ~]# echo "net.ipv4.tcp_window_scaling=1" >> /etc/sysctl.conf # 启用RFC 1323定义的window scaling,要支持超过64KB的TCP窗口,必须启用该值(1表示启用),TCP窗口最大至1GB,TCP链接双方都启用时才生效。 [root@yinzhengjie ~]# echo "net.ipv4.tcp_max_syn_backlog=2048" >> /etc/sysctl.conf #每个链接请求(SYN报文)都须要排队,直至本地服务器接收,该变量就是控制每一个端口的 TCP SYN队列长度的。若是链接请求多余该值,则请求会被丢弃。 [root@yinzhengjie ~]# echo "net.core.netdev_max_backlog=2000" >> /etc/sysctl.conf #该参数定义了当接口收到包的速率大于内核处理包的速率时,设备的输入队列中的最大报文数。 [root@yinzhengjie ~]# echo "net.ipv4.tcp_sack=1" >> /etc/sysctl.conf #管理TCP的选择性应答,容许接收端向发送端传递关于字节流中丢失的序列号,减小了段丢失时须要重传的段数目,当段丢失频繁时,sack是颇有益的。 [root@yinzhengjie ~]# echo "net.ipv4.tcp_fack=1">> /etc/sysctl.conf #启用转发应答,能够进行有选择应答(SACK)从而减小拥塞状况的发生,这个选项也应该启用。 [root@yinzhengjie ~]# echo "net.ipv4.tcp_tw_recycle=1">> /etc/sysctl.conf #可以更快地回收TIME-WAIT套接字,默认是0。 [root@yinzhengjie ~]# echo "net.ipv4.tcp_timestamps=0" >> /etc/sysctl.conf #表示再也不检查时间戳,默认值是开启的。发现tcp_timestamps默认是开启,若是再把tcp_tw_recycle设置为1,则60s内同一源ip主机的socket connect请求中的timestamp必须是递增的。也就是说服务器打开了 tcp_tw_reccycle了,就会检查时间戳,若是对方发来的包的时间戳是乱跳的或者说时间戳是滞后的,这样服务器确定不会回复,因此服务器就把带了“倒退”的时间戳的包看成是“recycle的tw链接的重传数据,不是新的请求”,因而丢掉不回包,就出现了开始说的syn不响应。