当前位置:网站首页>总结:Prometheus存储
总结:Prometheus存储
2022-07-27 23:36:00 【小魏的博客】
一、介绍
prometheus 提供了本地存储(TSDB)时序型数据库的存储方式,在2.0版本之后,压缩数据的能力得到了大大的提升(每个采样数据仅仅占用3.5byte左右空间),单节点情况下可以满足大部分用户的需求,但本地存储阻碍了prometheus集群化的实现,因此在集群中应当采用 其他时序性数据来替代,比如influxdb。
prometheus 分为三个部分,分别是:抓取数据、存储数据和查询数据。
在早期有一个单独的项目叫做 TSDB,但是,在2.1.x的某个版本,已经不单独维护这个项目了,直接将这个项目合并到了prometheus的主干上了。
二、案例
我们的prometheus目前还是各个业务单独存储,存储到云端(StorageClass)
如下是创建StorageClass

关键的两个文件:
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: prometheus-data-03
provisioner: prometheus-data-03/nfs
reclaimPolicy: Retain
我理解:部署一个客户端pod,这个客户端会依据上面创建的StorageClass将数据推送到云存储
kind: Deployment
apiVersion: apps/v1
metadata:
name: nfs-provisioner-03
spec:
selector:
matchLabels:
app: nfs-provisioner-03
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-provisioner-03
spec:
serviceAccount: nfs-provisioner
containers:
- name: nfs-provisioner
image: docker-registry.xxx.virtual/hubble/nfs-client-provisioner:v3.1.0-k8s1.11
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: prometheus-data-03/nfs
- name: NFS_SERVER
value: hubble-587ceb02-5e85e802.cnhz1.qfs.xxx.storage
- name: NFS_PATH
value: /hubble-wuhan-lkg
volumes:
- name: nfs-client-root
nfs:
server: hubble-587ceb02-5e85e802.cnhz1.qfs.xxx.storage
path: /hubble-wuhan-lkg
上面的server地址其实就是云存储那边提供的地址:

