prometheus监控主流的中间件mongodb
测试环境说明
准备用来测试的服务器,信息如下:
| 作用 | ip | 端口 |
|---|---|---|
| Prometheus服务端 数据存储、数据采集、提供webUI | 10.0.0.51 | 9090 |
| grafana | 10.0.0.51 | 3000 |
| dokcer;在客户端上运行MongoDB容器 | 10.0.0.61 | 27017 |
零、基础环境准备
1.客户端安装docker环境
2.部署mysql服务
2.1拉镜像
[root@node61 ~]# docker image ls |grep mongo
mongo 8.0.6-noble 7ef8fa6da12d 19 months ago 888MB
[root@node61 ~]#
2.2部署dokcer
[root@node61 ~]# docker run -d --name mongodb-server --network host mongo:8.0.6-noble
74be9057c4a0a9c35d9d37fd96c5045a2cef26c1c5fed191fc6eca4663351e0d
2.3检查
[root@node61 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74be9057c4a0 mongo:8.0.6-noble "docker-entrypoint.s..." 3 seconds ago Up 2 seconds mongodb-server
[root@node61 ~]#
[root@node61 ~]# ss -ntl | grep 27017
LISTEN 0 4096 0.0.0.0:27017 0.0.0.0:*
[root@node61 ~]#
一、客户端采集数据
MongoDB本身不自带metrics接口,需要部署第三方exporter。
1.下载exporter
方式一、浏览器下载,然后上传
方式二、命令行安装
[root@node61 ~]# wget https://github.com/percona/mongodb_exporter/releases/download/v0.43.1/mongodb_exporter-0.43.1.linux-amd64.tar.gz
2.安装
[root@node61 ~]# tar xf mongodb_exporter-0.43.1.linux-amd64.tar.gz -C /usr/local/bin/ mongodb_exporter-0.43.1.linux-amd64/mongodb_exporter --strip-components=1
[root@node61 ~]# ll /usr/local/bin/mongodb_exporter
-rwxr-xr-x 1 1001 geoclue 20467864 Dec 13 20:10 /usr/local/bin/mongodb_exporter*
3.运行MySQL-exporter暴露MySQL的监控指标
运行exporter
[root@node61 ~]#mongodb_exporter --mongodb.uri=mongodb://10.0.0.61:27017 --log.level=info --collect-all
验证
[root@node61 ~]# ss -tnulp |grep 9216
二、prometheus配置服务发现(静态监控目标)
三、grafana展示监控
prometheus监控主流的中间件之nginx
两种方法
🤔prometheus监控nginx有两种方法(任选其一都可以)
1.编译安装nginx时,安装nginx-vts-modules模块,此模块可以提供metrics接口
2.安装nginx,部署nginx-vts-exporter,提供metrics接口。

测试环境说明
准备用来测试的服务器,信息如下:
| 作用 | ip | 端口 |
|---|---|---|
| Prometheus服务端 数据存储、数据采集、提供webUI | 10.0.0.51 | 9090 |
| grafana | 10.0.0.51 | 3000 |
| 编译安装nginx环境; 配置nginx的vts接口 | 10.0.0.61 | 80(nginx端口; 8086(vts接口) |
| nginx-vtx-exporter | 10.0.0.61 | 9913 |
一、编译安装nginx
一、安装编译依赖(Ubuntu)
推荐阅读:
编译 nginx 源码需要 3 个基础库
Installing NGINX Open Source | NGINX Documentation

[root@node61 conf.d]# apt -y install git wget gcc make zlib1g-dev build-essential libtool openssl libssl-dev
二、下载nginx源码
1.进入一个放源码的目录,比如 /usr/local/src
[root@node61 conf.d]# cd /usr/local/src
2.下载源码
方式一、github上下载
[root@node61 src]# wget https://github.com/nginx/nginx/releases/download/release-1.28.0/nginx-1.28.0.tar.gz
方式二、nginx官网下载
wget https://nginx.org/download/nginx-1.28.0.tar.gz
3.解压
[root@node61 src]# tar xf nginx-1.28.0.tar.gz
三、下载nginx-module-vts模块
nginx-module-vts模块下载地址:
https://github.com/vozlt/nginx-module-vts/
推荐版本:0.2.1

上传到服务器
[root@node61 ~]# ll /root/nginx-module-vts-0.2.1.tar.gz
-rw-r--r-- 1 root root 179679 Nov 10 14:01 /root/nginx-module-vts-0.2.1.tar.gz
[root@node61 ~]#
解压
tar xf

四、编译安装nginx
进入解压目录
[root@node61 src]# cd nginx-1.28.0/
configure
[root@node61 ~]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/logs/nginx.pid --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --with-http_ssl_module --with-http_v2_module --with-http_realip_module --without-http_rewrite_module --with-http_stub_status_module --without-http_gzip_module --with-file-aio --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/root/nginx-module-vts-0.2.1
编译安装
[root@node61 nginx-1.28.0]# make
[root@node61 nginx-1.28.0]# make install
5.安装后验证
[root@node61 conf]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.28.0
built by gcc 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04.3)
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/logs/nginx.pid --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --with-http_ssl_module --with-http_v2_module --with-http_realip_module --without-http_rewrite_module --with-http_stub_status_module --without-http_gzip_module --with-file-aio --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/root/nginx-module-vts-0.2.1
[root@node61 conf]#
6.配置systemd启动
[root@node61 conf]# cat > /etc/systemd/system/nginx.service <<'EOF'
[Unit]
Description=Nginx Compiled Source
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
启动
[root@node61 conf]# systemctl daemon-reload
[root@node61 conf]# systemctl start nginx
[root@node61 conf]# systemctl enable nginx
[root@node61 conf]# systemctl status nginx
● nginx.service - LSB: Stop/start nginx
Loaded: loaded (/etc/init.d/nginx; generated)
Active: active (exited) since Tue 2026-11-10 14:13:07 CST; 2s ago
Docs: man:systemd-sysv-generator(8)
Process: 26145 ExecStart=/etc/init.d/nginx start (code=exited, status=0/SUCCESS)
CPU: 15ms
Nov 10 14:13:07 node61 systemd[1]: Starting LSB: Stop/start nginx...
Nov 10 14:13:07 node61 systemd[1]: Started LSB: Stop/start nginx.
[root@node61 conf]#

二、配置nginx(提供指标查询接口,给 exporter 拉)
[root@node61 ~]# cat /usr/local/nginx/conf/nginx.conf
...
http {
...
# ========== VTS监控配置开始 ==========
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
# 有upstream后端节点时,开启后端指标采集
# vhost_traffic_status_filter_by_upstream on;
# ========== VTS监控配置结束 ==========
...
# VTS采集专用server,仅本机访问,给nginx-vts-exporter拉取指标
server {
listen 8086;
server_name _;
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format json;
allow 10.0.0.0/24;
deny all;
}
}
...
}
配置解释:
vhost_traffic_status_zone;
-
作用:开启 VTS 模块,在内存里创建一块共享内存区域,用来存放所有监控统计数据(连接数、请求数、流量、响应码、各虚拟主机统计、upstream 后端统计等)。
-
没有这一行,VTS 直接不工作,后面的
/status页面不会有任何统计数据。
listen 127.0.0.1:8086;
只监听本机回环地址,只有 node61 本机程序能访问 8086 端口,别的服务器(包括 Prometheus 服务器)直接连不上 ,这也是你刚才报错 connection refused 的源头之一。 安全设计:VTS 接口不对外暴露。
vhost_traffic_status_display;
开启 VTS 状态页面输出,访问 /status 才会返回统计内容;不加这条,访问这个路径会 404。
vhost_traffic_status_display_format json;
指定返回格式为 JSON。
三、安装nginx-vtx-exporter
一、下载
方式一、浏览器下载
1.官网
https://github.com/sysulq/nginx-vts-exporter

