赞
踩
参考:HAproxy的配置详解
配置文件路径:/etc/haproxy/haproxy.cfg
- #---------------------------------------------------------------------
- # 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
-
- # lpf
- debug
-
- #---------------------------------------------------------------------
- # 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
-
-
- # lpf
- # timeout connect 5000
- # timeout client 500000
- # timeout server 500000
-
- #---------------------------------------------------------------------
- # main frontend which proxys to the backends
- #---------------------------------------------------------------------
- frontend main *:5000
- acl url_static path_beg -i /static /images /javascript /stylesheets
- acl url_static path_end -i .jpg .gif .png .css .js
-
- use_backend static if url_static
- default_backend app
-
- #---------------------------------------------------------------------
- # static backend for serving up images, stylesheets and such
- #---------------------------------------------------------------------
- backend static
- balance roundrobin
- server static 127.0.0.1:4331 check
-
- #---------------------------------------------------------------------
- # round robin balancing between the various backends
- #---------------------------------------------------------------------
- backend app
- balance roundrobin
- server app1 127.0.0.1:5001 check
- server app2 127.0.0.1:5002 check
- server app3 127.0.0.1:5003 check
- server app4 127.0.0.1:5004 check
-
-
-
- # ============================================ lpf ============================================= #
- frontend apps
- bind 0.0.0.0:80
- option tcplog
- mode tcp
- default_backend apps
-
- backend apps
- mode tcp
- balance roundrobin
- server webserver1 ${CRC_IP}:80 check
-
- frontend apps_ssl
- bind 0.0.0.0:443
- option tcplog
- mode tcp
- default_backend apps_ssl
-
- backend apps_ssl
- mode tcp
- balance roundrobin
- option ssl-hello-chk
- server webserver1 ${CRC_IP}:443 check
-
- frontend api
- bind 0.0.0.0:6443
- option tcplog
- mode tcp
- default_backend api
-
- backend api
- mode tcp
- balance roundrobin
- option ssl-hello-chk
- server webserver1 ${CRC_IP}:6443 check

- HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分,配置文件对缩进没有要求
-
- - global:全局配置段
- - 进程及安全配置相关的参数
- - 性能调整相关参数
- - Debug参数
- - proxies:代理配置段
- - defaults:为frontend, backend, listen提供默认配置
- - frontend:前端,相当于nginx中的server {} ;可以有多组
- - backend:后端,相当于nginx中的upstream {};可以有多组,
- - listen:同时拥有前端和后端配置,配置简单,生产推荐使用
-
- ### global配置
-
- global 配置参数说明
-
- 官方文档:http://cbonte.github.io/haproxy-dconv/2.4/configuration.html#3
-
- ~~~shell
- chroot #锁定运行目录,把haproxy的进程禁锢在一个文件夹内
- deamon #以守护进程运行,后台方式运行,在容器中运行的话不要加这个选项
- stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin process 1 #socket文件,利用它和haproxy通讯,可以发管理指令
- user, group, uid, gid #运行haproxy的用户身份
- nbproc n #开启的haproxy worker 进程数,默认进程数是一个,一般和CPU核数相同
- #nbthread 1 #多进程 nbproc配置互斥(版本有关,CentOS8的haproxy1.8无此问题),指定每个haproxy进程开启的线程数,默认为每个进程一个线程
- #如果同时启用nbproc和nbthread 会出现以下日志的错误,无法启动服务
- #Apr 7 14:46:23 haproxy haproxy: [ALERT] 097/144623 (1454) : config : cannot enable multiple processes if multiple threads are configured. Please use either nbproc or nbthread but not both.
-
- cpu-map 1 0 #绑定haproxy worker 进程至指定CPU,将第1个work进程绑定至0号CPU
- cpu-map 2 1 #绑定haproxy worker 进程至指定CPU,将第2个work进程绑定至1号CPU
- maxconn n #每个haproxy进程的最大并发连接数,可以是100000,还可以更大一百万
- maxsslconn n #每个haproxy进程ssl最大连接数,用于haproxy配置了证书的场景下,不建议配置,可以把ssl连接放到web服务器上
- maxconnrate n #每个进程每秒创建的最大连接数量,控制瞬间并发
- spread-checks n #后端server状态check随机提前或延迟百分比时间,可以错开检查时间,建议2-5(20%-50%)之间,默认值0,不随机
- pidfile #指定pid文件路径,要和service指定的pid路径一样
- log 127.0.0.1 local2 info #定义全局的syslog服务器;日志服务器需要开启UDP协议,最多可以定义两个