部署prometheus:
kind: Service
apiVersion: v1
metadata:
name: prometheus-headless
namespace: example-nfs
labels:
app.kubernetes.io/name: prometheus
spec:
type: ClusterIP
clusterIP: None
selector:
app.kubernetes.io/name: prometheus
ports:
- name: web
protocol: TCP
port: 9090
targetPort: web
- name: grpc
port: 10901
targetPort: grpc
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
namespace: example-nfs
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: prometheus-example-nfs
subjects:
- kind: ServiceAccount
name: prometheus
namespace: example-nfs
roleRef:
kind: ClusterRole
name: prometheus
apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: prometheus
namespace: example-nfs
labels:
app.kubernetes.io/name: prometheus
spec:
serviceName: prometheus-headless
podManagementPolicy: Parallel
replicas: 2
selector:
matchLabels:
app.kubernetes.io/name: prometheus
template:
metadata:
labels:
app.kubernetes.io/name: prometheus
spec:
serviceAccountName: prometheus
securityContext:
fsGroup: 1000
runAsUser: 0
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app.kubernetes.io/name
operator: In
values:
- prometheus
topologyKey: kubernetes.io/hostname
containers:
- name: prometheus
image: docker-registry.xxx.virtual/hubble/prometheus:v2.34.0
args:
- --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=1d
ports:
- containerPort: 9090
name: web
protocol: TCP
livenessProbe:
failureThreshold: 6
httpGet:
path: /example-nfs/prometheus/-/healthy
port: web
scheme: HTTP
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 3
readinessProbe:
failureThreshold: 120
httpGet:
path: /example-nfs/prometheus/-/ready
port: web
scheme: HTTP
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 3
resources:
requests:
memory: 1Gi
limits:
memory: 30Gi
volumeMounts:
- mountPath: /etc/prometheus/config_out
name: prometheus-config-out
readOnly: true
- mountPath: /prometheus
name: prometheus-storage
- mountPath: /etc/prometheus/rules
name: prometheus-rules
- name: thanos
image: docker-registry.xxx.virtual/hubble/thanos:v0.25.1
args:
- 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_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
ports:
- name: http-sidecar
containerPort: 10902
- name: grpc
containerPort: 10901
livenessProbe:
httpGet:
port: 10902
path: /-/healthy
readinessProbe:
httpGet:
port: 10902
path: /-/ready
volumeMounts:
- name: prometheus-config-tmpl
mountPath: /etc/prometheus/config
- name: prometheus-config-out
mountPath: /etc/prometheus/config_out
- name: prometheus-rules
mountPath: /etc/prometheus/rules
- name: prometheus-storage
mountPath: /prometheus
volumes:
- name: prometheus-config-tmpl
configMap:
defaultMode: 420
name: prometheus-config-tmpl
- name: prometheus-config-out
emptyDir: {}
- name: prometheus-rules
configMap:
name: prometheus-rules
volumeClaimTemplates:
- metadata:
name: prometheus-storage
labels:
app.kubernetes.io/name: prometheus
spec:
storageClassName: prometheus-data-03
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 100Gi
limits:
storage: 300Gi
三、存储原理
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=trueskip_head作用:是否跳过存留在内存中还未写入磁盘中的数据,仍在block块中的数据, 默认是false
五、数据还原
利用api方式制作成snapshot后,还原时将snapshot中的文件覆盖到data目录下,重启prometheus即可!
添加定时备份任务(每周日3点备份)
crontable -e #注意时区,修改完时区后,需要重启 crontab systemctl restart cron
0 3 * * 7 sudo /usr/bin/curl -XPOST -I http://127.0.0.1:9090/api/v1/admin/tsdb/snapshot >> /home/bill/prometheusbackup.log边栏推荐
- 【分布式开发】之 CAP 原则
- 逻辑回归原理
- Software testing interview question: where do your performance testing requirements come from?
- 对迁移学习中域适应的理解和3种技术的介绍
- ICML2022 | 在线决策Transformer
- 迅为i.MX6ULL开发板Qt系统移植-交叉编译Qt代码
- Rviz uses arbotix to control robot motion
- 面试题 01.06. 字符串压缩
- Thoroughly understand kubernetes scheduling framework and plug-ins
- Redefine analysis - release of eventbridge real-time event analysis platform
猜你喜欢

字节月薪28K,分享一波我的自动化测试经验....

Summary of common shortcut keys in idea

BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)

“你“想当测试/开发程序员吗?努力发芽的我们......

Qt 绘制系统简介
![[game] Nintendo Nintendo switch ultra detailed purchase / use guide and precautions (continuous update according to your own use...)](/img/7e/9e0e17e2ea8b8679ad7e1750a8b6d1.png)
[game] Nintendo Nintendo switch ultra detailed purchase / use guide and precautions (continuous update according to your own use...)

ICML2022 | 在线决策Transformer

"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 supplementary problem solution (a, C, J)

Knowledge of two-dimensional array

现货白银如何计算盈亏
随机推荐
PHP利用某些函数bypass waf探讨
Leetcode 2347. the best poker hand
Dart 代码注释和文档编写规范
华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
C language main function transfer parameters
面试题 01.06. 字符串压缩
From functional testing to automated testing, my monthly salary has exceeded 30k+, and I have 6 years of testing experience.
登录功能实现
实现OCR语言识别Demo(二)- 图片及识别内容的展现和交互
Nokia announces cooperation with Broadcom to develop 5g chip
MATLAB 44种动漫渐变色绘图程序
ABAP CDs table function introduction and examples
LeetCode 2347. 最好的扑克手牌
使用Gateway的流式api修改请求路径
How to make digital retail undertake the development task of the era of traffic and retention may be the key
Gazebo control example
Qt 绘制系统简介
普通设备能不能接入TSN时间敏感网络?
如何让数字零售承接起流量时代和留量时代的发展重任,或许才是关键所在
Matlab 44 animation gradient drawing programs