热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

prometheus持久化到mysql_Prometheus数据持久化存储(二)

Prometheus(普罗米修斯)Prometheus数据持久化存储(二)PrometheusInfluxGrafanaMysql数据持久化存储Prometheus的存储数据库默认

Prometheus(普罗米修斯)

Prometheus数据持久化存储(二)

Prometheus+Influx+Grafana+Mysql数据持久化存储

Prometheus的存储数据库默认只保留15天的数据,Grafana存储的配置以及图表都还在容器之中它自己生成的sqlit数据库中长期存储并不友好。

为了更适应老板的需求,长期存储数据,我们可以添加时序数据库InfluxDB作为prometheus后端存储,可以修改grafana的存储为mysql以便自己做一些自定义操作的时候方便一点。

Prometheus+Influx存储数据

cat docker-compose-prometheus-influxdb.yml

version: "2.3"

services:

prometheus:

image: prom/prometheus:latest

container_name: prometheus

hostname: prometheus

restart: always

volumes:

- /prometheus/prometheus/config:/etc/prometheus

- /prometheus/prometheus/data:/prometheus

- /etc/localtime:/etc/localtime

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=15d'

- '--query.max-concurrency=50'

- '--web.enable-lifecycle'

ports:

- "9090:9090"

depends_on:

- influxdb

logging:

driver: "json-file"

options:

max-size: "1g"

networks:

- prom_monitor

influxdb:

image: influxdb:latest

container_name: influxdb

hostname: influxdb

restart: always

volumes:

- /prometheus/prometheus/influxdb/config:/etc/influxdb

- /prometheus/prometheus/influxdb/data:/var/lib/influxdb/data

- /etc/localtime:/etc/localtime

ports:

- "8086:8086"

- "8083:8083"

environment:

- INFLUXDB_DB=prometheus

- INFLUXDB_ADMIN_ENABLED=true

- INFLUXDB_ADMIN_USER=admin

- INFLUXDB_ADMIN_PASSWORD=adminpwd

- INFLUXDB_USER=prometheus

- INFLUXDB_USER_PASSWORD=prometheuspwd

- INFLUXDB_CONFIG_PATH=/etc/influxdb/influxdb.conf

logging:

driver: "json-file"

options:

max-size: "1g"

networks:

- prom_monitor

networks:

prom_monitor:

driver: bridge

mkdir -p influxdb/data influxdb/config

cat influxdb/config/influxdb.conf

[meta]

dir = "/var/lib/influxdb/meta"

[data]

dir = "/var/lib/influxdb/data"

engine = "tsm1"

wal-dir = "/var/lib/influxdb/wal"

通过以上文件看出,重新指定了Prometheus的存储数据库为InfluxDB,当然,还需要配置Prometheus.yml文件。

cat config/prometheus.yml

在Prometheus的配置文件中,添加远程读写。

# 指定了Prometheus的存储数据库为InfluxDB

remote_write:

- url: "http://influxdb:8086/api/v1/prom/write?db=prometheus&u=prometheus&p=prometheuspwd"

remote_read:

- url: "http://influxdb:8086/api/v1/prom/read?db=prometheus&u=prometheus&p=prometheuspwd"

其它配置:

cat config/prometheus.yml

添加如下配置

scrape_configs:

- job_name: 'linux-server'# 定义任务名

scrape_interval: 5s

static_configs:

file_sd_configs:

- files:

- '/etc/prometheus/fileconfig/*-nodes.json'

file_sd_configs启动prometheus的文件发现功能,可以将配置主机等信息写入文件中,如果用户修改了配置,比如增加或删除了某些主机,那么Prometheus会自动发现这些更改。

cat config/fileconfig/office-nodes.json

[

{

"targets": ["192.168.1.20:9100"],

"labels": {

"instance": "192.168.1.20",

"alias": "office0",

"job": "node"

}

},

{

"targets": ["192.168.1.17:9111"],

"labels": {

"instance": "192.168.1.17",

"alias": "office3",

"job": "node"

}

}

]

登录influxdb数据库查看相应的数据信息

docker exec -it influxdb bash

influx

use prometheus

show MEASUREMENTS

select * from up

可以查看到相关数据。

完整的prometheus.yml文件

global:

scrape_interval: 15s # 默认抓取间隔, 15秒向目标抓取一次数据。

external_labels:

monitor: 'codelab-monitor'

# 这里表示抓取对象的配置

scrape_configs:

- job_name: 'linux-server'# 定义任务名

scrape_interval: 5s

static_configs:

file_sd_configs:

- files:

- '/etc/prometheus/fileconfig/*-nodes.json'

# 指定了Prometheus的存储数据库为InfluxDB

remote_write:

- url: "http://influxdb:8086/api/v1/prom/write?db=prometheus&u=prometheus&p=prometheuspwd"

remote_read:

- url: "http://influxdb:8086/api/v1/prom/read?db=prometheus&u=prometheus&p=prometheuspwd"

# 配置告警规则位置

rule_files:

- "rules/*rules.yml"

# 配置alertmanager告警发送消息配置

alerting:

alertmanagers:

- static_configs:

- targets: ['192.168.1.20:9093']

附加:

influxdb时序数据库的基本命令

# influxdb相关操作

#登录数据库

influx

influx -host 127.0.0.1 -port 8086 -username prometheus -password prometheuspwd

#查看数据库

show databases

#切换数据库

use prometheus

#查看数据表

show MEASUREMENTS

#查看数据

select * from up

#查看用户

show users

#创建用户

create user "username" with password 'password' with all privileges

#删除用户

drop user username

Grafana+Mysql存储数据

cat docker-compose-grafana-mysql.yml

version: "2.3"

services:

grafana:

image: grafana/grafana:latest

container_name: grafana

hostname: grafana

restart: always

volumes:

- /prometheus/grafana/config:/etc/grafana

- /prometheus/grafana/logs:/var/log/grafana

- /prometheus/grafana/data:/var/lib/grafana

- /prometheus/grafana/dashboards:/etc/grafana/provisioning/dashboards

- /etc/localtime:/etc/localtime

ports:

- "3000:3000"

user: "104"

depends_on:

- db

networks:

- prom_monitor

db:

image: mysql:5.7

command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

container_name: mysql

hostname: mysql

restart: always

environment:

- MYSQL_ROOT_PASSWORD=Admin_pwd

- MYSQL_DATABASE=grafana

- MYSQL_USER=grafana

- MYSQL_PASSWORD=grafana

volumes:

- /prometheus/grafana/mysql:/var/lib/mysql

- /etc/localtime:/etc/localtime

expose:

- "3306"

networks:

- prom_monitor

networks:

prom_monitor:

driver: bridge

grafana配置文件增加数据库信息

cat config/grafana.ini

[database]

type = mysql

host = mysql:3306

name = grafana

user = grafana

password = grafana

url = mysql://grafana:grafana@mysql:3306/grafana

[session]

provider = mysql

provider_config = `grafana:grafana@tcp(mysql:3306)/grafana`

COOKIE_name = grafana_session

COOKIE_secure = false

session_life_time = 86400

启动服务

docker-compose -f docker-compose-grafana-mysql.yml up -d

登录数据库查看信息

docker exec -it mysql bash

mysql -ugrafana -pgrafana

mysql> use grafana;

mysql> show tables;

可查看到相关的数据表



推荐阅读
author-avatar
____洋果子
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有