HaProxy主要配置文件详解和配置实验

您所在的位置:网站首页 haproxy配置文件详解 HaProxy主要配置文件详解和配置实验

HaProxy主要配置文件详解和配置实验

2023-03-12 23:41| 来源: 网络整理| 查看: 265

global配置

官方文档:http://cbonte.github.io/haproxy-dconv/2.1/configuration.html#3

chroot #锁定运行目录deamon #以守护进程运行stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin process 1#socket文件user, group, uid, gid #运行haproxy的用户身份nbproc n #开启的haproxy worker 进程数,默认进程数是一个#nbthread 1 #和多进程 nbproc配置互斥(版本有关,CentOS8的haproxy1.8无此问题),指定每个haproxy进程开启的线程数,默认为每个进程一个线程#如果同时启用nbproc和nbthread 会出现以下日志的错误,无法启动服务Apr 7 14:46:23 haproxy haproxy: [ALERT] 097/144623 (1454) : config : cannotenable multiple processes if multiple threads are configured. Please use eithernbproc or nbthread but not both.cpu-map 1 0 #绑定haproxy worker 进程至指定CPU,将第1个work进程绑定至0号CPUcpu-map 2 1 #绑定haproxy worker 进程至指定CPU,将第2个work进程绑定至1号CPUmaxconn n #每个haproxy进程的最大并发连接数maxsslconn n #每个haproxy进程ssl最大连接数,用于haproxy配置了证书的场景下maxconnrate n #每个进程每秒创建的最大连接数量spread-checks n #后端server状态check随机提前或延迟百分比时间,建议2-5(20%-50%)之间,默认值0pidfile #指定pid文件路径log 127.0.0.1 local2 info #定义全局的syslog服务器;日志服务器需要开启UDP协议,最多可以定义两个Proxies配置

官方文档:http://cbonte.github.io/haproxy-dconv/2.1/configuration.html#4

defaults [] #默认配置项,针对以下的frontend、backend和listen生效,可以多个name也可以没有namefrontend #前端servername,类似于Nginx的一个虚拟主机 server和LVS服务集群。backend #后端服务器组,等于nginx的upstream和LVS中的RS服务器listen #将frontend和backend合并在一起配置,相对于frontend和backend配置更简洁,生产常用Proxies配置-defaultsoption redispatch #当server Id对应的服务器挂掉后,强制定向到其他健康的服务器,重新派发option abortonclose #当服务器负载很高时,自动结束掉当前队列处理比较久的连接,针对业务情况选择开启option http-keep-alive #开启与客户端的会话保持option forwardfor #透传客户端真实IP至后端web服务器mode http|tcp #设置默认工作类型,使用TCP服务器性能更好,减少压力timeout http-keep-alive 120s #session 会话保持超时时间,此时间段内会转发到相同的后端服务器timeout connect 120s #客户端请求从haproxy到后端server最长连接等待时间(TCP连接之前),默认单位mstimeout server 600s #客户端请求从haproxy到后端服务端的请求处理超时时长(TCP连接之后),默认单位ms,如果超时,会出现502错误,此值建议设置较大些,访止502错误timeout client 600s #设置haproxy与客户端的最长非活动时间,默认单位ms,建议和timeoutserver相同timeout check 5s #对后端服务器的默认检测超时时间default-server inter 1000 weight 3 #指定后端服务器的默认设置Proxies配置-listen 简化配置

使用listen替换 frontend和backend的配置方式,可以简化设置,通常只用于TCP协议的应用

#官网业务访问入口listen WEB_PORT_80bind 10.0.0.7:80mode httpoption forwardforserver web1 10.0.0.17:8080 check inter 3000 fall 3 rise 5server web2 10.0.0.27:8080 check inter 3000 fall 3 rise 5Proxies配置-frontend bind: #指定HAProxy的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字段中#格式:bind []: [, ...] [param*]#注意:如果需要绑定在非本机的IP,需要开启内核参数:net.ipv4.ip_nonlocal_bind=1backlog #针对所有server配置,当前端服务器的连接数达到上限后的后援队列长度,注意:不支持backend示例: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-proxylisten http_https_proxy #https监听 bind :80 bind :443 ssl crt /etc/haproxy/site.pem #公钥和私钥公共文件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-proxylisten external_bind_app1 #监听file descriptor bind "fd@${FD_APP1}"

