HAProxy安装、配置做tcp流量转发
高可用HAProxy——yum安装部署配置使用
HAProxy介绍
HAProxy是高性能TCP(第四层)/HTTP(第七层)反向代理负载均衡服务器。(The Reliable, High Performance TCP/HTTP Load Balancer)
HAProxy安装部署
yum安装
查看详细信息
这里可以看到配置文件路径
修改配置文件(最好先提前复制一下备份)
查看状态
HAProxy配置文件
HAProxy配置文件主要由全局设定和代理设定两部分组成,包含5个域:global、default、frontend、backend、listen。
global
# 全局配置,定义haproxy进程的工作特性和全局配置
global
log 127.0.0.1 local2
chroot /var/lib/haproxy #chroot运行的路径
pidfile /var/run/haproxy.pid #haproxy pid的存放位置
maxconn 65536 #最大连接数
nbproc 10
ulimit-n 200000
user haproxy #haproxy的运行用户
group haproxy #haproxy的运行用户的所属组
daemon #守护进程的方式在后台工作
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
全局配置,通常是一些进程级别的配置,与操作系统相关。
default
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http #默认使用的七层协议,也可以是tcp四层协议,如果配置为health,则表示健康检查,返回ok
log global
option tcplog #详细记录tcp日志
option redispatch
option dontlognull #不记录健康检查的日志信息
option forwardfor
retries 3 #重试次数为3次,失败3次以后则表示服务不可用
timeout http-request 5s #http请求超时时间,客户端建立连接5s但不请求数据的时候,关闭客户端连接
timeout queue 10s #等待最大时间,表示等待最大时长为10s
timeout connect 10s #连接超时时间,表示客户端请求转发至服务器所等待的时长为10s
timeout client 30m #客户端超时时间,表示客户端非活跃状态的时间为30min
timeout server 30m #服务器超时时间,表示客户端与服务器建立连接后,等待服务器的超时时间为30min
timeout http-keep-alive 10s #持久连接超时时间,表示保持连接的超时时长为10s
timeout check 10s #心跳检测超时时间,表示健康状态监测时的超时时间为10s
默认参数配置,主要是涉及的公共配置,在defaults中一次性添加。frontend、backend、listen未配置时,都可以默认defaults中的参数配置。若配置了,会覆盖。
frontend & backend
frontend test
bind *:8082
default_backend test
option httplog
acl user-core path_beg /test/v1/user/
use_backend user-core_server if user-core
# test
backend test
mode http
balance roundrobin
server node1 10.xxx.xxx.1:7000 check port 7000 inter 5000 rise 5 fall 5
server node2 10.xxx.xxx.2:7000 check port 7000 inter 5000 rise 5 fall 5
# user-core_server
backend user-core_server
mode http
balance roundrobin
server node1 10.xxx.xxx.1:7001 check port 7001 inter 5000 rise 5 fall 5
server node2 10.xxx.xxx.2:7001 check port 7001 inter 5000 rise 5 fall 5 backup
frontend haproxy_statis_front
bind *:8081
mode http
default_backend statis_haproxy
backend statis_haproxy
mode http
balance roundrobin
stats uri /haproxy/stats
stats auth haproxy:zkK_HH@zz
stats refresh 30s
stats show-node
stats show-legends
stats hide-version
frontend可以看作是前端接收请求的部分,内部指定后端;
backend可以看作是后端服务接收请求的部分;
frontend中acl规则详解
1)语法:acl 自定义的acl名称 acl方法 -i [匹配的路径或文件]
其中:
acl:是一个关键字,表示定义ACL规则的开始,后面跟自定义acl名称;
-i:忽略大小写,后面跟匹配的路径或者文件的正则表达式;
2)语法:use_backend backend实例名称 if acl规则
use_backend:后面跟backend实例名;
if:后面跟acl规则名称,表示如果满足acl规则,则请求backend实例;
3)示例:
acl user-core path_beg /test/v1/user/
use_backend user-core_server if user-core
1
2
表示符合http://ip:port/test/v1/user会转发user-core_server实例服务;
listen
listen admin_stats
bind *:8080 #监听端口
mode http
option httplog
log global
stats enable #统计接口启用开关
maxconn 10
stats refresh 30s #页面刷新时长
stats uri /haproxy?stats #haproxy ui访问后缀
stats realm haproxy #认证时的realm,作为提示用的
stats auth admin:admin #认证用户名和密码
stats hide-version #隐藏HAProxy版本号
stats admin if TRUE #管理界面只有认证通过后才能在ui上进行管理
listen是`frontend和backend的组合,haproxy的监控ui可以通过这个进行配置。
HAProxy TCP 负载均衡配置
global
maxconn 51200
chroot /var/lib/haproxy
uid 99
gid 99
daemon
#quiet
nbproc 1 #进程数
pidfile /var/run/haproxy.pid
defaults
mode tcp #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
#retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
timeout connect 20000ms #连接超时
timeout client 30000ms #客户端超时
timeout server 30000ms #服务器超时
#timeout check 2000 #=心跳检测超时
log 127.0.0.1 local0 err #[err warning info debug]
balance roundrobin #负载均衡算法
option httplog#日志类别,采用httplog
#option httpclose #每次请求完毕后主动关闭http通道,ha-proxy不支持keep-alive,只能模拟这种模式的实现
option dontlognull
#option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
listen admin_stats
bind 0.0.0.0:3333 #监听端口
option httplog #采用http日志格式
mode http
stats refresh 30s #统计页面自动刷新时间
stats uri /ha #统计页面url
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
#stats hide-version #隐藏统计页面上HAProxy的版本信息a
frontend tcp_front80
bind *:80
mode tcp
log global
option tcplog
timeout client 300s
backlog 4096
maxconn 65000
default_backend tcp_behind80
backend tcp_behind80
mode tcp
option tcplog
option log-health-checks
balance roundrobin
server s1 47.112.165.207:199
timeout connect 1s
timeout queue 5s
timeout server 300s
frontend tcp_front443
bind *:443
mode tcp
log global
option tcplog
timeout client 300s
backlog 4096
maxconn 65000
default_backend tcp_behind443
backend tcp_behind443
mode tcp
option tcplog
option log-health-checks
balance roundrobin
server s1 47.112.165.207:200
timeout connect 1s
timeout queue 5s
timeout server 300s