HAProxy base configuration in detail
HAProxy is a high performance, enterprise-class load balancing scheduler, supports load balancing and scheduling four seven TCP HTTP protocol, and supports cookie-based persistence, support for regular expressions and web state statistics, automatic failover, etc., So it is widely used in enterprise business systems within the entrance as a cluster of load balancing scheduler.
HAPrpxy haproxy.cfg profile consists of two parts, namely global and proxies portion.
global: global configuration section
1, process parameters and related security configuration
2, performance tuning parameters
3, Debug parameter
global configuration parameter official document: https: //cbonte.github.io/haproxy-dconv/2.0/intro.html
global # 全局段配置
chroot #(安全配置选项)锁定haproxy用户运行目录。
deamon #以后台守护进程运行
stats socket /var/lib/haproxy/haproxy.sock mode 600 level
admin #socket文件路径、文件权限及管理级别
user haproxy # 设置运行haproxy的用户(非登录用户),也可使用uid关键字替代。
group haproxy # 设置运行haproxy的用户组,也可使用gid关键字替代。
nbproc <number> #开启的haproxy进程数,通常与CPU核心数保持一致,开启多进程提高并发处理能力。
cpu-map 1 0 #将第1个子进程绑定到0号CPU核心,以此类推(优化配置项,haproxy开启了多进程时,绑定haproxy子进程至指定CPU核心上,避免多个子进程在不同CPU核心上切换,提升性能)
nbthread <number> #指定每个haproxy进程开启的线程数,默认为每个进程一个线程,与nbproc互斥,即开启多进程时无法开启多线程,通常适合单核心CPU开启此项提升处理能力。
maxconn <number> #单haproxy进程的最大并发连接数,需要调整优化系统最大文件描述符数(ulimit -n)。
maxconnrate <number> #单个haproxy进程的连接每秒的创建速率上限
maxsslconn <number> #单个haproxy进程的ssl连接最大并发连接数,用于haproxy配置了证书的场景下。需要调整优化系统最大文件描述符数(ulimit -n)。
maxsslrate <number> #单haproxy进程的ssl连接每秒的创建速率上限,用于haproxy配置了证书的场景下。
spread-checks #后端server状态check随机提前或延迟百分比时间,建议2-5(20%-50%)之间。
pidfile /var/lib/haproxy/haproxy.pid #指定pid文件路径
log 127.0.0.1 local3 info #定义全局的syslog服务器到本机rsyslog服务的local3设备上,最多可以定义两个。
proxies: the proxy configuration section
The official document: https: //cbonte.github.io/haproxy-dconv/2.0/configuration.html#4
defaults [name] # default configuration items, for the following frontend, backend and lsiten effect, you can name more
frontend [name] # front end servername, similar to a virtual host Nginx server {}
backend [name] # backend server group, the upstream {} is equal to nginx
listen [name] # The frontend and backend combined configuration
Note: name fields should use only "-", "_", ":", and strictly case-sensitive, such as: Web and web servers are completely different groups. "."
Proxies配置-defaults
defaults # 默认部分的定义
option redispatch # 当使用了cookie会话保持时,haproxy将会将其请求的后端服务器的serverID插入到cookie中,以保证会话的SESSION持久性;而此时,如果后端的服务器宕掉了,但是客户端的cookie是不会刷新的,如果设置此参数,将会将客户的请求强制定向到另外一个后端server上,以保证服务的正常。
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接。
option dontlognull # 启用该项,日志中将不会记录空连接。所谓空连接就是在上游的负载均衡器或者监控系统为了探测该服务是否存活可用时,需要定期的连接或者获取某一固定的组件或页面,或者探测扫描端口是否在监听或开放等动作被称为空连接;官方文档中标注,如果该服务上游没有其他的负载均衡器的话,建议不要使用该参数,因为互联网上的恶意扫描或其他动作就不会被记录下来
option httpclose #使用该参数,每处理完一个request时,haproxy都会去检查http头中的Connection的值,如果该值不是close,haproxy将会将其删除,如果该值为空将会添加为:Connection: close。使每个客户端和服务器端在完成一次传输后都会主动关闭TCP连接。与该参数类似的另外一个参数是“option forceclose”,该参数的作用是强制关闭对外的服务通道,因为有的服务器端收到Connection: close时,也不会自动关闭TCP连接,如果客户端也不关闭,连接就会一直处于打开,直到超时。
option http-keep-alive #开启与客户端的会话保持
option forwardfor #透传客户端真实IP至后端web服务器方便访问日志统计
mode http #默认工作类型(mode {http|tcp|health} 。http是七层模式,tcp是四层模式,health是健康检测,返回OK)
timeout connect 120s #客户端请求到后端server的最长连接等待时间(TCP三次握手之前)
timeout server 600s #客户端请求到后端服务端的超时超时时长(TCP三次握手之后)默认单位是毫秒
timeout client 600s #与客户端的最长非活动时间,默认单位是毫秒
timeout http-keep-alive 120s #session会话保持超时时间,范围内会转发到相同的后端服务器,默认单位是毫秒。
timeout check 5s #对后端服务器的检测超时时间
haproxy status monitoring web page configuration
listen status # 定义一个名为status的部分作为web状态监控页面
bind 0.0.0.0:9999 # 定义监听的网络套接字地址
mode http # 定义为HTTP模式
log global # 继承global中log的定义
stats refresh 30s # stats是haproxy的一个统计页面的套接字,该参数设置统计页面的刷新间隔为30s
stats uri /admin?stats # 设置统计页面的uri为/admin?stats
stats realm Private lands # 设置统计页面认证时的提示内容
stats auth admin:password # 设置统计页面认证的用户和密码,如果要设置多个,另起一行写入即可
stats hide-version # 隐藏统计页面上的haproxy版本信息
Proxies Configuration -frontend
bind:指定HAProxy的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字段中
listen http_proxy #监听http的多个IP的多个端口和sock文件
bind :80,:443,:8801-8810
bind 10.0.0.1:10080,10.0.0.1:10443
bind /var/run/ssl-frontend.sock user root mode 600 accept-proxy
listen http_https_proxy #https监听
bind :80
bind :443 ssl crt /etc/haproxy/site.pem #https证书路径
listen http_https_proxy_explicit #监听ipv6、ipv4和unix sock文件
bind ipv6@:80
bind ipv4@public_ssl:443 ssl crt /etc/haproxy/site.pem
bind [email protected] user root mode 600 accept-proxy
listen external_bind_app1 #监听file descriptor(文件描述符)
bind "fd@${FD_APP1}"
Production Example frontend configuration:
frontend WEB_PORT #定义一个名为WEB_PORT的前端部分
bind :80,:8080
bind 192.168.7.102:10080,:8801-8810,192.168.7.101:9001-9010 #可同时监听多个IP和端口范围
mode http/tcp #指定负载协议类型
use_backend backend_name #调用的后端服务器组名称
Proxies Configuration -backend:
mode http/tcp #指定负载协议类型
option #配置选项
server #定义后端real server
check #对指定real进行健康状态检查,默认不开启
addr IP #可指定的健康状态监测IP
port num #指定的健康状态监测端口
inter num #健康状态检查间隔时间,默认2000 ms
fall num #后端服务器失效检查次数,默认为3
rise num #后端服务器从下线恢复检查次数,默认为2
weight #默认为1,最大值为256,0表示不参与负载均衡
backup #将后端服务器标记为备份状态
disabled #将后端服务器标记为不可用状态
redirect prefix http://www.magedu.net/ #将请求临时重定向至其它URL,只适用于http模式。
maxconn num #当前后端server的最大并发连接数
backlog num #当server的连接数达到上限后的后援队列长度
Note: back option plus httpchk, smtpchk, mysql-check, pgsql-check, ssl-hello-chk methods, may be used to achieve more application layer detection.
frontend + backend configuration example
#官网业务访问入口======================================
frontend WEB_PORT_80
bind 192.168.7.248:80
mode http
use_backend web_prot_http_nodes
backend web_prot_http_nodes
mode http
option forwardfor
server 192.168.7.101 192.168.7.101:8080 check inter 3000 fall 3 rise 5
server 192.168.7.102 192.168.7.102:8080 check inter 3000 fall 3 rise 5
Proxies Configuration -listen
Code written listen more concise manner, using the recommended replacement frontend and backend listen, listen configuration example as follows:
#官网业务访问入口=====================================
listen WEB_PORT_80
bind 192.168.7.102:80
mode http
option forwardfor
server web1 192.168.7.101:80 check inter 3000 fall 3 rise 5
server web2 192.168.7.102:80 check inter 3000 fall 3 rise 5
Note: generally single point of failure to solve the problem, will bind HAProxy Keepalived hot standby HA embodiment, therefore listen listening bind the IP address segment should typically be configured as a virtual VIP address, it is necessary to open the system kernel parameters net.ipv4.ip_nonlocal_bind = 1, and allows harpoxy keepalived listens IP address the current state of the machine does not exist (as VIP drift to another node).