HAproxy本身不记录客户端的访问日志.此外为减少服务器负载,一般生产中HAProxy不记录日志.
也可以配置HAProxy利用rsyslog服务记录日志到指定日志文件中
HAProxy日志配置
- #在global配置项定义:
- log 127.0.0.1 local{1-7} info #基于syslog记录日志到指定设备,级别有(err、warning、info、debug)
- listen web_port
- bind 127.0.0.1:80
- mode http
- log global #开启当前web_port的日志功能,默认不记录日志
- server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
-
- # systemctl restart haproxy
Rsyslog配置
- vim /etc/haproxy/haproxy.cfg
- #在global配置项定义:
- log 远程IP地址 local{1-7} info #基于syslog记录日志到指定设备,级别有(err、warning、info、debug)
-
- vim /etc/rsyslog.conf
- $ModLoad imudp #打开udp 514端口,默认注释掉的
- $UDPServerRun 514
- ......
- local3.* /var/log/haproxy.log
- ......
-
- # systemctl restart rsyslog
- ~~~powershell
- defaults [<name>] #默认配置项,针对以下的frontend、backend和listen生效,可以多个name也可以没有name
- frontend <name> #前端servername,类似于Nginx的一个虚拟主机 server和LVS服务集群。
- backend <name> #后端服务器组,等于nginx的upstream和LVS中的RS服务器
- listen <name> #将frontend和backend合并在一起配置,相对于frontend和backend配置更简洁,生产常用
-
- 注意:
- frontend+backend 是配合使用的,listen 是工作生产中常用的
- name字段只能使⽤”-”、”_”、”.”、和”:”,并且严格区分⼤⼩写,例如:Web和web是完全不同的两组服务器。
defaults 配置参数:
- option redispatch #当server Id对应的服务器挂掉后,强制定向到其他健康的服务器,重新派发
- option abortonclose #当服务器负载很高时,自动结束掉当前队列处理比较久的连接,针对业务情况选择开启,生产中一般不加
- option http-keep-alive #开启与客户端的会话保持
- option forwardfor #透传客户端真实IP至后端web服务器
- mode http|tcp #设置默认工作类型,后面listen的优先比比默认高,可以单独设置
- timeout http-keep-alive 120s #session 会话保持超时时间,此时间段内会转发到相同的后端服务器
- timeout connect 120s #客户端请求从haproxy到后端server最长连接等待时间(TCP连接之前),默认单位ms
- timeout server 600s #客户端请求从haproxy到后端服务端的请求处理超时时长(TCP连接之后),默认单位ms,如果超时,会出现502错误,此值建议设置较大些,访止502错误
- timeout client 600s #设置haproxy与客户端的最长非活动时间,默认单位ms,建议和timeout server相同
- timeout check 2s #对后端服务器的默认检测超时时间,
- #default-server inter 1000 weight 3 #指定后端服务器的默认设置
-
- 说明
- option 是可选项,可以不选,一般都会写上
frontend 配置参数:
- frontend +业务名称
- bind #指定HAProxy的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字段中
- #格式:
- bind [<address>]:<port_range> [, ...] [param*]
- #注意:如果需要绑定在非本机的IP,需要开启内核参数:net.ipv4.ip_nonlocal_bind=1
- use_backend <后端服务器名称>
-
- ⽣产⽰例:
- frontend WEB_PORT
- bind :80,:8080
- bind 192.168.7.102:10080,:8801-8810,192.168.7.101:9001-9010
- mode http/tcp #指定负载协议类型
- use_backend backend_name #调⽤的后端服务器组名称
Proxies配置-backend
定义一组后端服务器,backend服务器将被frontend进行调用。
注意:backend 的名称必须唯一,并且必须在listen或frontend中事先定义才可以使用,否则服务无法启动
- mode http|tcp #指定负载协议类型,和对应的frontend必须一致
- option #配置选项
- server #定义后端real server,必须指定IP和端口
-
- server 配置
- 针对一个server配置
- check #对指定real进行健康状态检查,如果不加此设置,默认不开启检查,check后面没有其它配置也可以启用检查功能
- #默认对相应的后端服务器IP和端口,利用TCP连接进行周期性健康性检查,注意必须指定端口才能实现健康性检查
- addr <IP> #可指定的健康状态监测IP,可以是专门的数据网段,减少业务网络的流量
- port <num> #指定的健康状态监测端口
- inter <num> #健康状态检查间隔时间,默认2000 ms,单位是毫秒 =2s
- fall <num> #后端服务器从线上转为线下的检查的连续失效次数,默认为3
- rise <num> #后端服务器从下线恢复上线的检查的连续有效次数,默认为2
- weight <weight> #默认为1,最大值为256,0(状态为蓝色)表示不参与负载均衡,但仍接受持久连接
- backup #将后端服务器标记为备份状态,只在所有非备份主机down机时提供服务,类似Sorry Server
- disabled #将后端服务器标记为不可用状态,即维护状态,除了持久模式,将不再接受连接,状态为深黄色,优雅下线,不再接受新用户的请求
- redirect prefix http://www.baidu.com/ #将请求临时(302)重定向至其它URL,只适用于http模式
- redir http://www.baidu.com #将请求临时(302)重定向至其它URL,只适用于http模式
- maxconn <maxconn> #当前后端server的最大并发连接数,是HAproxy转发给后端server的最大连接数,不是HAproxy接受请求的最大连接数,很少设置
- backlog <backlog> #当前端服务器的连接数达到上限后的后援队列长度,注意:不支持backend

