当前位置:   article > 正文

HAproxy的配置详解_haproxy配置详解

haproxy配置详解

参考:HAproxy的配置详解

一、配置示例 

配置文件路径:/etc/haproxy/haproxy.cfg

  1. #---------------------------------------------------------------------
  2. # Example configuration for a possible web application. See the
  3. # full configuration options online.
  4. #
  5. # http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
  6. #
  7. #---------------------------------------------------------------------
  8. #---------------------------------------------------------------------
  9. # Global settings
  10. #---------------------------------------------------------------------
  11. global
  12. # to have these messages end up in /var/log/haproxy.log you will
  13. # need to:
  14. #
  15. # 1) configure syslog to accept network log events. This is done
  16. # by adding the '-r' option to the SYSLOGD_OPTIONS in
  17. # /etc/sysconfig/syslog
  18. #
  19. # 2) configure local2 events to go to the /var/log/haproxy.log
  20. # file. A line like the following can be added to
  21. # /etc/sysconfig/syslog
  22. #
  23. # local2.* /var/log/haproxy.log
  24. #
  25. log 127.0.0.1 local2
  26. chroot /var/lib/haproxy
  27. pidfile /var/run/haproxy.pid
  28. maxconn 4000
  29. user haproxy
  30. group haproxy
  31. daemon
  32. # turn on stats unix socket
  33. stats socket /var/lib/haproxy/stats
  34. # lpf
  35. debug
  36. #---------------------------------------------------------------------
  37. # common defaults that all the 'listen' and 'backend' sections will
  38. # use if not designated in their block
  39. #---------------------------------------------------------------------
  40. defaults
  41. mode http
  42. log global
  43. option httplog
  44. option dontlognull
  45. option http-server-close
  46. option forwardfor except 127.0.0.0/8
  47. option redispatch
  48. retries 3
  49. timeout http-request 10s
  50. timeout queue 1m
  51. timeout connect 10s
  52. timeout client 1m
  53. timeout server 1m
  54. timeout http-keep-alive 10s
  55. timeout check 10s
  56. maxconn 3000
  57. # lpf
  58. # timeout connect 5000
  59. # timeout client 500000
  60. # timeout server 500000
  61. #---------------------------------------------------------------------
  62. # main frontend which proxys to the backends
  63. #---------------------------------------------------------------------
  64. frontend main *:5000
  65. acl url_static path_beg -i /static /images /javascript /stylesheets
  66. acl url_static path_end -i .jpg .gif .png .css .js
  67. use_backend static if url_static
  68. default_backend app
  69. #---------------------------------------------------------------------
  70. # static backend for serving up images, stylesheets and such
  71. #---------------------------------------------------------------------
  72. backend static
  73. balance roundrobin
  74. server static 127.0.0.1:4331 check
  75. #---------------------------------------------------------------------
  76. # round robin balancing between the various backends
  77. #---------------------------------------------------------------------
  78. backend app
  79. balance roundrobin
  80. server app1 127.0.0.1:5001 check
  81. server app2 127.0.0.1:5002 check
  82. server app3 127.0.0.1:5003 check
  83. server app4 127.0.0.1:5004 check
  84. # ============================================ lpf ============================================= #
  85. frontend apps
  86. bind 0.0.0.0:80
  87. option tcplog
  88. mode tcp
  89. default_backend apps
  90. backend apps
  91. mode tcp
  92. balance roundrobin
  93. server webserver1 ${CRC_IP}:80 check
  94. frontend apps_ssl
  95. bind 0.0.0.0:443
  96. option tcplog
  97. mode tcp
  98. default_backend apps_ssl
  99. backend apps_ssl
  100. mode tcp
  101. balance roundrobin
  102. option ssl-hello-chk
  103. server webserver1 ${CRC_IP}:443 check
  104. frontend api
  105. bind 0.0.0.0:6443
  106. option tcplog
  107. mode tcp
  108. default_backend api
  109. backend api
  110. mode tcp
  111. balance roundrobin
  112. option ssl-hello-chk
  113. server webserver1 ${CRC_IP}:6443 check

二、基础配置详解

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

2.1 HAProxy日志配置

HAproxy本身不记录客户端的访问日志.此外为减少服务器负载,一般生产中HAProxy不记录日志.

也可以配置HAProxy利用rsyslog服务记录日志到指定日志文件中

