Haproxy壓測及參數調優


Haproxy壓測及參數調優

[root@3b9d411b-0a16-4bc4-acd8-10f71b997c29 etc]# ss -s&&free -g Total: 102008 (kernel 102040) TCP: 102003 (estab 101812, closed 56, orphaned 77, synrecv 0, timewait 0/0), ports 30060 Transport Total IP IPv6 * 102040 - - RAW 0 0 0 UDP 4 3 1 TCP 101947 101946 1 INET 101951 101949 2 FRAG 0 0 0 total used free shared buffers cached Mem: 7 2 5 0 0 0 -/+ buffers/cache: 2 5 Swap: 0 0 0 
先看上面的信息:
  • 這台機器已將建立10w個鏈接,最高壓測到17w左右
  • 但是內存和cpu都沒有到達瓶頸

cpu和內存的原因可能是沒有用ssl鏈接都是基於http鏈接計算量小的緣故,也有可能是vegeta
沒有輸出足夠的壓力源導致,后期會完善壓測方法,由此說明我們haproxy還有更大潛力挖掘。

下面會介紹單個Haproxy機器上實現這種規模訪問所需的配置,和詳細的內核調優方法。

測試目標

我們要測試的組件是HAProxy 1.6 版。生產環境是在4核8G的機器上運行該軟件,當前所有的連接都是非 SSL 的。

測試目標有兩方面:

  • 將線上環境Haproxy連接數從可憐的2000鏈接數提升到1w

  • 其次,希望能夠測試單個 HAProxy 每秒請求數和最大並發連接數的上限

目標一主要因為業務方面需要提高和優化Haproxy性能。 目標二是為了可以在生產環境中部署最少規模的 HAProxy 機器。

組件和配置

  • 使用多台客戶端機器來執行 HAProxy 壓力測試。

  • 線上測試的 HAProxy1.6的機器 4核,8G,centos6.9

  • 相關后端服務器,5台4核4g,centos6,每個機器運行5個tomcat

壓測工具及機器

工具

機器

  • 三台4核4g,內核參數一致

優化參數

  • sysctl.conf配置
net.ipv4.ip_forward = 0
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1
# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536
# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 2
net.ipv4.conf.all.rp_filter = 2
net.ipv4.conf.all.send_redirects = 1
net.ipv4.conf.default.send_redirects = 1
### 表示是否打開TCP同步標簽(syncookie),同步標簽可以防止一個套接字在有過多試圖連接到達時引起過載
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.ip_local_port_range = 20000 65000
net.ipv4.tcp_max_syn_backlog = 40960
net.ipv4.tcp_max_tw_buckets = 819200
net.core.somaxconn = 262144
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_synack_retries = 3
net.ipv4.tcp_syn_retries = 3
net.ipv4.tcp_sack = 1
net.ipv4.tcp_fack = 1
net.ipv4.tcp_dsack = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
### 系統中所允許的文件句柄的最大數目
fs.file-max = 65535
### 單個進程所允許的文件句柄的最大數目
fs.nr_open = 65535
kernel.pid_max = 65536
net.ipv4.tcp_rmem = 4096 87380 8388608
net.ipv4.tcp_wmem = 4096 87380 8388608
net.core.rmem_max = 8388608
net.core.wmem_max = 8388608
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_window_scaling = 1
  • /etc/security/limits.conf配置
*                soft    nproc           65535
*                hard    nproc           65535
*                soft    nofile          65535
*                hard    nofile          65535
  • HAproxy 配置
global
    group root
    user root
    daemon
    nbproc 1
    log 127.0.0.1 local3
    pidfile /var/run/haproxy/pid/10.163.162.65_80.pid
    ulimit-n 1000000 ##這里增大文件打開數
    max-spread-checks  1000ms
    maxconn     30000000 ##這里放開限制
    maxconnrate 30000000
    maxsessrate 30000000
    maxsslconn  30000000
    maxsslrate  30000000
    tune.ssl.default-dh-param 2048
    spread-checks 20
    stats timeout 5000ms
    stats maxconn 50
    stats socket /var/run/haproxy/sock/10.163.162.65_80.sock mode 600 level admin process 1
defaults
    mode http
    maxconn     30000000 ##這里最好與上面一致,否則默認限制2000,找了好久
    option  abortonclose
    option redispatch
    option forwardfor
    balance roundrobin
    log 127.0.0.1 local3 err
    retries 3
    option clitcpka
    option srvtcpka
