haproxy tcp 4層代理限速方案

global
    maxconn         20000 #最大連接數
    ulimit-n        204800  #ulimit的數量限制
    log             127.0.0.1 local3
    user             haproxy 
    group            haproxy
    nbproc         1
    daemon 
    pidfile         /var/run/haproxy.pid
defaults
    log global  
    mode tcp 
    retries 3 
    timeout connect 5s #連接超時
    timeout client 30s #客戶端超時
    timeout server 30s #服務器端超時
    option redispatch  
    option          nolinger
    no option dontlognull
    option      tcplog
    option log-separate-errors


listen admin_stats  #監控頁面設置
    bind 0.0.0.0:80
    bind-process 1
    mode http
    log 127.0.0.1 local3 err
    stats refresh 30s 
    stats uri /haproxy_status
    stats realm welcome login\ Haproxy
    stats auth admin:admin
    stats hide-version
    stats admin if TRUE

frontend mongodb_read  
   stick-table type ip size 1000k expire 30s store conn_cur,conn_rate(3s) # 3秒內的連接次數限制到2000次
   tcp-request connection reject if { src_conn_cur ge 2000 } || { src_conn_rate ge 6000 }
   tcp-request connection track-sc1 src
    bind        ip:60021
    default_backend db_read


backend db_read 
    balance roundrobin
    server db1 ip:60020 check inter 1500 rise 3 fall 3 weight 2

haproxy.service

[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg   -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg  -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

編譯

make TARGET=linux-glibc  USE_SYSTEMD=1 USE_PCRE=1  USE_ZLIB=1
yum install systemd-devel

haproxy版本

HA-Proxy version 2.3.10

下載地址:https://share.weiyun.com/RDaV0n4E

ab壓測效果

Completed 1200 requests
Completed 2400 requests
apr_socket_recv: Connection reset by peer (104)
Total of 3449 requests completed

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章