一些通用性的haproxy调优tips

一、硬件和系统
haproxy是单线程,非阻塞,事件驱动,所以会最大化利用单个CPU内核,选择haproxy的硬件时要关注如下:
 
1.选择CPU的时候,选择高主频,大缓存的型号,比内核数更重要
2.选择支持多队列的网卡将网卡中断做"CPU亲和"(推荐Intel 服务器网卡,例如I350AM4,4端口千兆,每个端口8条RX,8条TX队列;CPU亲和可使用Intel驱动源码中的set_irq_affinity.sh脚本)
3.根据haproxy官方文档,将Haproxy和网卡中断绑定在同一个cpu,不同的core上能获得最佳性能,例如: 网卡绑定在CPU0的core0,则haproxy进程绑定在CPU0的core1上效果最佳。因为他们可以尽可能的利用CPU cycle,同时可以共享CPU L3内存。可以使用如下方法确认同在一个物理CPU的内核:
]# cat /sys/bus/cpu/devices/cpu0/topology/core_siblings_list 
0-3,8-11
本例中,core0-3,8-11在同一个物理CPU上,共享CPU L3内存。
4.请务必禁用或删除IRQ Balance
5.如果连接频率非常高(> 5K/s),避免使用虚拟机运行haproxy
6.尽量避免使用iptables conntrack,会影响性能
7.尽量避免将比较占用cpu的进程与haproxy或者网卡中断绑定在同一个core上运行,尤其是在SSL Offload这种场景下,例如如下一些:
backup clients
munin client
nagios plugins or clients
snmpd / net-snmp
syslog / rsyslogd / syslog-ng
zabbix clients
可以使用taskset来将上述进程绑定到不同的core上去。
8.如果使用SSL Offload功能,请选择openssl的1.0.2的分支,这个分支对于Intel xeon的avx2,ADCX, ADOX, MULX等指令进行过优化,可以最大化利用CPU的特性。
 
二、针对短连接的系统调整(红色部分比较重要)
用来建立连接的本地端口范围,越大越好,默认"32768 61000",推荐"1024 65024"   
net.ipv4.ip_local_port_range  = 1025 65534
 
支持高连接率,默认"1024",推荐"100000"
net.ipv4.tcp_max_syn_backlog = 100000
net.core.netdev_max_backlog = 100000
 
对于向外建立的连接,允许重复使用同一个本地源端口,默认"0",推荐"1"
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
 
每个socket所能允许的尚未被haproxy接受或处理的未完成连接请求数量,默认"128",推荐"65534"
net.core.somaxconn = 65534
 
增大文件句柄数
# cat /proc/sys/fs/file-nr
fs.file-max = 65535
 
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_syncookies = 0
 
三、针对长连接的系统调整
用来建立连接的本地端口范围,越大越好,默认"32768 61000",推荐"1025 65534"   
net.ipv4.ip_local_port_range  = 1025 65534
 
每个TCP连接分配的接收和发送缓冲
# memory allocation min/pressure/max.
# read buffer, write buffer, and buffer space
net.ipv4.tcp_rmem = 10000000 10000000 10000000
net.ipv4.tcp_wmem = 10000000 10000000 10000000
net.ipv4.tcp_mem = 10000000 10000000 10000000
 
net.core.rmem_max = 11960320
net.core.wmem_max = 11960320
net.core.rmem_default = 11960320
net.core.wmem_default = 11960320
net.core.optmem_max = 11960320
net.core.netdev_max_backlog = 300000
 
# turn off selective ACK and timestamps
net.ipv4.tcp_sack = 0
net.ipv4.tcp_timestamps = 0
 
#默认情况下,此参数为1,将大幅削减空闲连接时的CWnd,对长连接如SSL产生负面影响。将其设置为0,可以显著提高性能。
net.ipv4.tcp_slow_start_after_idle = 0
 
四、haproxy的多进程
 
haproxy可以运行多进程,但是官方不太推荐。
 
