安装Prometheus
- 下载Prometheus:从官方网站或Github下载Prometheus的二进制文件。
- 安装(Windows环境):
- 将Prometheus.exe放在
C:\Prometheus目录下。 - 在命令提示符中运行以下命令,添加Prometheus到PATH:
SET PATH=%PATH%;C:\Prometheus
- 将Prometheus.exe放在
- 启动Prometheus:
- 在命令提示符中运行
prometheus.exe。 - 或者通过服务管理器启动Prometheus服务。
- 在命令提示符中运行
配置Prometheus
-
配置文件位置:默认配置文件位于
C:\Prometheus\config\prometheus.yml。 -
编辑配置文件:
global: scrape_interval: 15s # 数据收集间隔 remote_write: true # 启用远程写入 scraping: - job_name: 'prometheus' scrape_configs: - job: 'prometheus' static_configs: targets: ['localhost:909'] alerting: - receiver: 'alertmanager' configurations: - receiver: 'alertmanager' configurations: - receiver: 'slack' channel: '#prometheus-alerts' send_resolved: true -
保存并验证配置:确保配置文件正确,使用YAML validator工具检查。
设置数据收集器
- HTTP数据源:
scraping: - job_name: 'http-server' scrape_configs: - job: 'http-server' static_configs: targets: ['http://localhost:808'] scrape_interval: 5s - JMX数据源:
- 安装JMX Exporter。
- 在配置文件中添加:
scraping: - job_name: 'jmx' scrape_configs: - job: 'jmx' static_configs: targets: ['localhost:1099'] collector: 'jmx'
配置警报规则
- 定义警报:
alerting: - receiver: 'alertmanager' configurations: - receiver: 'slack' channel: '#prometheus-alerts' send_resolved: true match: metric: 'metric_name' metric_value: 'threshold' labels: metric_name: 'cpu_usage' instance: '{{ template "instance" . }}' expression: '{{ re "(\d+)(\s)(\d+)") }}')receiver:指定警报接收器(如Alertmanager)。match:定义触发警报的条件。
配置数据存储(可选)
- 持久化存储(InfluxDB):
- 安装并配置InfluxDB。
- 在Prometheus配置中添加:
storage: 'influxdb': scheme: 'influxdb' data_source: 'localhost:8086' database: 'prometheus' retention: '1d'
集成Grafana
- 配置Grafana数据源:
- 运行Grafana并登录。
- 在Grafana的数据源配置中添加Prometheus:
- 名称:Prometheus
- 类型:Prometheus
- URL:
http://localhost:909 - 数据源名:
prometheus
配置远程调度
- 添加远程任务:
remote_write: - job_name: 'remote-prometheus' targets: - 'http://remote-prometheus:909' scrape_config: interval: '5m' metric scrape configs: - job: 'http-endpoints' static_configs: targets: ['http://localhost:800']
测试和验证
- 运行Prometheus:确保Prometheus正常运行,数据源可用。
- 查询数据:使用Prometheus Query Language(PQL)测试数据,
prometheus query 'metric_name {instance="my-instance"}' - 检查警报:确保警报规则正确触发,配置接收器和渠道。
优化和监控
- 调整配置:根据需求优化
scrape_interval和样本存储。 - 监控Prometheus:使用Prometheus自身指标监控性能,确保没有性能瓶颈。
备份和恢复
- 备份配置文件:定期备份
C:\Prometheus\config\prometheus.yml以便恢复。
通过以上步骤,您可以成功配置Prometheus,收集和监控您的系统数据,创建可视化仪表盘,并设置自动化警报,继续探索Prometheus的高级功能,如多数据源集成、远程调度和多租户支持,以进一步提升监控能力。









