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