多进程的好处: 
  • 专核专用:  每个进程绑定一个内核,每个进程可以专用于一种任务(或者应用,或者协议),例如: 1个进程专门处理HTTP,另一个进程专门处理MySQL
  • 向上扩展:  可以通过增加CPU核数,增加计算容量
  • SSL offload:  SSL生成key的能力与进程数线性相关,但TLS session resumption 超过3个进程则收益甚微。
多进程的缺点:
  • 每个进程有自己的内存区域,意味着:
  • debug模式不能在multi-process模式下工作
  • frontend和相关的backend必须运行在同一进程内
  • stick-table同步不能兼容,即peers指令不可用
  • 存储在每个进程本地的内存信息不能共享: stick-table和tracked counter,stats状态信息,server的maxconn,connection rate
  • 每个haproxy进程都会做health check: 后端的backend会被每个haproxy进程探测死活,一个service在不同进程内可能会有短暂的不同状态
  • 配置文件会更复杂
如果对stats严重需要,可以针对每个haproxy进程做一个stats。
 
global
    nbproc 4
    cpu-map 1 0     # 前一个数字是进程号,后一个数字是CPU内核的号
    cpu-map 2 1
    cpu-map 3 2
    cpu-map 4 3
 
可以将不同任务绑定到不同的内核上独立执行
frontend access_http
   bind 0.0.0.0:80
   bind-process 1
frontend access_https
   bind 0.0.0.0:443 ssl crt /etc/yourdomain.pem
   bind-process 2 3 4
 
五、网卡驱动方面
 
[chloe@biru ~]$ lspci
 07:00.0 Ethernet controller: Intel Corporation 82599EB 10-Gigabit SFI/SFP+ Network Connection (rev 01)
 
[chloe@biru ~]$ grep 0700 /proc/bus/pci/devices
0700    808610fb        28              d590000c                       0                    ecc1                       0                d58f800c                       0                       0                   80000                       0                      20                       0                    4000                  0                0        ixgbe
 
[chloe@biru ~]$ setpci -v -d 8086:10fb e6.b=2e
 
 
关闭Intel 网卡的LRO功能
]# ethtool -K eth0 lro off
]#ethtool -K eth1 lro off
 
参考文档:
1.https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Tuning_and_Optimizing_Red_Hat_Enterprise_Linux_for_Oracle_9i_and_10g_Databases/chap-Oracle_9i_and_10g_Tuning_Guide-Setting_File_Handles.html
2.http://dak1n1.com/blog/7-performance-tuning-intel-10gbe/
3.http://fibrevillage.com/sysadmin/91-tcp-performance-tuning-10g-nic-with-high-rtt-in-linux
4.http://nguyendangquockhanh.blogspot.hk/2013/12/tcp-tuning-10gb-network-cards-on-linux.html
5.http://www.intel.co.uk/content/dam/doc/application-note/82575-82576-82598-82599-ethernet-controllers-interrupts-appl-note.pdf
6.http://blog.yufeng.info/archives/2037
7.http://ju.outofmemory.cn/entry/94177
8.https://greenhost.nl/2013/04/10/multi-queue-network-interfaces-with-smp-on-linux/
9.http://support.citrix.com/article/CTX12
10.http://www.slideshare.net/Severalnines/haproxy-mysql-slides
11.http://blog.onefellow.com/post/82478335338/haproxy-mapping-process-to-cpu-core-for-maximum
12.http://www.jdon.com/performance/mobile-linux.html
13.http://www.cagataygurturk.com/using-haproxy-in-multicore-environments/
14.http://www.lognormal.com/blog/2012/09/27/linux-tcpip-tuning/
15.https://www.haproxy.com/doc/hapee/1.5/system/tunning.html
16.https://software.intel.com/zh-cn/articles/improving-openssl-performance
17.http://blog.haproxy.com/2011/09/16/benchmarking_ssl_performance/
18.https://software.intel.com/zh-cn/articles/accelerating-ssl-load-balancers-with-intel-xeon-v3-processors
19.https://software.intel.com/en-us/articles/accelerating-ssl-load-balancers-with-intel-xeon-e5-v4-processors

阅读原文 

 

posted on 2017-06-12 13:44  生活费  阅读(5182)  评论(0编辑  收藏  举报

导航