目录
在 Windows 环境下安装和部署 Prometheus + Grafana 的步骤如下:
1. 安装 Prometheus
1.1 下载 Prometheus
- 访问 Prometheus 官方下载页。
- 选择
prometheus-*.windows-amd64.zip
(如prometheus-2.47.0.windows-amd64.zip
)。 - 解压到本地目录(如
C:\Prometheus
)。
1.2 配置 Prometheus
- 修改
prometheus.yml
(用记事本或VS Code打开):global: scrape_interval: 15s scrape_configs: - job_name: "prometheus" # 监控Prometheus自身 static_configs: - targets: ["localhost:9090"]
- 保存文件。
1.3 启动 Prometheus
- 打开 命令提示符(CMD) 或 PowerShell。
- 进入Prometheus目录并启动:
cd C:\Prometheus .\prometheus.exe --config.file=prometheus.yml
- 访问
http://localhost:9090
验证是否运行。
2. 安装 Grafana
2.1 下载 Grafana
- 访问 Grafana 官方下载页。
- 选择 Windows 版本(如
grafana-10.2.0.windows-amd64.zip
)。 - 解压到本地目录(如
C:\Grafana
)。
2.2 启动 Grafana
- 进入Grafana目录:
cd C:\Grafana\bin
- 启动Grafana:
.\grafana-server.exe
- 访问
http://localhost:3000
(默认账号/密码:admin/admin
)。
3. 配置 Grafana 连接 Prometheus
添加数据源:
- 登录Grafana → 左侧菜单 Configuration → Data Sources → Add data source。
- 选择 Prometheus,填写URL
http://localhost:9090
→ Save & Test。
导入仪表盘:
- 在Grafana中搜索 Dashboard ID(如
1860
是Node Exporter的模板)。 - 左侧菜单 Dashboards → Import → 输入ID → 选择Prometheus数据源。
- 在Grafana中搜索 Dashboard ID(如
4. 监控 Windows 系统(使用 Windows Exporter)
4.1 安装 Windows Exporter
- 下载 windows_exporter。
- 运行安装程序(如
windows_exporter-0.24.0-amd64.msi
)。 - 默认端口为
9182
,访问http://localhost:9182/metrics
验证。
4.2 修改 Prometheus 配置
在 prometheus.yml
中添加:
- job_name: "windows_exporter"
static_configs:
- targets: ["localhost:9182"]
重启Prometheus生效(关闭原CMD窗口,重新运行 prometheus.exe
)。
4.3 在Grafana中查看
导入Windows监控仪表盘(如 Windows Exporter Dashboard)。
5. 设置为 Windows 服务(可选)
5.1 Prometheus 作为服务
- 使用 NSSM(非官方服务管理器):
choco install nssm # 通过Chocolatey安装 nssm install Prometheus "C:\Prometheus\prometheus.exe" --config.file="C:\Prometheus\prometheus.yml" nssm start Prometheus
5.2 Grafana 作为服务
- 在Grafana目录中运行:
nssm install Grafana "C:\Grafana\bin\grafana-server.exe" nssm start Grafana
6. 常见问题
- 端口冲突:检查
9090
(Prometheus)、3000
(Grafana)、9182
(Windows Exporter)是否被占用。 - 防火墙阻止:允许相关端口的入站连接(Windows Defender防火墙)。
- 数据源连接失败:确保Prometheus和Windows Exporter正在运行。
通过以上步骤,你可以在Windows环境下快速搭建完整的监控系统,实时可视化服务器和应用的性能数据!