v0.10.8

上传到服务器
[root@node61 ~]# ll nginx-vtx-exporter_0.10.8_linux_amd64.tar.gz
-rw-r--r-- 1 root root 3290350 Nov 10 19:12 nginx-vtx-exporter_0.10.8_linux_amd64.tar.gz
[root@node61 ~]#
二、运行
1.解压
[root@node61 ~]# tar xf nginx-vtx-exporter_0.10.8_linux_amd64.tar.gz -C /usr/local/bin/
2.运行nginx-vtx-exporter
[root@node61 ~]# nginx-vtx-exporter -nginx.scrape_uri=http://10.0.0.61:8086/status/format/json
验证
[root@node61 ~]# ss -tnulp |grep 9913
tcp LISTEN 0 4096 *:9913 *:* users:(("nginx-vtx-expor",pid=2141,fd=3))
[root@node61 ~]#

四、prometheus配置静态监控目标
1.编辑配置文件
新增一个job任务:
采集客户端的http接口
有两种方法:(任选其一都可以)
1.直采nginx的vts接口
2.采集expoter的metrics接口
115 #nginx-vtx-modules
116 - job_name: "nginx-vtx-modules"
117 # metrics_path defaults to '/metrics'
118 metrics_path: "/status/format/prometheus"
119 static_configs:
120 - targets:
121 - "10.0.0.61:8086"
122
123 #nginx-vtx-exporter
124 - job_name: "nginx-vtx-exporter"
125 # metrics_path 默认就是 /metrics,这里可以省略
126 static_configs:
127 - targets:
128 - 10.0.0.61:9913 # ✅ nginx-vts-exporter 默认监听端口 9913

2.热加载生效
3.验证
3.1打开prome的WebUI,查看job任务是否加载成功
http://10.0.0.51:9090/targets?search=

五、grafana展示监控
一、部署grafana
二、配置grafana
1.添加Prometheus数据源
2.新建dashboard
