Haproxy 安装与配置

温馨提醒

下载

1
wget https://www.haproxy.org/download/3.1/src/haproxy-3.1.1.tar.gz

编译安装

1
2
3
4
5
6
7
tar xf haproxy-3.1.1.tar.gz

cd haproxy-3.1.1

make TARGET=linux-glibc PREFIX=/usr/local/haproxy SBINDIR=/usr/local/sbin ARCH=x86_64 USE_OPENSSL=1 ADDLIB=-lz

make install PREFIX=/usr/local/haproxy SBINDIR=/usr/local/sbin

创建配置文件

vim /usr/local/haproxy/conf/haproxy.cfg

 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
global
    log [address] [device] [maxlevel] [minlevel]	# 日志输出配置,如log 127.0.0.1 local0 info warning,即向本机rsyslog或syslog的local0输出info到warning级别的日志。其中[minlevel]可以省略。HAProxy的日志共有8个级别,从高到低为emerg/alert/crit/err/warning/notice/info/debug
    chroot      /var/lib/haproxy
    maxconn     4096 		# 设置每个haproxy进程的并发连接数
    nbproc      2 			# 设置haproxy启动时的进程数,不能超过CPU核数
    user        haproxy		# 指定 haproxy 进程所属的用户
    group       haproxy		# 指定 haproxy 进程所属的组
    daemon 				   # haproxy 以后台模式运行
    stats socket /var/lib/haproxy/stats
    pidfile     /var/run/haproxy.pid

defaults 						# 默认配置
    mode     http 			    # 运行模式,分为tcp(四层)或者http(七层)两种模式
    timeout connect 5000ms		# 连接 server 端超时 5s
    timeout client 50000ms		# 客户端响应超时 50s
    timeout server 50000ms		# server 端响应超时 50s
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8		# 记录客户端真实IP
    option                  redispatch
    retries                 3 		# haproxy连接后端服务器失败的次数,达到该次数后将标记该后端服务器为不可用并踢出集群
    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

frontend  web 				# 定义前端节点的名字
    bind        *:80 		# haproxy服务所监听的IP与端口
    mode        http
    option	httplog			# Haproxy默认不记录http日志,通过这个配值启用详细日志
    option	dontlognull	    # 日志中不记录上级负载均衡器发送的用于检测的心跳数据包
    option httpclose        # 表示在客户端和服务器完成一次连接请求后,haproxy将主动关闭http连接,这对性能非常有帮助
    option	forwardfor	except 127.0.0.0/8		# 让后端服务器能获得客户端真实IP
    default_backend     nginx 					# haproxy默认引用的后端服务器组

backend nginx 						# 定义后端服务器组,可以定义多组
	mode	http
    balance     roundrobin 			# 定义负载均衡调度算法,还有static-rr、source、uri等多种算法
    	# roundrobin    轮询方式
    	# source        根据请求源IP
    	# static-rr     根据权重 
    	# leastconn     最少连接者先处理
    	# uri           根据请求uri
    	# url_param     根据请求的url参数
    option      redispatch 			    # 在节点请求失败发生切换时保持cookie
    option      httpchk GET /index.html # 检查后端节点页面状态码是否为200,如果异常就不再将请求发送给该服务器,除了GET还可以使用HEAD
    option  forwardfor header X-Forwarded-For     # 获取客户端真实IP
    server      server1 192.168.1.102:80 weight 5 check inter 2000 rise 2 fall 3
    server      server2 192.168.1.103:80 weight 5 check inter 2000 rise 2 fall 3
    	# weight 代表权重,默认为1,最大为256,0不参与负载
    	# check 健康检查,加上该配置,否则后台服务器宕机了,负载均衡还会把请求发送到该宕机的服务器上
    	# inter 代表健康检查的时间间隔为 2000 毫秒
    	# rise 代表健康检查通过了多少次认为该服务器可用
    	# fall 检测3次失败,则会把该后端服务器标志宕机,不再往该服务器发送请求

listen  admin_stats 				# haproxy状态监控页配置
    bind        0.0.0.0:9188
    mode        http
    log	127.0.0.1 local 0 err
    stats enable
    stats refresh 1s 				# 监控页面自动刷新时间
    stats uri /status 				# 监控页面的地址,ip+端口+项目名访问
    stats auth admin:admin123 		# 登录监控页面的帐号和密码
    stats hide-version 				# 隐藏版本号
    stats admin if TRUE				# 可在监控页面上手动启动或关闭后端服务器

配置使用系统的 rsyslog 服务记录 haproxy 日志

vim /etc/rsyslog.conf

1
2
3
ModLoad imudp
UDPServerRun 514
local3.*	/var/log/haproxy.log

systemctl restart rsyslog

启动

1
haproxy -f /usr/local/haproxy/conf/haproxy.cfg

配置反向代理 mysql

vim /usr/local/haproxy/conf/haproxy.cfg

 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
global
    ......

defaults
    ......

listen mysql1 
    bind 0.0.0.0:3306
    balance leastconn
    mode tcp
    server node1 192.168.1.35:3306 check port 3306 rise 2 fall 2

listen mysql2 
    bind 0.0.0.0:3307                 # 监听的实例名称,地址和端口
    balance leastconn                       # 负载均衡算法
    mode tcp
    server node1 192.168.1.36:3307 check port 3307 rise 2 fall 2

listen admin_stats 
    bind 0.0.0.0:8080
    mode http
    maxconn 10
    stats enable
    stats refresh 5s
    stats uri /stats
    stats realm Cloud\ Haproxy
    stats auth admin:admin
    stats hide-version