HAProxy日志配置

  1. #在global配置项定义:
  2. log 127.0.0.1 local{1-7} info #基于syslog记录日志到指定设备,级别有(err、warning、info、debug)
  3. listen web_port
  4. bind 127.0.0.1:80
  5. mode http
  6. log global #开启当前web_port的日志功能,默认不记录日志
  7. server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
  8. # systemctl restart haproxy

Rsyslog配置

  1. vim /etc/haproxy/haproxy.cfg
  2. #在global配置项定义:
  3. log 远程IP地址 local{1-7} info #基于syslog记录日志到指定设备,级别有(err、warning、info、debug)
  4. vim /etc/rsyslog.conf
  5. $ModLoad imudp #打开udp 514端口,默认注释掉的
  6. $UDPServerRun 514
  7. ......
  8. local3.* /var/log/haproxy.log
  9. ......
  10. # systemctl restart rsyslog

2.2 Proxies配置

  1. ~~~powershell
  2. defaults [<name>] #默认配置项,针对以下的frontend、backend和listen生效,可以多个name也可以没有name
  3. frontend <name> #前端servername,类似于Nginx的一个虚拟主机 server和LVS服务集群。
  4. backend <name> #后端服务器组,等于nginx的upstream和LVS中的RS服务器
  5. listen <name> #将frontend和backend合并在一起配置,相对于frontend和backend配置更简洁,生产常用
  6. 注意:
  7. frontend+backend 是配合使用的,listen 是工作生产中常用的
  8. name字段只能使⽤”-”、”_”、”.”、和”:”,并且严格区分⼤⼩写,例如:Web和web是完全不同的两组服务器。

Proxies配置-defaults

defaults 配置参数

  1. option redispatch #当server Id对应的服务器挂掉后,强制定向到其他健康的服务器,重新派发
  2. option abortonclose #当服务器负载很高时,自动结束掉当前队列处理比较久的连接,针对业务情况选择开启,生产中一般不加
  3. option http-keep-alive #开启与客户端的会话保持
  4. option forwardfor #透传客户端真实IP至后端web服务器
  5. mode http|tcp #设置默认工作类型,后面listen的优先比比默认高,可以单独设置
  6. timeout http-keep-alive 120s #session 会话保持超时时间,此时间段内会转发到相同的后端服务器
  7. timeout connect 120s #客户端请求从haproxy到后端server最长连接等待时间(TCP连接之前),默认单位ms
  8. timeout server 600s #客户端请求从haproxy到后端服务端的请求处理超时时长(TCP连接之后),默认单位ms,如果超时,会出现502错误,此值建议设置较大些,访止502错误
  9. timeout client 600s #设置haproxy与客户端的最长非活动时间,默认单位ms,建议和timeout server相同
  10. timeout check 2s #对后端服务器的默认检测超时时间,
  11. #default-server inter 1000 weight 3 #指定后端服务器的默认设置
  12. 说明
  13. option 是可选项,可以不选,一般都会写上

Proxies配置-frontend

frontend 配置参数:

  1. frontend +业务名称
  2. bind #指定HAProxy的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字段中
  3. #格式:
  4. bind [<address>]:<port_range> [, ...] [param*]
  5. #注意:如果需要绑定在非本机的IP,需要开启内核参数:net.ipv4.ip_nonlocal_bind=1
  6. use_backend <后端服务器名称>
  7. ⽣产⽰例:
  8. frontend WEB_PORT
  9. bind :80,:8080
  10. bind 192.168.7.102:10080,:8801-8810,192.168.7.101:9001-9010
  11. mode http/tcp #指定负载协议类型
  12. use_backend backend_name #调⽤的后端服务器组名称

