一、介绍

prometheus 提供了本地存储(TSDB)时序型数据库的存储方式,在2.0版本之后,压缩数据的能力得到了大大的提升(每个采样数据仅仅占用3.5byte左右空间),单节点情况下可以满足大部分用户的需求,但本地存储阻碍了prometheus集群化的实现,因此在集群中应当采用 其他时序性数据来替代,比如influxdb。

prometheus 分为三个部分,分别是:抓取数据存储数据查询数据

在早期有一个单独的项目叫做 TSDB,但是,在2.1.x的某个版本,已经不单独维护这个项目了,直接将这个项目合并到了prometheus的主干上了。

二、案例

我们的prometheus目前申请了三个文件系统NFS,托管集群存在一个文件系统,QKE集群存在另一个文件系统,单都是存储到云端(StorageClass)。

存储的方式:每个prometheus实例对应一个目录,如果有多个prometheus实例,则对应多个目录。

另外,注意,每个文件系统对应一个StorageClass自由对象。

以prometheus托管集群为例,如下是创建StorageClass

关键的两个文件:

创建StorageClass资源对象:

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:name: prometheus-data-03
provisioner: prometheus-data-03/nfs
reclaimPolicy: Retain

创建StorageClass后,还需要K8S存储插件进行真正的存储工作:

nfs-client-provisioner 可动态为kubernetes提供pv卷,是Kubernetes的简易NFS的外部provisioner,本身不提供NFS,需要现有的NFS服务器提供存储。持久卷目录的命名规则为:${namespace}-${pvcName}-${pvName}。

树莓派k8s集群安装nfs-client-provisioner_崔一凡的技术博客_51CTO博客

kind: Deployment
apiVersion: apps/v1
metadata:name: nfs-provisioner-03
spec:selector:matchLabels:app: nfs-provisioner-03replicas: 1strategy:type: Recreatetemplate:metadata:labels:app: nfs-provisioner-03spec:serviceAccount: nfs-provisionercontainers:- name: nfs-provisionerimage: docker-registry.xxx.virtual/hubble/nfs-client-provisioner:v3.1.0-k8s1.11volumeMounts:- name: nfs-client-rootmountPath: /persistentvolumesenv:- name: PROVISIONER_NAMEvalue: prometheus-data-03/nfs- name: NFS_SERVERvalue: hubble-587ceb02-5e85e802.cnhz1.qfs.xxx.storage- name: NFS_PATHvalue: /hubble-wuhan-lkgvolumes:- name: nfs-client-rootnfs:server: hubble-587ceb02-5e85e802.cnhz1.qfs.xxx.storagepath: /hubble-wuhan-lkg

上面的server地址其实就是云存储那边提供的地址:

部署prometheus:

