Prometheus Push Proxy多采集器配置疑问:如何采集客户端Node Exporter及多Exporter指标
你好呀,看了你的问题,核心是客户端在防火墙后,没法让Prometheus直接拉取本地的Node Exporter和nftables Exporter指标,现在用了push-proxy但不知道怎么让它采集多个源的指标对吧?其实你可能对push-proxy的用法有点误解,我给你梳理两种可行的解决方案:
方案一:用客户端轻量Prometheus+Remote Write(最推荐)
这种方式最优雅,相当于在客户端跑一个迷你版Prometheus,专门负责采集本地所有Exporter的指标,然后统一推送到远端的Prometheus服务器:
客户端配置迷你Prometheus
新建一个简单的配置文件(比如client_prom.yml),指定要采集的本地Exporter:scrape_configs: - job_name: 'client_local_exporters' static_configs: - targets: ['localhost:9100', 'localhost:9102'] # 这里填Node Exporter(9100)和nftables Exporter的端口启动客户端迷你Prometheus
启动时开启Remote Write功能,把指标推送到远端Prometheus的接收端点:prometheus --config.file=client_prom.yml --web.listen-address=":9101" --remote-write.url=http://你的Prometheus服务器地址:8082/api/v1/write这个迷你Prometheus会自动定期拉取本地两个Exporter的指标,然后推送到远端服务器,还自带重试、缓存等机制,比手动脚本靠谱多了。
远端Prometheus配置
启动远端Prometheus时,要开启Remote Write接收功能,不然收不到推送的指标:prometheus --config.file=prometheus.yml --web.listen-address=":8082" --web.enable-remote-write-receiver另外,你之前在
prometheus.yml里配置的push_proxyjob可以删掉了,因为指标是通过Remote Write直接推过来的,远端Prometheus会自动存储这些指标。
方案二:用脚本手动推送(适合临时场景)
如果你不想跑迷你Prometheus,也可以用脚本定期拉取Exporter指标,再推送到push-proxy:
推送Node Exporter指标
写个简单的curl命令,拉取9100端口的指标再推送到push-proxy:curl http://localhost:9100/metrics | curl -X POST --data-binary @- http://localhost:9101/metrics/job/push_proxy/instance/client_node推送nftables Exporter指标
同理,针对nftables Exporter的端口(假设是9102):curl http://localhost:9102/metrics | curl -X POST --data-binary @- http://localhost:9101/metrics/job/push_proxy/instance/client_nftables做成定时任务
把这两个命令加到crontab里,比如每分钟执行一次,保证指标能持续推送。
关于你之前的疑问解释
- 为什么
--metrics-addr不能设多个端口?因为这个参数是push-proxy自身监听的端口,用来接收外部推送过来的指标,它只能绑定一个端口,没法同时绑定多个。push-proxy本身不负责主动采集其他Exporter的指标,它只是个转发工具。 - 之前设
--metrics-addr=":9100"报错,是因为Node Exporter已经占用了9100端口,端口冲突了,所以得给push-proxy或者迷你Prometheus用另一个端口(比如你之前用的9101)。
备注:内容来源于stack exchange,提问作者LeifSec

