部署HAProxy负载均衡
笔特 | 12/7/2022, 9:31:43 AM | 1481 次阅读
准备
CentOS服务器关闭防火墙(或者配置开放端口)
安装
yum install haproxy -y
1
配置
# 编辑配置文件,可提前备份原有配置文件
vi /etc/haproxy/haproxy.cfg
1
2
2
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
# 配置负载均衡(重点)
listen emes-cluster
bind 0.0.0.0:8080 #代理端口
mode http #模式 TCP
balance roundrobin #调度算法
server emes_101 192.168.92.101:8080 check port 8080 inter 2000 rise 2 fall 3
server emes_102 192.168.92.102:8080 check port 8080 inter 2000 rise 2 fall 3
server emes_103 192.168.92.103:8080 check port 8080 inter 2000 rise 2 fall 3
# 配置HAProxy监控地址页面
listen admin
bind 0.0.0.0:5000
mode http
option httplog
stats enable
stats uri /dbs
# ACCOUNT 替换为用户名 PASSWORD 替换为具体的密码
stats auth ACCOUNT:PASSWORD
stats realm welcome login\ Haproxy
stats refresh 10s
stats admin if TRUE
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
启动
# 启用开机自启
systemctl enable haproxy
# 启动服务
systemctl start haproxy
1
2
3
4
2
3
4