CentOS 7 HAProxy 使用示例

1. 使用yum安装HAProxy

1
2
yum makecache fast
yum install -y haproxy

2. 借助rsyslog配置HAProxy输出日志到文件

1
2
3
4
5
6
7
8
9
# 编辑/etc/rsyslog.conf
# 启用在udp 514端口接收日志消息
$ModLoad imudp
$UDPServerRun 514

# 以下内容追加到配置文件最后

# Save haproxy log to haproxy.log
local0.* /var/log/haproxy.log

3. 配置HAProxy,这里仅提供一个四层TCP代理的示例,实际中请根据需要修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 备份/etc/haproxy/haproxy.cfg
cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak

# 编辑/etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0 debug
maxconn 50000
uid 99
gid 99
#daemon
nbproc 1
pidfile haproxy.pid

defaults
mode tcp
log global
maxconn 50000
retries 3
timeout connect 10s
timeout client 60m
timeout server 60m

listen stats
mode http
bind 0.0.0.0:9090
log global
stats refresh 30s
stats uri /haproxy-status
stats realm Haproxy\ Statistics
stats auth admin:12345678
stats hide-version
stats admin if TRUE

frontend remote-tools-xxx-frontend
mode tcp
bind :43745
default_backend remote-tools-xxx-backend

backend remote-tools-xxx-backend
mode tcp
balance roundrobin
server node01 x.x.x.x:43745 weight 3 minconn 100 maxconn 50000 check inter 5000 rise 2 fall 5

4. 启动HAProxy

1
2
systemctl start haproxy.service
systemctl status haproxy.service

5. 参考资料

https://blog.51cto.com/yanconggod/2062213