kind: Service
apiVersion: v1
metadata:name: prometheus-headlessnamespace: example-nfslabels:app.kubernetes.io/name: prometheus
spec:type: ClusterIPclusterIP: Noneselector:app.kubernetes.io/name: prometheusports:- name: webprotocol: TCPport: 9090targetPort: web- name: grpcport: 10901targetPort: grpc
---apiVersion: v1
kind: ServiceAccount
metadata:name: prometheusnamespace: example-nfs---apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:name: prometheus-example-nfs
subjects:- kind: ServiceAccountname: prometheusnamespace: example-nfs
roleRef:kind: ClusterRolename: prometheusapiGroup: rbac.authorization.k8s.io
---apiVersion: apps/v1
kind: StatefulSet
metadata:name: prometheusnamespace: example-nfslabels:app.kubernetes.io/name: prometheus
spec:serviceName: prometheus-headlesspodManagementPolicy: Parallelreplicas: 2selector:matchLabels:app.kubernetes.io/name: prometheustemplate:metadata:labels:app.kubernetes.io/name: prometheusspec:serviceAccountName: prometheussecurityContext:fsGroup: 1000runAsUser: 0affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: app.kubernetes.io/nameoperator: Invalues:- prometheustopologyKey: kubernetes.io/hostnamecontainers:- name: prometheusimage: docker-registry.xxx.virtual/hubble/prometheus:v2.34.0args:- --config.file=/etc/prometheus/config_out/prometheus.yaml- --storage.tsdb.path=/prometheus- --storage.tsdb.retention.time=30d- --web.external-url=/example-nfs/prometheus- --web.enable-lifecycle- --storage.tsdb.no-lockfile- --storage.tsdb.min-block-duration=2h- --storage.tsdb.max-block-duration=1dports:- containerPort: 9090name: webprotocol: TCPlivenessProbe:failureThreshold: 6httpGet:path: /example-nfs/prometheus/-/healthyport: webscheme: HTTPperiodSeconds: 5successThreshold: 1timeoutSeconds: 3readinessProbe:failureThreshold: 120httpGet:path: /example-nfs/prometheus/-/readyport: webscheme: HTTPperiodSeconds: 5successThreshold: 1timeoutSeconds: 3resources:requests:memory: 1Gilimits:memory: 30GivolumeMounts:- mountPath: /etc/prometheus/config_outname: prometheus-config-outreadOnly: true- mountPath: /prometheusname: prometheus-storage- mountPath: /etc/prometheus/rulesname: prometheus-rules- name: thanosimage: docker-registry.xxx.virtual/hubble/thanos:v0.25.1args:- sidecar- --tsdb.path=/prometheus- --prometheus.url=http://127.0.0.1:9090/example-nfs/prometheus- --reloader.config-file=/etc/prometheus/config/prometheus.yaml.tmpl- --reloader.config-envsubst-file=/etc/prometheus/config_out/prometheus.yaml- --reloader.rule-dir=/etc/prometheus/rules/env:- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.nameports:- name: http-sidecarcontainerPort: 10902- name: grpccontainerPort: 10901livenessProbe:httpGet:port: 10902path: /-/healthyreadinessProbe:httpGet:port: 10902path: /-/readyvolumeMounts:- name: prometheus-config-tmplmountPath: /etc/prometheus/config- name: prometheus-config-outmountPath: /etc/prometheus/config_out- name: prometheus-rulesmountPath: /etc/prometheus/rules- name: prometheus-storagemountPath: /prometheusvolumes:- name: prometheus-config-tmplconfigMap:defaultMode: 420name: prometheus-config-tmpl- name: prometheus-config-outemptyDir: {}- name: prometheus-rulesconfigMap:name: prometheus-rulesvolumeClaimTemplates:- metadata:name: prometheus-storagelabels:app.kubernetes.io/name: prometheusspec:storageClassName: prometheus-data-03accessModes:- ReadWriteOncevolumeMode: Filesystemresources:requests:storage: 100Gilimits:storage: 300Gi

--storage.tsdb.min-block-duration:控制数据落盘的时间,即最小落盘时间是2小时

--storage.tsdb.min-block-duration:控制数据落盘的时间,即最大落盘时间是1天

--storage.tsdb.retention.time配置本地保留30天数据,减少空间占用;

如上,volumeClaimTemplates即是定义一个PVC,如何创建PV呢?使用StorageClass定义的模板去创建PV。

那么prometheus查询数据的时候是怎么查的呢?

其实对于prometheus来说,就是从prometheus这个实例提供的接口查询数据而已,和普通的容器一样,不管是云存储还是node存储或是其他存储,对于容器来说是透明的,都是从挂载到的目录去查询,所以远端对于prometheus来说就是查本地的目录文件。

三、存储原理

prometheus将采集到的样本以时间序列的方式保存在内存(TSDB 时序数据库)中,并定时保存到硬盘中。

与zabbix不同,zabbix会保存所有的数据,而prometheus本地存储会保存15天,超过15天以上的数据将会被删除,若要永久存储数据,有两种方式:

  • 方式一:修改prometheus的配置参数“storage.tsdb.retention.time=10000d”;
  • 方式二:将数据引入存储到Influcdb中。

prometheus按照block块的方式来存储数据,每2小时为一个时间单位,首先会存储到内存中,当到达2小时后,会自动写入磁盘中。

为防止程序异常而导致数据丢失,采用了WAL机制,即2小时内记录的数据存储在内存中的同时,还会记录一份日志,存储在block下的wal目录中。当程序再次启动时,会将wal目录中的数据写入对应的block中,从而达到恢复数据的效果。

当删除数据时,删除条目会记录在tombstones 中,而不是立刻删除。

prometheus采用的存储方式称为“时间分片”,每个block都是一个独立的数据库。优势是可以提高查询效率,查哪个时间段的数据,只需要打开对应的block即可,无需打开多余数据。

四、数据备份

1、完全备份

  备份prometheus的data目录可以达到完全备份的目的,但效率较低。

2、快照备份

prometheus提供了一个功能,是通过API的方式,快速备份数据。实现方式:

  • 首先,修改prometheus的启动参数,新增以下两个参数:
--storage.tsdb.path=/usr/local/share/prometheus/data \
--web.enable-admin-api
  • 然后,重启prometheus
  • 最后,调用接口备份:
# 不跳过内存中的数据,即同时备份内存中的数据
curl -XPOST http://127.0.0.1:9090/api/v2/admin/tsdb/snapshot?skip_head=false
# 跳过内存中的数据
curl -XPOST http://127.0.0.1:9090/api/v2/admin/tsdb/snapshot?skip_head=true