listen 10.163.162.65:80
    balance roundrobin
    bind 10.163.162.65:80  
    option tcp-check
    option httplog
    #option dontlognull
    timeout http-keep-alive 10s
    timeout http-request    10s
    errorfile 400 /usr/local/haproxy/errorfile/400.html
    errorfile 403 /usr/local/haproxy/errorfile/403.html
    errorfile 408 /usr/local/haproxy/errorfile/408.html
    errorfile 500 /usr/local/haproxy/errorfile/500.html
    errorfile 502 /usr/local/haproxy/errorfile/502.html
    errorfile 503 /usr/local/haproxy/errorfile/503.html
    errorfile 504 /usr/local/haproxy/errorfile/504.html
    timeout connect 100000
    timeout queue   100000
    timeout client  100000
    timeout server  100000
    timeout check   100000
    cookie SERVERID insert indirect nocache
     
        ##后端服務器
        server  172.16.100.110:8480 172.16.100.110:8480 cookie 1852060044 check inter 30000 rise 3 fall 3 weight 22  
        server  172.16.100.110:8180 172.16.100.110:8180 cookie 1852051288 check inter 30000 rise 3 fall 3 weight 22  
        server  172.16.100.110:8080 172.16.100.110:8080 cookie 1852051260 check inter 30000 rise 3 fall 3 weight 22  
        server  172.16.100.110:8280 172.16.100.110:8280 cookie 1852059892 check inter 30000 rise 3 fall 3 weight 22  
        server  172.16.100.110:8380 172.16.100.110:8380 cookie 1852059664 check inter 30000 rise 3 fall 3 weight 22 
        
        server  172.16.100.162:8480 172.16.100.162:8480 cookie 2852060044 check inter 30000 rise 3 fall 3 weight 22  
        server  172.16.100.162:8180 172.16.100.162:8180 cookie 2852051288 check inter 30000 rise 3 fall 3 weight 22  
        server  172.16.100.162:8080 172.16.100.162:8080 cookie 2852051260 check inter 30000 rise 3 fall 3 weight 22  
        server  172.16.100.162:8280 172.16.100.162:8280 cookie 2852059892 check inter 30000 rise 3 fall 3 weight 22  
        server  172.16.100.162:8380 172.16.100.162:8380 cookie 2852059664 check inter 30000 rise 3 fall 3 weight 22

壓測過程

  • ab
#!/bin/bash
for i in `seq 1 10000`
do
ab -c 10000 -n 100000  http://10.163.162.103/index.html
done

壓測結果

Total: 15694 (kernel 15725)
TCP:   16529 (estab 15077, closed 748, orphaned 170, synrecv 0, timewait 747/0), ports 6077

Transport Total     IP        IPv6
*     15725     -         -        
RAW   0         0         0        
UDP   4         3         1        
TCP   15781     15780     1        
INET      15785     15783     2        
FRAG      0         0         0 

             total       used       free     shared    buffers     cached
Mem:             7          1          5          0          0          0
-/+ buffers/cache:          1          5
Swap:            0          0          0

發現客戶端資源消耗的比較大,反而壓不上去。

  • 引入vegeta

使用3台客戶端vegeta進行壓力測試

echo "POST http://10.163.162.65" | vegeta -cpus=4 attack -duration=10m  -header="sleep:1000"  -rate=2000 -workers=500 | tee reports.bin | vegeta report

這里我們簡單了解下Vegeta提供的一些參數細節:

1 -cpus=4,定義客戶端使用的內核數量。為了能夠達到需要的壓力值,我們將施壓機配置適當調整。仔細觀察結果數據會發現,實際壓力並不大,配置調整的主要目的是為了能夠支撐大量狀態為后端服務器休眠的連接。
2 -duration=10m,該參數顧名思義,如果沒有指定執行時間,測試將永久運行。
3 -rate=2000,每秒鍾請求數。

壓測結果

Total: 188713 (kernel 189041)
TCP:   188652 (estab 162234, closed 22, orphaned 0, synrecv 0, timewait 21/0), ports 40061

Transport Total     IP        IPv6
*     189041    -         -        
RAW   0         0         0        
UDP   4         3         1        
TCP   188630    188629    1        
INET      188634    188632    2        
FRAG      0         0         0        

             total       used       free     shared    buffers     cached
Mem:             7          2          4          0          0          0
-/+ buffers/cache:          2          4
Swap:            0          0          0

很輕松達到了16w,而且客戶端資源沒有消耗太大情況下,看樣子如果在客戶端配置好的情況下20w也不成問題。

結論

內核主要優化的點:

  • 內核中 fs.file-max = 65535 ### 系統中所允許的文件句柄的最大數目,客戶端內核也要優化
  • net.ipv4.tcp_syncookies = 1 ### 表示是否打開TCP同步標簽(syncookie),同步標簽可以防止一個套接字在有過多試圖連接到達時引起過載
  • 其他內核參數也要適當的優化
    haproxy主要優化的點:

global的

  • ulimit-n 1000000 ##這里增大文件打開數
  • maxconn 30000000 ##這里放開限制

defaults 中的

  • maxconn 30000000 ##這里最好與上面一致,否則默認限制2000
    壓測工具:
  • ab很消耗客戶端資源而且壓力源不夠穩定,沒有多核提升性能
  • vegeta這個工具很好壓力源穩定,還有很多的功能沒有挖掘出來
    壓測工具直接影響壓測結果,所以一定要選好。
    后端服務:
  • 后端服務器不要太多,本次測試時發現10台以上不會增加多連接數
  • 每個服務器內存分大點,跑5個tomcat很容易被打死

本次壓測效果基本符合預期,發現主要是內核參數與haproxy配置參數不當導致,還有壓測工具要選對,后期還有進一步優化的可能比如:nbproc綁定cpu,vegeta功能挖掘。



作者:bobozaker
鏈接:https://www.jianshu.com/p/21d03ad14477
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM