在Linux系统下tcp协议栈有个keepAlive的属性,可以主动探测socket是否可用,不过这个属性的默认值很大,全局设置可vim /etc/sysctl.conf,加上:
net.ipv4.tcp_keepalive_intvl = 20在程序中keepAlive主要用途设置如下:
#include <sys/socket.h>在http早期,每个http请求都要求打开一个tpc socket连接,并且使用一次之后就断开这个tcp连接。
使用keep-alive可以改善这种状态,即在一次TCP连接中可以持续发送多份数据而不会断开连接。通过使用keep-alive机制,可以减少tcp连接建立次数,也意味着可以减少TIME_WAIT状态连接,以此提高性能和提高httpd服务器的吞吐率(更少的tcp连接意味着更少的系统内核调用,socket的accept()和close()调用)。
Httpd守护进程,一般都提供了keep-alive timeout时间设置参数。比如nginx的keepalive_timeout,和Apache的KeepAliveTimeout。这个keepalive_timout时间值意味着:一个http产生的tcp连接在传送完最后一个响应后,还需要hold住keepalive_timeout秒后,才开始关闭这个连接。
当httpd守护进程发送完一个响应后,理应马上主动关闭相应的tcp连接,设置 keepalive_timeout后,httpd守护进程会想说:”再等等吧,看看浏览器还有没有请求过来”,这一等,便是keepalive_timeout时间。如果守护进程在这个等待的时间里,一直没有收到浏览发过来http请求,则关闭这个http连接。
/proc/sys/net/ipv4/tcp_keepalive_time
当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时。
/proc/sys/net/ipv4/tcp_keepalive_intvl
当探测没有确认时,重新发送探测的频度。缺省是75秒。
/proc/sys/net/ipv4/tcp_keepalive_probes
在认定连接失效之前,发送多少个TCP的keepalive探测包。缺省值是9。这个值乘以tcp_keepalive_intvl之后决定了,一个连接发送了keepalive之后可以有多少时间没有回应。
tcp_keepalive_time(开启keepalive的闲置时长)tcp_keepalive_intvl(keepalive探测包的发送间隔) 和tcp_keepalive_probes (如果对方不予应答,探测包的发送次数)
There are two ways to configure keepalive parameters inside the kernel via userspace commands:
procfs interface
sysctl interface
We mainly discuss how this is accomplished on the procfs interface because it's the most used, recommended and the easiest to understand. The sysctl interface, particularly regarding the sysctl(2) syscall and not the sysctl(8) tool, is only here for the purpose of background knowledge.
The procfs interface
This interface requires both sysctl and procfs to be built into the kernel, and procfs mounted somewhere in the filesystem (usually on/proc, as in the examples below). You can read the values for the actual parameters by "catting" files in /proc/sys/net/ipv4/directory:
# cat /proc/sys/net/ipv4/tcp_keepalive_time 7200
# cat /proc/sys/net/ipv4/tcp_keepalive_intvl 75
# cat /proc/sys/net/ipv4/tcp_keepalive_probes 9
The first two parameters are expressed in seconds, and the last is the pure number. This means that the keepalive routines wait for two hours (7200 secs) before sending the first keepalive probe, and then resend it every 75 seconds. If no ACK response is received for nine consecutive times, the connection is marked as broken.
Modifying this value is straightforward: you need to write new values into the files. Suppose you decide to configure the host so that keepalive starts after ten minutes of channel inactivity, and then send probes in intervals of one minute. Because of the high instability of our network trunk and the low value of the interval, suppose you also want to increase the number of probes to 20.
Here's how we would change the settings:
# echo 600 > /proc/sys/net/ipv4/tcp_keepalive_time
# echo 60 > /proc/sys/net/ipv4/tcp_keepalive_intvl
# echo 20 > /proc/sys/net/ipv4/tcp_keepalive_probes
To be sure that all succeeds, recheck the files and confirm these new values are showing in place of the old ones.
这样,上面的三个参数配置完毕。
对 于一个已经建立的tcp连接。如果在keepalive_time时间内双方没有任何的数据包传输,则开启keepalive功能的一端将发送 keepalive数据包,若没有收到应答,则每隔keepalive_intvl时间再发送该数据包,发送keepalive_probes次。一直没有 收到应答,则发送rst包关闭连接。若收到应答,则将计时器清零。
Heartbeat简介Heartbeat心跳实战试验hi/%BB%C6%D0%C4%B2%A8/blog/item/d120a74da0e7fd38afc3ab09#vim ha(两个主机名【最好用uname -n查看】)nodesev2ping 222 222#vim ha(两个主机名【最好用uname -n查看】)nodesev2ping 222.90.88.1测试机的IP(要写外部的测试IP)#vim /etc/ha.d/authkeys(开启验证模式,这是安全方面的配置里面有三种认证方式:crc md5 sha1)auth 11 crc#chmod 600 authkeys(不做此步,则heartbeat服务无法启动)#vim /etc/ha.d/haresources(这个主要指明资源的来源,是远端挂载)在
Lvs采用dr模型 前段两台director,然后这两台director装上keepalived提供高可用,keepalived配置好后,会自动添加realserver以及vip. 后端nginx修改内核参数ar
首先要知道一点所有的TCP/IP的参数修改是临时的,因为它们都位于/PROC/SYS/NET目录下,如果想使参数长期保存,可以通过编辑/ETC/SYSCTL.CONF文件来实现,这里不做详细说明,只针对Linux的TCPIP内核参数优化列举相关参数:
1、为自动调优定义socket使用的内存
2、默认的TCP数据接收窗口大小(字节)
3、最大的TCP数据接收窗口
4、默认的TCP发送窗口大小
5、最大的TCP数据发送窗口
6、在每个网络接口接收数据包的速率比内核处理这些包速率快时,允许送到队列的数据包最大数目
7、定义了系统中每一个端口最大的监听队列长度
8、探测消息未获得相应时,重发该消息的间隔时间
9、在认定tcp连接失效之前,最多发送多少个keepalive探测消息等。
相关优化参数代码:
sh# net.core.wmem_default 发送缓存区预留内存默认大小 默认值 16k# net.core.rmem_default 接受缓存区预留内存默认大小 默认值 16k# net.core.wmem_max 发送缓存区预留内存最大值 默认值 128k# net.core.rmem_max 接受缓存区预留内存最大值 默认值 128k# net.unix.max_dgram_qlen 进程间通信发送数据, 默认10# net.ipv4.tcp_syncookies# net.ipv4.syn_retries# net.ipv4.tcp_fin_timeout 如果socket连接由本端关闭,则保持在FIN-WAIT-2状态的时间# net.ipv4.tcp_keepalive_time 当keepalive起作用的时候,tcp发送keepalive消息的频度,默认2小时# net.ipv4.route_max_size 路由缓存最大值