当前位置:   article > 正文

HAproxy 配置_haproxy.conf

haproxy.conf

核心配置

HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分
global:全局配置段
   进程及安全配置相关的参数
   性能调整相关参数
proxies:代理配置段
   defaults:为frontend, backend, listen提供默认配置
   frontend:前端,相当于nginx中的server {}
   backend:后端,相当于nginx中的upstream {}
   listen:同时拥有前端和后端配置

golbal配置:
  chroot /opt/haproxy #锁定运行目录
  deamon  #以守护进程运行
  pidfile /run/haproxy.pid #指定pid文件路径
  stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin #socket文件
  user, group,     #运行haproxy的用户身份
  nbproc   1       #work进程数,默认进程数一个,建议与CPU核心一致
  cpu-map 1 0      #第一个work进程与0号CPU绑定
  maxconn 100000   #每个haproxy进程的最大并发连接数
  maxsslconn n     #每个haproxy进程ssl最大连接数,用于haproxy配置了证书的场景下
  maxconnrate n    #每个进程每秒创建的最大连接数量
  spread-checks n #后端server状态check随机提前或延迟百分比时间,建议2-5(20%-50%)之间
  log 127.0.0.1 local2 info  #定义全局的syslog(443)服务器;最多可以定义两个
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Proxies配置

defaults 配置:   #default的配置可以写入到frontend或backend中

    option redispatch     #当服务器挂掉后,强制定向到其他健康的服务器
    option http-keep-alive  #开启持久连接
    option forwardfor      #透传客户端真实IP至后端web服务器 
    mode http|tcp          #设置默认负载协议类型,tcp协议性能更好
    timeout connect 600s   #会话最长等待时间
    timeout client 600s    #客户端最长非活动时长
    timeout server 600s    #请求处理超时时长

#frontend结合backend 配置:
frontend http_80
    bind 10.0.0.8:80  #指定HAProxy的监听地址,可以同时监听多个IP或端口
    mode http|tcp     
    use_backend http_nginx  #调用的后端服务器组名称
backend http_nginx
   option forwardfor
   server 10.0.0.7 10.0.0.7:80   check inter 3000 fall 3 rise 5  
   server 10.0.0.17 10.0.0.17:80   check inter 3000 fall 3 rise 5

#listen 代替frontend+backend 配置
listen web_80
  bind 172.16.0.8:80
  log global
  server 10.0.0.7  10.0.0.7:80  check inter 3000 fall 2 rise 3
  server 10.0.0.17 10.0.0.17:80 check inter 3000 fall 2 rise 3
  
#server段的option选项 
check  #对后端服务器进行健康状态检查
  inter 2s  #每隔两s进行一次健康性检查
  fall 3    #连续三次检查失败,则下线调度列表
  rise 3    #连续三次检查成功,则上线调度列表
  weight 1   #权重设置,0表示不参与负载均衡,但仍接受持久连接
  backup    #将后端主机标记为备份状态,只在所有非备份主机down机时提供服务
  disabled  #将后端服务器标记禁用状态,将不再接受连接
  redir http://www.baidu.com    #将请求临时(302)重定向至其它URL,只适用于http模式
  maxconn 1000 #当前后端server的最大并发连接数
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

使用子配置文件保存配置

mkdir /etc/haproxy/conf.d/
#添加子配置目录到service文件中,必须是.cfg后缀的文件
vim /lib/systemd/system/haproxy.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

systemctl daemon-reload 
systemctl restart haproxy
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

环境准备

客户端:  eth0:172.16.0.6
Haproxy:   eth0: 10.0.0.8
           eth1:172.16.0.8
后端web: web1: 10.0.0.7
         web2:10.0.0.17
  • 1
  • 2
  • 3
  • 4
  • 5

listen替代frontend+backend

#当外网主机访问172.16.0.8将请求调度到后端web服务器
listen web_80
 bind 172.16.0.8:80
 log global
 server 10.0.0.7  10.0.0.7:80  check inter 3000 fall 2 rise 3
 server 10.0.0.17 10.0.0.17:80 check inter 3000 fall 2 rise 3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/303183
推荐阅读
相关标签
  

闽ICP备14008679号