haproxy prometheus 的監控metrics 使用的是exporter ,因為haproxy 對於狀態統計報告處理的
比較好,我們可以了stats 同時支持一個csv的api 接口,所以exporter也是基於這個搞的開發,同時
里面對於不同版本的haproxy 做了適配
環境准備
- docker-compose 文件
version: "3"
services:
haproxy:
image: haproxy:1.7
ports:
- "5000:5000"
- "10080:10080"
volumes:
- "./conf/haproxy:/usr/local/etc/haproxy:ro"
exporter:
image: quay.io/prometheus/haproxy-exporter:v0.9.0
command: --haproxy.scrape-uri="http://admin:password@haproxy:10080/haproxy?stats;csv"
ports:
- "9101:9101"
g:
image: grafana/grafana
ports:
- "3000:3000"
p:
image: prom/prometheus
volumes:
- "./conf/prometheus.yml:/etc/prometheus/prometheus.yml"
ports:
- "9090:9090"
- haproxy 配置文件
conf/haproxy/haproxy.cfg 很簡單就是配置了stats 同時配置了一個簡單的jenkins 的proxy+ lb
global
log 127.0.0.1 local2
maxconn 4000
# turn on stats unix socket
#---------------------------------------------------------------------
# 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
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind 0.0.0.0:5000
default_backend app
backend app
balance roundrobin
server app1 10.15.0.80:80 check
server app2 10.15.0.80:8080 check
server app3 127.0.0.1:5003 check
server app4 127.0.0.1:5004 check
listen stats
bind 0.0.0.0:10080
mode http
log global
maxconn 10
clitimeout 100s
srvtimeout 100s
contimeout 100s
timeout queue 100s
stats enable
stats hide-version
stats refresh 30s
stats show-node
stats auth admin:password
stats uri /haproxy?stats
- exporter 配置
主要是啟動的時候指定haproxy server 的地址,因為使用了basic auth, exporter 比較方便支持basic auth 格式的url
command: --haproxy.scrape-uri="http://admin:password@haproxy:10080/haproxy?stats;csv"
- prometheus 配置
實際上就是通過靜態配置添加experter 地址 ./conf/prometheus.yml
scrape_configs:
- job_name: haproxy
metrics_path: /metrics
static_configs:
- targets: ['exporter:9101']
運行&&效果
- 啟動
docker-compose up -d
- 訪問效果
說明
docker-compose文件同時集成了grafana,可以方便的進行UI的可視化展示
參考資料
https://github.com/rongfengliang/haproxy_promethues-docker-compose
https://github.com/prometheus/haproxy_exporter
https://hub.docker.com/_/haproxy