haproxy的配置和一些报错

本文介绍了如何配置haproxy,并连接到两个不同的tomcat服务器。通过yum安装haproxy,编辑配置文件,然后运行haproxy检查连接。成功配置后,通过特定URL查看stats页面验证。在配置过程中遇到的警告主要关于日志格式和监听错误,通过调整配置文件并重启haproxy服务可解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先建立了两个tomcat服务器

  173.16.80.104:3020  

  173.16.80.70:8090 

下载haproxy

sudo yum install haproxy

一般在/etc/haproxy

编辑配置文件 sudo vi haproxy.cfg

global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        root
    group       root
    daemon


    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats


defaults
    log global
    mode http
    option http-keep-alive
    option httplog
    timeout connect 5000ms
    timeout client 10000ms
    timeout server 50000ms
    timeout http-request 20000ms
    balance roundrobin


listen stats
    mode http
    log 127.0.0.1 local0 err
    bind  0.0.0.0:1088
    stats enable
    stats hide-version
    stats uri     /stats
    stats refresh 30s
    stats auth    admin:admin
    stats admin if TRUE


listen tomcat
    bind 0.0.0.0:5222
    mode http
    maxconn 300
    log 127.0.0.1 local0 debug
    balance roundrobin
    server  static1 173.16.80.104:3020 check inter 2000 fall 3 weight 30
    server  static2 173.16.80.70:8090 check inter 2000 fall 3 weight 30
配置文件解释
###########全局配置#########

global
    log         127.0.0.1 local2  
 (log 127.0.0.1 local0 #[日志输出配置,所有日志都记录在本机,通过local0输出]
  log 127.0.0.1 local1 notice #定义haproxy 日志级别[error warringinfo debug])

    chroot      /var/lib/haproxy   #chroot运行路径

    pidfile     /var/run/haproxy.pid  #haproxy 进程PID文件

    maxconn     4000 #默认最大连接数,需考虑ulimit-n限制

    user        root    #运行haproxy的用户

    group       root     #运行haproxy的用户所在的组

    daemon       #以后台形式运行harpoxy


    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

########默认配置############

defaults
    log global
    mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK

    option http-keep-alive #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip

    option httplog #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现

    timeout connect 5000ms #连接超时

    timeout client 10000ms #客户端超时

    timeout server 50000ms #服务器超时
 #timeout http-keep-alive10s #默认持久连接超时时间

 #timeout queue 1m #默认队列超时时间

    timeout http-request 20000ms #默认http请求超时时间

    balance roundrobin #设置默认负载均衡方式,轮询方式
 
 #balance source #设置默认负载均衡方式,类似于nginx的ip_hash

 #balnace leastconn #设置默认负载均衡方式,最小连接数




########统计页面配置########

listen stats
    mode http #http的7层模式

    log 127.0.0.1 local0 err #错误日志记录

    bind  0.0.0.0:1088 #设置Frontend和Backend的组合体,监控组的名称,按需要自定义名称

    stats enable
    stats hide-version #隐藏统计页面上HAProxy的版本信息

    stats uri     /stats #统计页面url

    stats refresh 30s #统计页面自动刷新时间

    stats auth    admin:admin #设置监控页面的用户和密码:admin,可以设置多个用户名

    stats admin if TRUE #设置手工启动/禁用,后端服务器(haproxy-1.4.9以后版本)

########backend后端配置##############


listen tomcat
    bind 0.0.0.0:5222
    mode http
    maxconn 300
    log 127.0.0.1 local0 debug
    balance roundrobin
    server  static1 173.16.80.104:3020 check inter 2000 fall 3 weight 30 #设服务器信息
    server  static2 173.16.80.70:8090 check inter 2000 fall 3 weight 30  #设服务器信息
重新启动[chenwb@hadoop03 haproxy]$ sudo systemctl restart haproxy

运行 [chenwb@hadoop03 haproxy]$ sudo haproxy -f /etc/haproxy/haproxy.cfg

检查两个tomcat是否成功 

浏览器地址栏输入配置的ip+端口 出现以下页面



然后在浏览器地址栏输入

http://173.16.80.70:1088/stats

得到一下的页面表示成功配置


如果出现一下的错误

[WARNING] 079/184258 (56658) : config : log format ignored for proxy 'stats' since it has no log address.

[WARNING] 079/184258 (56658) : config : log format ignored for proxy 'tomcat' since it has no log address.

只是没配置日志

[WARNING] 079/184258 (56658) : [haproxy.main()] Cannot raise FD limit to 8034.
[ALERT] 079/184258 (56658) : Starting frontend GLOBAL: error when trying to preserve previous UNIX socket [/var/lib/haproxy/stats]
[ALERT] 079/184258 (56658) : Starting proxy stats: cannot listen to socket [127.0.0.1:8066]

[ALERT] 079/184258 (56658) : Starting proxy tomcat: cannot listen to socket [0.0.0.0:5111]

修改配置为上面的配置文件内容 然后使用sudo启动就可以了



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值