- 介绍
- Remote Write
- Remote Read
- 使用influxdb作为 Remote Storage
- 参考文档
Prometheus的本地存储设计可以减少其自身运维和管理的复杂度,同时能够满足大部分用户监控规模的需求。但是本地存储也意味着Prometheus无法持久化数据,无法存储大量历史数据,同时也无法灵活扩展和迁移。
为了保持Prometheus的简单性,Prometheus并没有尝试在自身中解决以上问题,而是通过定义两个标准接口(remote_write/remote_read)
,让用户可以基于这两个接口对接将数据保存到任意第三方的存储服务中,这种方式在Promthues中称为Remote Storage。
用户可以在Prometheus配置文件中指定Remote Write(远程写)的URL地址,一旦设置了该配置项,Prometheus将采集到的样本数据通过HTTP的形式发送给适配器(Adaptor)
。而用户则可以在适配器中对接外部任意的服务。外部服务可以是真正的存储系统,公有云的存储服务,也可以是消息队列等任意形式。
如下图所示,Promthues的Remote Read(远程读)也通过了一个适配器实现。在远程读的流程当中,当用户发起查询请求后,Promthues将向remote_read中配置的URL发起查询请求(matchers,ranges)
,Adaptor根据请求条件从第三方存储服务中获取响应的数据。同时将数据转换为Promthues的原始样本数据返回给Prometheus Server。
当获取到样本数据后,Promthues在本地使用PromQL对样本数据进行二次处理。
使用influxdb作为 Remote Storage注意:启用远程读设置后,只在数据查询时有效,对于规则文件的处理,以及Metadata API的处理都只基于Prometheus本地存储完成。
目前Prometheus社区也提供了部分对于第三方数据库的Remote Storage支持:
| 存储服务 | 支持模式 |
|---|---|
| AppOptics | write |
| Chronix | write |
| Cortex | read/write |
| CrateDB | read/write |
| Gnocchi | write |
| Graphite | write |
| InfluxDB | read/write |
| OpenTSDB | write |
| PostgreSQL/TimescaleDB | read/write |
| SignalFx | write |
这里将介绍如何使用Influxdb作为Prometheus的Remote Storage,从而确保当Prometheus发生宕机或者重启之后能够从Influxdb中恢复和获取历史数据。
这里使用docker-compose定义并启动influxdb数据库服务:
version: '3.8'
services:
prometheus:
restart: always
privileged: true
hostname: prometheus
container_name: prometheus
image: prom/prometheus:latest
ports:
- "9090:9090"
volumes:
- ./prom:/etc/prometheus
command:
- '--config.file=/etc/prometheus/config/prometheus.yml'
# 支持热更新
- '--web.enable-lifecycle'
influxdb:
image: influxdb:1.8
container_name: influxdb
hostname: influxdb
restart: always
volumes:
- ./influxdb/data:/var/lib/influxdb:rw
ports:
- "8086:8086"
environment:
TZ: Asia/Shanghai
INFLUXDB_DB: prometheus
INFLUXDB_ADMIN_ENABLED: "true"
INFLUXDB_ADMIN_USER: admin
INFLUXDB_ADMIN_PASSWORD: admin
INFLUXDB_HTTP_LOG_ENABLED: "false"
INFLUXDB_REPORTING_DISABLED: "true"
INFLUXDB_USER: promuser
INFLUXDB_USER_PASSWORD: prompass
编辑 prometheus.yml 配置文件:
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "/etc/prometheus/rules/*.yml"
# - "first_rules.yml"
# - "second_rules.yml"
# The storage database of prometheus is specified as Influxdb
remote_write:
- url: "http://influxdb:8086/api/v1/prom/write?db=prometheus"
basic_auth:
username: promuser
password: prompass
remote_read:
- url: "http://influxdb:8086/api/v1/prom/read?db=promuser"
basic_auth:
username: promuser
password: prompass
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's prometheus itself.
scrape_configs:
# The job name is added as a label `job=` to any timeseries scraped from this config.
- job_name: "prometheus"
static_configs:
- targets: [ "localhost:9090" ]
启动服务:
docker-compose up -d
# 查看
docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------------------
influxdb /entrypoint.sh influxd Up 0.0.0.0:8086->8086/tcp,:::8086->8086/tcp
prometheus /bin/prometheus --config.f ... Up 0.0.0.0:9090->9090/tcp,:::9090->9090/tcp
可以从上图中看出,prometheus采集数据已经写入到influxdb 数据库中。
当数据写入成功后,停止Prometheus服务。同时删除Prometheus的data目录,模拟Promthues数据丢失的情况后重启Prometheus。
打开Prometheus UI如果配置正常,Prometheus可以正常查询到本地存储以删除的历史数据记录。
参考文档prometheus-book



