为了创建一个基于Docker的Prometheus监控系统,首先我们需要在喜欢的位置创建一个项目根目录。
示例:
[root@demo-78 ~]# mkdir /data/prometheus
接下来,我们需要在项目根目录创建三个配置文件:docker-compose.yml、prometheus.yml和node_down.yml。 以下是详细的配置信息。
prometheus.yml配置:
global:
scrape_interval: 15s
evaluation_interval: 15s
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets: ['10.0.5.78:9093']
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "node_down.yml"
# Scrape configuration
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['10.0.5.78:9094']
- job_name: 'redis'
static_configs:
- targets: ['10.0.5.78:9121']
labels:
instance: redis
...
#基于文件自动加载新监控任务
- job_name: 'file_ds'
file_sd_configs:
- files: ['/etc/prometheus/reload/*.yml']
refresh_interval: 5s
docker-compose.yml配置:
version: '3'
networks:
monitor:
driver: bridge
services:
prometheus:
image: prom/prometheus
container_name: prometheus
hostname: prometheus
restart: always
volumes:
- /prometheus/reload:/etc/prometheus/reload
- /prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
- /prometheus/node_down.yml:/etc/prometheus/node_down.yml
ports:
- "9094:9090"
networks:
- monitor
...
node_down.yml配置:
groups:
- name: node_down
rules:
- alert: InstanceDown
expr: up == 0
for: 1m
labels:
user: test
annotations:
summary: "Instance {{ $labels.instance }} down"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."
以上是Docker容器的配置,接下来我们需要启动容器。
# 启动容器
[root@demo-78 prometheus]# docker-compose up -d
请自行执行命令查看容器是否成功启动。
常见问题
-
如何使用prometheus.yml配置的"基于文件自动加载新监控任务"?
项目启动后,根目录会生成一个名叫reload的文件夹,只需在此文件夹中添加新增的监控配置,即可自动注册到prometheus服务中。配置书写格式如下:
[root@demo-78 ~]# cat /prometheus/reload/web.yml - targets: - '10.0.5.79:9100' labels: job: 'pig-web' __metrics_path__: '/web/actuator/prometheus'
或:
[root@demo-piggpt-78 ~]# cat /prometheus/reload/mysql.yml - targets: ['10.0.5.78:9104']
这里只是给个实例,有特别的需求请自行查看资料,这两种写法唯一的区别在于:第一种写法我需要自定义metrics_path,否则默认拉取指标的路径(第二种写法)就是:10.0.5.78:9104/metrics。对于很多官网的exporter,使用第二种方式即可。
-
容器mysql-exporter启动报错:caller=mysqld_exporter.go:225 level=info msg="Error parsing host config" file=.my.cnf err="no configuration found"
由于mysql-exporter更新过后,配置写法有改变。截止2024年5月28日,最新的mysql-exporter:0.15.1按照当前配置部署能够成功启动,如遇无法启动,请自行查看官方文档。