使用docker-compose部署Prometheus和InfluxDB

官方

https://www.influxdata.com/blog/prometheus-remote-write-support-with-influxdb-2-0/

参考博客

https://www.cnblogs.com/zongxiang/p/12981592.html

https://blog.yasking.org/a/telegraf-influxdb-grafana.html

https://blog.csdn.net/catoop/article/details/127533069

Prometheus权限问题

https://github.com/prometheus/prometheus/issues/5976

部署InfluxDB2数据库以及Telegraf

InfluxDB2.x引入了Telegraf,必须使用Telegraf才能将Prometheus的数据写入InfluxDB2.x中
部署方法请参考博客的使用Telegraf+InfluxDB构建监控

部署Prometheus

Prometheus的存储数据库默认只保留15天的数据
因为后期可能将数据迁移到其它机器,而现在这台机器做服务,所以我选择加一个时序数据库方便迁移。
Prometheus+InfluxDB实现监控数据的持久化

获取配置文件

1
2
3
4
5
6
7
8
9
#Prometheus
sudo docker run -d prom/prometheus:v2.46.0
sudo docker cp 容器ID:/etc/prometheus/prometheus.yml /data/docker/prometheus/prometheus/config/prometheus.yml
#获取完配置删掉容器

#Alertmanager
sudo docker run -d prom/alertmanager:v0.26.0
sudo docker cp 容器ID:/etc/alertmanager/alertmanager.yml /data/docker/prometheus/alertmanager/alertmanager.yml

编辑prometheus的docker-compose.yaml文档

docker-compose-prometheus.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
version: "3"
services:
prometheus:
image: prom/prometheus:v2.46.0
user: "1000:1000" #或者指定 user: root,否则运行容器无法启动,log提示没有权限
container_name: prometheus
hostname: prometheus
restart: always
volumes:
- /data/docker/prometheus/prometheus/config:/etc/prometheus:rw
- /data/docker/prometheus/prometheus/data:/prometheus:rw
- /etc/localtime:/etc/localtime:ro
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--log.level=info'
- '--web.listen-address=0.0.0.0:9090'
- '--storage.tsdb.path=/prometheus'
- '--storage.tsdb.retention=30d'
- '--query.max-concurrency=30'
- '--web.enable-lifecycle'
logging:
driver: "json-file"
options:
max-size: "1g"
ports:
- "9090:9090"
networks:
- prom_monitor
alertmanager:
image: prom/alertmanager:v0.26.0
container_name: alertmanager
hostname: alertmanager
volumes:
- /data/docker/prometheus/alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml
ports:
- "9093:9093"
networks:
- prom_monitor
networks:
prom_monitor:
driver: bridge

修改prometheus配置文件

vim /data/docker/prometheus/prometheus/config/prometheus.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
scrape_interval: 5s

# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.

static_configs:
- targets: ["localhost:9090"]

- job_name: node
static_configs:
- targets: ['10.20.12.1:9100']

remote_write:
- url: "http://10.20.10.4:8087/receive"
# 数据写入telegraf,8087是下面Telegraf配置中设置的监听端口

修改Telegraf的配置文件

官方说明
https://www.influxdata.com/blog/prometheus-remote-write-support-with-influxdb-2-0/
vim telegraf.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#配置监听端口和path,使之可以接受数据,这里会应用到Prometheus的配置文件中
[[inputs.http_listener_v2]]
## Address and port to host HTTP listener on
service_address = ":8087"
## Path to listen to.
path = "/receive"
## Data format to consume.
data_format = "prometheusremotewrite"

#读取Prometheus这个服务自身的指标数据进行测试(跟 Prometheus 写入数据到 InfluxDB 没有关系)
#[[inputs.prometheus]]
#采集Prometheus这个服务的指标数据,可以观察到能否正常写入数据到bucket中
#该url为本例安装的Prometheus服务的指标地址,数组格式可以是多个
# urls = ["http://10.20.10.4:9090/metrics"]

这里服务绑定了8087端口,要在influxdb的docker-compose中添加端口映射
具体配置文件请看使用Telegraf+InfluxDB构建监控

1
2
3
4
5
ports:
- "8125:8125/udp"
- "8092:8092/udp"
- "8094:8094/tcp"
- "8087:8087/tcp"

重新部署telegraf

回到/data/docker/database/influxdb2_1/influxdb2_1重新部署influxdb目录下的docker-compose的telegraf

1
sudo docker-compose up -d telegraf

docker-compose部署Prometheus

回到Prometheus的docker-compose所在目录执行docker-compose
/data/docker/prometheus

1
sudo docker-compose -f docker-compose-prometheus.yaml up -d

在InfluxDB2.0中已经可以看到Prometheus获取的数值

部署完成