Proxies配置-backend

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

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

  1. mode http|tcp #指定负载协议类型,和对应的frontend必须一致
  2. option #配置选项
  3. server #定义后端real server,必须指定IP和端口
  4. server 配置
  5. 针对一个server配置
  6. check #对指定real进行健康状态检查,如果不加此设置,默认不开启检查,check后面没有其它配置也可以启用检查功能
  7. #默认对相应的后端服务器IP和端口,利用TCP连接进行周期性健康性检查,注意必须指定端口才能实现健康性检查
  8. addr <IP> #可指定的健康状态监测IP,可以是专门的数据网段,减少业务网络的流量
  9. port <num> #指定的健康状态监测端口
  10. inter <num> #健康状态检查间隔时间,默认2000 ms,单位是毫秒 =2s
  11. fall <num> #后端服务器从线上转为线下的检查的连续失效次数,默认为3
  12. rise <num> #后端服务器从下线恢复上线的检查的连续有效次数,默认为2
  13. weight <weight> #默认为1,最大值为256,0(状态为蓝色)表示不参与负载均衡,但仍接受持久连接
  14. backup #将后端服务器标记为备份状态,只在所有非备份主机down机时提供服务,类似Sorry Server
  15. disabled #将后端服务器标记为不可用状态,即维护状态,除了持久模式,将不再接受连接,状态为深黄色,优雅下线,不再接受新用户的请求
  16. redirect prefix http://www.baidu.com/ #将请求临时(302)重定向至其它URL,只适用于http模式
  17. redir http://www.baidu.com #将请求临时(302)重定向至其它URL,只适用于http模式
  18. maxconn <maxconn> #当前后端server的最大并发连接数,是HAproxy转发给后端server的最大连接数,不是HAproxy接受请求的最大连接数,很少设置
  19. backlog <backlog> #当前端服务器的连接数达到上限后的后援队列长度,注意:不支持backend

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

Proxies配置-listen 简化配置-生产中推荐

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

  1. #官网业务访问入口
  2. listen WEB_PORT_80 #业务的名称最好加上端口号,可以一目了然
  3. bind 10.0.0.7:80
  4. mode http
  5. option forwardfor
  6. server web1 10.0.0.17:8080 check inter 3s fall 3 rise 5 #web1 后端服务器的名称,自己取
  7. server web2 10.0.0.27:8080 check inter 3s fall 3 rise 5

配置实例

  1. 范例 1:frontend+backend
  2. frontend magedu-test-http
  3. bind :80,:8080
  4. mode tcp
  5. use_backend magedu-test-http-nodes
  6. backend magedu-test-http-nodes
  7. mode tcp
  8. default-server inter 1000 weight 6
  9. server web1 10.0.0.17:80 weight 2 check addr 10.0.0.117 port 8080
  10. server web1 10.0.0.27:80 check
  11. 范例 2:frontend+backend
  12. frontend WEB_PORT_80
  13. bind 192.168.7.248:80
  14. mode http
  15. use_backend web_prot_http_nodes
  16. backend web_prot_http_nodes
  17. mode http
  18. option forwardfor
  19. server 192.168.7.101 192.168.7.101:8080 check inter 3000 fall 3 rise 5
  20. server 192.168.7.102 192.168.7.102:8080 check inter 3000 fall 3 rise 5
  21. 范例 3:使⽤listen替换frontend和backend的配置⽅式:
  22. #官⽹业务访问⼊⼝=====================================
  23. listen WEB_PORT_80
  24. bind 192.168.7.102:80
  25. mode http
  26. option forwardfor
  27. server web1 192.168.7.101:80 check inter 3000 fall 3 rise 5
  28. server web2 192.168.7.101:80 check inter 3000 fall 3 rise 5

2.3 使用子配置文件保存配置

当业务众多时,将所有配置都放在一个配置文件中,会造成维护困难。可以考虑按业务分类,将配置信 息拆分,放在不同的子配置文件中,从而达到方便维护的目的。

  1. 添加子配置目录到unit文件中
  2. [root@centos7 ~]#vim /lib/systemd/system/haproxy.service
  3. [Unit]
  4. Description=HAProxy Load Balancer
  5. After=syslog.target network.target
  6. [Service]
  7. 修改下面两行
  8. ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -c -q
  9. ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -p /var/lib/haproxy/haproxy.pid
  10. ExecReload=/bin/kill -USR2 $MAINPID
  11. [Install]
  12. WantedBy=multi-user.target
  13. 创建子配置目录
  14. [root@centos7 ~]#mkdir /etc/haproxy/conf.d/
  15. 创建子配置文件,注意:必须为cfg后缀非.开头的配置文件
  16. [root@centos7 ~]#vim /etc/haproxy/conf.d/test.cfg
  17. listen WEB_PORT_80
  18. bind 10.0.0.7:80
  19. mode http
  20. balance roundrobin
  21. server web1 10.0.0.17:80 check inter 3000 fall 2 rise 5
  22. server web2 10.0.0.27:80 check inter 3000 fall 2 rise 5
  23. [root@centos7 ~]#systemctl daemon-reload ; systemctl restart haproxy
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/303132
推荐阅读
相关标签
  

闽ICP备14008679号