注意:option后面加 httpchk,smtpchk,mysql-check,pgsql-check,ssl-hello-chk方法,可用于实现更多应用层检测功能。
使用listen替换 frontend和backend的配置方式,可以简化设置,通常只用于TCP协议的应用
- #官网业务访问入口
- listen WEB_PORT_80 #业务的名称最好加上端口号,可以一目了然
- bind 10.0.0.7:80
- mode http
- option forwardfor
- server web1 10.0.0.17:8080 check inter 3s fall 3 rise 5 #web1 后端服务器的名称,自己取
- server web2 10.0.0.27:8080 check inter 3s fall 3 rise 5
- 范例 1:frontend+backend
- frontend magedu-test-http
- bind :80,:8080
- mode tcp
- use_backend magedu-test-http-nodes
-
- backend magedu-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
-
- 范例 2:frontend+backend
- 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
-
- 范例 3:使⽤listen替换frontend和backend的配置⽅式:
- #官⽹业务访问⼊⼝=====================================
- 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.101:80 check inter 3000 fall 3 rise 5

当业务众多时,将所有配置都放在一个配置文件中,会造成维护困难。可以考虑按业务分类,将配置信 息拆分,放在不同的子配置文件中,从而达到方便维护的目的。
- 添加子配置目录到unit文件中
- [root@centos7 ~]#vim /lib/systemd/system/haproxy.service
- [Unit]
- Description=HAProxy Load Balancer
- After=syslog.target network.target
- [Service]
- 修改下面两行
- ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -c -q
- ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -p /var/lib/haproxy/haproxy.pid
- ExecReload=/bin/kill -USR2 $MAINPID
- [Install]
- WantedBy=multi-user.target
-
- 创建子配置目录
- [root@centos7 ~]#mkdir /etc/haproxy/conf.d/
-
- 创建子配置文件,注意:必须为cfg后缀非.开头的配置文件
- [root@centos7 ~]#vim /etc/haproxy/conf.d/test.cfg
- listen 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
-
- [root@centos7 ~]#systemctl daemon-reload ; systemctl restart haproxy

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。