skip_head作用:是否跳过存留在内存中还未写入磁盘中的数据,仍在block块中的数据, 默认是false

五、数据还原

利用api方式制作成snapshot后,还原时将snapshot中的文件覆盖到data目录下,重启prometheus即可!

添加定时备份任务(每周日3点备份)

crontable -e                              #注意时区,修改完时区后,需要重启 crontab    systemctl  restart cron0 3 * * 7 sudo /usr/bin/curl -XPOST -I http://127.0.0.1:9090/api/v1/admin/tsdb/snapshot >> /home/bill/prometheusbackup.log

总结:Prometheus存储相关推荐

  1. prometheus存储模型

    Prometheus是目前被广泛使用的容器云监控系统,其底层实现了一个时序数据库(tsdb),它拥有强大的数据压缩和检索能力,可轻松应对每秒百万级的数据采集.本文基于最新的prometheus 2.1 ...

  2. Prometheus实战--Uber背书的存储解决方案M3

    前言 前面我们一直在做prometheus的远程存储的工作,一直缺乏一种大厂背书的解决方案.具体可能有下面的几点: 大厂背书且开源 可承接大规模海量metrics 非weave cortex这种对pr ...

  3. Prometheus 如何做到“活学活用”,大牛总结的避坑指南

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 作者丨徐亚松 来源丨http://www.xuyasong ...

  4. 监控神器Prometheus用不对,也就是把新手村的剑

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者丨徐亚松 来源丨http://www.xuyasong.com ...

  5. 高可用 Prometheus 架构实践中的踩坑集锦

    监控系统的历史悠久,是一个很成熟的方向,而 Prometheus 作为新生代的开源监控系统,慢慢成为了云原生体系的事实标准,也证明了其设计很受欢迎. 本文主要分享在 Prometheus 实践中遇到的 ...

  6. 高可用 Prometheus 的常见问题

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 监控系统的历史悠久,是一个很成熟的方向,而 Promet ...

  7. 开源监控系统 Prometheus 入门

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | 公众号「yangyidba」 一 简介 Pro ...

  8. 应用监控指标采集器 Prometheus 核心介绍

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 作者 | 码农小胖哥 来源 | 公众号「码农小胖哥」 Prometheus是一个最初在Sou ...

  9. prometheus简介

    一.prometheus简介 1.1 什么是prometheus? Prometheus是一个开源监控系统,它前身是SoundCloud的警告工具包.从2012年开始,许多公司和组织开始使用Prome ...

最新文章

  1. NodeJs教程(介绍总结!)终于在网上找到一个靠谱点的了T_T
  2. NOIP2016提高组复赛解题报告
  3. centos7添加防火墙端口
  4. Spring Boot 中使用 MyBatis 整合 Druid 多数据源
  5. 防止在多模块Maven中找到“未找到插件”
  6. 3 CO配置-企业结构-分配-把公司代码分配给成本控制范围
  7. python如何爬取图片到指定文件夹论文_基于Python的图片爬虫程序设计
  8. 三十、K8s供应链安全1-准入控制器
  9. JQuery小插件,Selected插件1
  10. 魔都上海为什么被称为“魔都”
  11. Exsi主机判断UPS状态自动进行关机
  12. ip经济ip猫:「飞海豚」大圣基因/医修鸽的ValueUP | Chain++
  13. F - Color the ball
  14. 2020年中国科技行业最可能发生的38件事
  15. 回顾这两个月的面试总结,不断的试错不断地坚持!!你也可以的
  16. matlab中S-function的概念及使用
  17. java计算机毕业设计后勤招标采购管理系统2021源码+mysql数据库+系统+lw文档+部署
  18. 如何做网站推广-外链为皇
  19. 深入浅出TCP/UDP 原理-UDP篇(2)及完整MATLAB实现UDP通信
  20. 关于添加SATA驱动的回复(XP安装时分两个阶段)

热门文章

  1. IE浏览器输入框反应慢的解决方式
  2. appium测试Cannot start the ‘XXX‘ application报错,无法打开app
  3. python基础教程免费下载-Python基础教程第三版PDF电子书免费下载
  4. IP地址,域名,服务器
  5. a king读后感 love of the_作业:读后感~Love, The One Creative Force~
  6. cocos creator 制作作砸金蛋
  7. 千寻位置NTRIP网络基准站
  8. Landsat影像的辐射定标和大气校正
  9. 联想miix2-11 linux,联想Miix2 11电脑硬盘分区详细教程
  10. 济南计算机学院排名,济南市最好的10所大学排名,山大第1,山东师范第3,济南大学第7...