Haproxy三:端口耗尽+在线维护

1.6 Haproxy TCP端口耗尽解决方案
在实际使用过程中,有两个问题比较容易发生:

  1. TCP端口耗尽
  2. 网卡带宽跑满
    本文重点讲讲如何优化问题1,问题2暂不讨论。
    1.6.1 优化一:使用尽可能多的端口
    Linux系统默认提供了65K个端口,每当Haproxy建立了一个到MySQL的连接,就会消耗一个端口;当Haproxy断开和MySQL的连接时,该端口并不会立即释放,而是会处于TIME_WAIT状态(2MSL),超时后才会释放此端口供新的连接使用。
    我的环境中,tcp_fin_timeout为15秒,也就是说如果我环境中的haproxy可以承载的最大并发连接数为64K/(15
    2)=2.1K,可实际上达不到这个上限,原因如下:
    net.ipv4.ip_local_port_range = 15000 65000
    linux会保留一段端口,实际能参与分配的端口数只有50K,为了获得尽可能多的可分配端口,做如下调整:

sysctl net.ipv4.ip_local_port_range=“1025 65000”

sysctl net.ipv4.ip_local_port_range=“1025 65000”

记得修改/etc/sysctl.conf中对应的内容
1.6.2 优化二:复用处于TIME_WAIT的端口
调整两个参数:
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
第一个参数很安全,可以不用过多关注。需要注意的是第二个参数,某些情况下会导致数据包被丢弃。
例如:client通过NAT连接haproxy,并且haproxy端打开了tcp_tw_recycle,同时saw_tstamp也没有关闭,当第一个连接建立并关闭后,此端口(句柄)处于TIME_WAIT状态,在2*MSL时间内又一个client(相同IP,如果打开了xfrm还要相同PORT)发一个syn包,此时linux内核就会认为这个数据包异常,从而丢掉这个包,并发送rst包.
不过通常情况下,client都是通过内网直接连接haproxy,所以可以认为tcp_tw_recycle是安全的,只是需要记住此坑。
1.6.3 优化三:缩短TIME_WAIT时间
Linux系统默认MSL为60秒,也就是正常情况下,120秒后处于TIME_WAIT的端口(句柄)才会释放,可以将MSL的时间缩小,缩短端口的释放周期。

cat /proc/sys/net/ipv4/tcp_fin_timeout

60

echo 15 > /proc/sys/net/ipv4/tcp_fin_timeout

这是一个折中的数值,太小也会导致其它问题
1.6.4 优化四:使用多IP
如优化一中所说,我们已经尽可能多的使用了系统提供的端口范围。但最多依然不超过65K。
Haproxy提供了内建的端口管理方法,可以充分利用以扩大我们的端口范围。
server mysql0 10.0.3.1:3306 check source 10.0.3.100:1025-65000
server mysql1 10.0.3.1:3306 check source 10.0.3.101:1025-65000
如果使用两个ip,我们可用的端口数就接近130K。扩展多个IP,就可以不断增加端口数。
1.6.5 优化五:使用长连接
服务最好使用长连接,一是避免频繁的申请连接,导致端口耗尽;二是避免创建连接带来的时间消耗。
1.7 Haproxy 在线维护
1.7.1 Haproxy Sock
开启Haproxy Unix Socket
在global添加:

stats socket /usr/local/haproxy/haproxy.sock mode 600 level admin
stats timeout 2m

1.7.2 Socat 工具
socat是一个多功能的网络工具,名字来由是“Socket CAT”,可以看作是netcat的N倍加强版,socat的官方网站:http://www.dest-unreach.org/socat/ 。
socat是一个两个独立数据通道之间的双向数据传输的继电器。这些数据通道包含文件、管道、设备(终端或调制解调器等)、插座(Unix,IP4,IP6 - raw,UDP,TCP)、SSL、SOCKS4客户端或代理CONNECT。
socat支持广播和多播、抽象Unix sockets、Linux tun/tap、GNU readline和PTY。它提供了分叉、记录和进程间通信的不同模式。多个选项可用于调整socat和其渠道,Socat可以作为TCP中继(一次性或守护进程),作为一个守护进程基于socksifier,作为一个shell Unix套接字接口,作为IP6的继电器,或面向TCP的程序重定向到一个串行线。

socat的主要特点就是在两个数据流之间建立通道;且支持众多协议和链接方式:ip, tcp, udp, ipv6, pipe,exec,system,open,proxy,openssl,socket等。

使用socat可以查看和设置HAProxy状态,首先得让HAProxy产生出一个sock出来(hatop ,socat都是基于这个的,没这个什么都做不了)。

[root@lb-node3 ~]#yum install –y readline-devel openssl-devel tcp_wrappers
[root@lb-node3 ~]# cd /usr/local/src
[root@lb-node3 src]# wgethttp://www.dest-unreach.org/socat/download/socat-1.7.2.4.tar.gz
[root@lb-node3 src]# tar zxf socat-1.7.2.4.tar.gz
[root@lb-node3 src]# cd socat-1.7.2.4
[root@lb-node3 socat-1.7.2.4]# ./configure&& make && make install
常用功能
获取帮助:
echo “help”| socat stdio /usr/local/haproxy/haproxy.sock
Unknown command. Please enter one of the following commands only :
clear counters : clear max statistics counters (add ‘all’ for all counters)
clear table : remove an entry from a table
help : this message
prompt : toggle interactive mode with prompt
quit : disconnect
show info : report information about the running process
show pools : report information about the memory pools usage
show stat : report counters for each proxy and server
show errors : report last request and response errors for each proxy
show sess [id] : report the list of current sessions or dump this session
show table [id]: report table usage stats or dump this table’s contents
get weight : report a server’s current weight
set weight : change a server’s weight
set server : change a server’s state or weight
set table [id] : update or create a table entry’s data
set timeout : change a timeout setting
set maxconn : change a maxconn setting
set rate-limit : change a rate limiting value
disable : put a server or frontend in maintenance mode
enable : re-enable a server or frontend which is in maintenance mode
shutdown : kill a session or a frontend (eg:to release listening ports)
show acl [id] : report avalaible acls or dump an acl’s contents
get acl : reports the patterns matching a sample for an ACL
add acl : add acl entry
del acl : delete acl entry
clear acl : clear the content of this acl
show map [id] : report avalaible maps or dump a map’s contents
get map : reports the keys and values matching a sample for a map
set map : modify map entry
add map : add map entry
del map : delete map entry
clear map : clear the content of this map
set ssl : set statement for ssl
查看状态:
echo “show info;show stat” | socat /usr/local/haproxy/haproxy.sock stdio
关闭节点:
echo “disable server b_yxpopo_com/hongbao-node11” | socat /usr/local/haproxy/haproxy.sock stdio
启用节点:
echo “enable server b_yxpopo_com/hongbao-node11” | socat /usr/local/haproxy/haproxy.sock stdio

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值