生产示例:frontend magedu_web_port #可以采用后面形式命名:业务-服务-端口号 bind :80,:8080 bind 10.0.0.7:10080,:8801-8810,10.0.0.17:9001-9010 mode http|tcp #指定负载协议类型 use_backend #调用的后端服务器组名称

Proxies配置-backend mode http|tcp #指定负载协议类型,和对应的frontend必须一致option #配置选项server #定义后端real server,必须指定IP和端口

注意:option后面加 httpchk,smtpchk,mysql-check,pgsql-check,ssl-hello-chk方法,可用于实现更多应用层检测功能

server 配置

定义一组后端服务器,backend服务器将被frontend进行调用。

注意: backend 的名称必须唯一,并且必须在listen或frontend中事先定义才可以使用,否则服务无法启动

#针对一个server配置check #对指定real进行健康状态检查,如果不加此设置,默认不开启检查,只有check后面没有其它配置也可以启用检查功能#默认对相应的后端服务器IP和端口,利用TCP连接进行周期性健康性检查,注意必须指定端口才能实现健康性检查 addr #可指定的健康状态监测IP,可以是专门的数据网段,减少业务网络的流量 port #指定的健康状态监测端口 inter #健康状态检查间隔时间,默认2000 ms fall #后端服务器从线上转为线下的检查的连续失效次数,默认为3 rise #后端服务器从下线恢复上线的检查的连续有效次数,默认为2

weight #默认为1,最大值为256,0(状态为蓝色)表示不参与负载均衡,但仍接受持久连接backup #将后端服务器标记为备份状态,只在所有非备份主机down机时提供服务,类似Sorry Serverdisabled #将后端服务器标记为不可用状态,即维护状态,除了持久模式,将不再接受连接,状态为深黄色,优雅下线,不再接受新用户的请求redirect prefix http://www.baidu.com/ #将请求临时(302)重定向至其它URL,只适用于http模式redir http://www.baidu.com #将请求临时(302)重定向至其它URL,只适用于http模式maxconn #当前后端server的最大并发连接数

frontend+backend 配置实例 范例1:frontend magedu-test-http bind :80,:8080 mode tcp use_backend linweiwei-test-http-nodesbackend linweiwei-test-http-nodes mode tcp default-server inter 1000 weight 6 server web1 10.0.0.17:80 weight 2 check addr 10.0.0.117 port 8080 server web1 10.0.0.27:80 check

#官网业务访问入口frontend WEB_PORT_80 bind 10.0.0.7:80 mode http use_backend web_prot_http_nodesbackend web_prot_http_nodes mode http option forwardfor server 10.0.0.17 10.0.0.17:8080 check inter 3000 fall 3 rise 5 server 10.0.0.27 10.0.0.27:8080 check inter 3000 fall 3 rise 5

使用子配置文件保存配置

注意: 子配置文件的文件后缀必须为.cfg

#创建子配置目录[root@centos7 ~]#mkdir /etc/haproxy/conf.d/#创建子配置文件,注意:必须为cfg后缀非.开头的配置文件[root@centos7 ~]#vim /etc/haproxy/conf.d/test.cfglisten WEB_PORT_80 bind 10.0.0.7:80 mode http balance roundrobin server web1 10.0.0.17:80 check inter 3000 fall 2 rise 5 server web2 10.0.0.27:80 check inter 3000 fall 2 rise 5#添加子配置目录到unit文件中[root@centos7 ~]#vim /lib/systemd/system/haproxy.service[Unit]Description=HAProxy Load BalancerAfter=syslog.target network.target

[Service]#修改下面两行ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f/etc/haproxy/conf.d/ -c -qExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f/etc/haproxy/conf.d/ -p /var/lib/haproxy/haproxy.pidExecReload=/bin/kill -USR2 $MAINPID[Install]WantedBy=multi-user.target

[root@centos7 ~]#systemctl daemon-reload[root@centos7 ~]#systemctl restart haproxy

HAProxy日志配置

HAproxy本身不记录客户端的访问日志.此外为减少服务器负载,一般生产中HAProxy不记录日志.也可以配置HAProxy利用rsyslog服务记录日志到指定日志文件中



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3