HAProxy 是一款广泛使用的高性能负载均衡器,支持 TCP 和 HTTP 协议,提供高可用性、负载均衡和代理服务。
HAProxy 2.0 以上版本提供了完善的指标暴露体系,方便观测云收集对应的指标信息。
版本要求
HAProxy 2.0
HAProxy Enterprise 2.0r1
HAProxy ALOHA 11.5
以下主要是采用 docker 的方式部署 HAProxy 并上报数据到观测云。
HAProxy 安装部署
准备配置 haproxy.cfg
#----------------# Global settings#----------------global log 127.0.0.1 local2 maxconn 4000 daemon
defaults mode tcp log global option tcplog option dontlognull option http-server-close 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
#--------------------#Application prometheus#--------------------
# Haproxy 开启指标frontend prometheus bind *:8405 mode http http-request use-service prometheus-exporter if { path /metrics } no log
frontend mysql mode tcp bind :3308 log global default_backend mysql_servers
backend mysql_servers mode tcp balance leastconn server s1 192.168.2.114:3306 weight 3 check inter 5s rise 2 fall 3 server s2 192.168.2.115:3306 weight 1 check inter 5s rise 2 fall 3
复制代码
通过 prometheus-exporter 暴露指标信息,端口为 8405 ,path 为 /metrics 。 并配置了 mysql 负载均衡,且对外访问端口为 3308 。
Docker-compose
编写 docker-compose.yaml 文件
version: '3'services: haproxy: image: haproxy container_name: haproxy volumes: - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg #插件文件挂载 ports: - 89:80 - 8405:8405 - 3308:3308
复制代码
运行
执行以下命令
docker-compose up -d
查看运行状态 docker-compose ps
root@liurui:/home/liurui/middleware/haproxy# docker-compose ps NAME IMAGE COMMAND SERVICE CREATED STATUS PORTShaproxy haproxy "docker-entrypoint.s…" haproxy 6 days ago Up 3 hours 0.0.0.0:3308->3308/tcp, :::3308->3308/tcp, 0.0.0.0:8404-8405->8404-8405/tcp, :::8404-8405->8404-8405/tcp, 0.0.0.0:89->80/tcp, :::89->80/tcp
复制代码
访问指标信息
通过访问 8405 端口获取指标信息,如下所示:
# HELP haproxy_process_nbthread Number of started threads (global.nbthread)# TYPE haproxy_process_nbthread gaugehaproxy_process_nbthread 8# HELP haproxy_process_nbproc Number of started worker processes (historical, always 1)# TYPE haproxy_process_nbproc gaugehaproxy_process_nbproc 1# HELP haproxy_process_relative_process_id Relative worker process number (1)# TYPE haproxy_process_relative_process_id gaugehaproxy_process_relative_process_id 1# HELP haproxy_process_uptime_seconds How long ago this worker process was started (seconds)# TYPE haproxy_process_uptime_seconds gaugehaproxy_process_uptime_seconds 1364...
复制代码
观测云
注册登录观测云。
操作步骤
1、登录观测云,并获取 datakit 的安装命令
2、执行安装命令安装 datakit
3、开启 prom 采集器
由于 HAProxy 能够直接暴露 metrics url ,所以可以直接通过 prom 采集器进行采集。
进入 DataKit 安装目录下的 conf.d/prom ,复制 prom.conf.sample 为 haproxy.conf 。
cd /usr/local/datakit/conf.d/promcp prom.conf.sample haproxy.conf
复制代码
调整 haproxy.conf 内容如下:
urls = ["http://localhost:8405/metrics"]
source = "haproxy"
## Keep Exist Metric Name ## If the keep_exist_metric_name is true, keep the raw value for field names. keep_exist_metric_name = true
interval = "10s"
复制代码
其他配置按需调整,调整参数说明:
4、重启 Datakit
仪表板
通过访问 HAPorxy 代理的应用,如上面所配置的 prometheus、mysql,产生一些指标数据。观测云对 HAPorxy 也做了丰富的仪表板,可以在场景里面进行创建并查看。
评论