通常在k8s中,pod是随时可以被替换的,在整个环境里往往我们不太关注某一条鱼,只关注整个鱼群的状态,因此监控数据不会存储太长,因为借鉴意义并不大。但是有时的确想要从 Prometheus 中删除一些指标,如果这些指标不需要,或者只需要释放一些磁盘空间。
Prometheus 中的时间序列只能通过管理 HTTP API 删除(默认禁用)。
--web.enable-admin-api
As of Prometheus 2.0, the --web.enable-admin-api
flag controls access to the administrative HTTP API which includes functionality such as deleting time series. This is disabled by default. If enabled, administrative and mutating functionality will be accessible under the /api/*/admin/
paths. The --web.enable-lifecycle
flag controls HTTP reloads and shutdowns of Prometheus. This is also disabled by default. If enabled they will be accessible under the /-/reload
and /-/quit
paths.
要启用它--web.enable-admin-api
,根据安装方法通过启动脚本或 docker-compose 文件将标志传递给 Prometheus。
删除时间序列指标
使用以下语法删除与某个标签匹配的所有时间序列指标:
curl -X POST
-g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={name="linuxea.com"}'
要删除与 somejob或匹配的时间序列指标instance,请运行:
curl −X POST −g 'http://localhost:9090/api/v1/admin/tsdb/deleteseries?match[]=job="nodeexporter"'
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={job="node_exporter"}'
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={instance="192.168.0.1:9100"}'
要从 Prometheus 中删除所有数据,请运行:
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={name=~".+"}'
请注意,上述 API 调用不会立即删除数据
实际数据仍然存在于磁盘上,将在未来的压缩中被清除。
要确定何时删除旧数据,使用--storage.tsdb.retention
选项 eg --storage.tsdb.retention='365d'
(默认情况下,Prometheus 将数据保留 15 天)
要完全删除通过delete_series
发送clean_tombstones
API 调用删除的数据:
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/clean_tombstones'
删除所有历史数据
我们使用的kube-prometheus,添加--web.enable-admin-api
方式有所不同
打开prometheus-prometheus.yaml 添加enableAdminAPI: true
字段
probeNamespaceSelector: {}
probeSelector: {}
replicas: 2
resources:
requests:
memory: 400Mi
enableAdminAPI: true
ruleSelector:
matchLabels:
prometheus: k8s
而后kubectl apply -f prometheus-prometheus.yaml
,pod起来后查看
containers:
- args:
- '--web.console.templates=/etc/prometheus/consoles'
- '--web.console.libraries=/etc/prometheus/console_libraries'
- '--config.file=/etc/prometheus/config_out/prometheus.env.yaml'
- '--storage.tsdb.path=/prometheus'
- '--storage.tsdb.retention.time=15d'
- '--web.enable-lifecycle'
- '--storage.tsdb.no-lockfile'
- '--web.route-prefix=/'
- '--web.enable-admin-api'
image: 'quay.io/prometheus/prometheus:v2.26.0'
准备开始删除
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={__name__=~".+"}'
查看删除前的大小
[root@linuxea.com prometheus-db]# ls
01FPZGK1S0BZ9FZMSVPYWA1F52 01FQPP5PJTEMT1BARN8ARJTBKB 01FR222TD3ZWYAZTNRJP5SCJ6M 01FR4VB8N4J0D2DY979PTHZQY1 wal
01FQ59ZR5Z9VNNGY3JSXP6F9AQ 01FQWFJAC72YCBKEN19F39PYCM 01FR46R88Q9NN7GR99MMGX13VG 01FR526TSKK8JTN493PTBQH0XC
01FQB3CCFVNSP890V5KE07YD3M 01FQYDBKTHH8KB8K5GT581BKGE 01FR4MFC9JX6NK81G4Q1W0CJNY chunks_head
01FQGWS2M82Q23H0H2TD0JXK1Y 01FR0B54YSBJ0R54B5171P4Z7R 01FR4VB3HHRN0EE1GQCM6G0A77 queries.active
[root@linuxea.com nfs-k8s]# du -sh monitoring-prometheus-k8s-db-prometheus-k8s-0-pvc-ac911baf-f1f1-4bf3-a1f8-af2cb13c5d90/
7.1G monitoring-prometheus-k8s-db-prometheus-k8s-0-pvc-ac911baf-f1f1-4bf3-a1f8-af2cb13c5d90/
找到svc
[root@linuxea.com nfs-k8s]# kubectl -n monitoring get svc |grep prometheus-k8s
prometheus-k8s NodePort 10.68.110.123 <none> 9090:30090/TCP 128d
尝试curl一下
[root@linuxea.com nfs-k8s]# curl 10.68.110.123:9090
<a href="https://www.linuxea.com/graph">Found</a>.
执行
[root@linuxea.com manifests]# curl -X POST -g 'http://10.68.110.123:9090/api/v1/admin/tsdb/delete_series?match[]={__name__=~".+"}'
而后在查看大小
[root@linuxea.com manifests]# kubectl -n monitoring exec -it prometheus-k8s-0 -- /bin/sh
Defaulting container name to prometheus.
Use 'kubectl describe pod/prometheus-k8s-0 -n monitoring' to see all of the containers in this pod.
/prometheus $ du -sh ./
1004.4M ./
/prometheus $ ls
01FR46R88Q9NN7GR99MMGX13VG 01FR4VB8N4J0D2DY979PTHZQY1 chunks_head
01FR4MFC9JX6NK81G4Q1W0CJNY 01FR526TSKK8JTN493PTBQH0XC queries.active
01FR4VB3HHRN0EE1GQCM6G0A77 01FR592J0H8ANAWMFP2VVQ3NX9 wal
/prometheus $ du -sh ./
1006.6M ./
完全删除通过delete_series
发送clean_tombstones
API 调用删除的数据
[root@linuxea.com manifests]# curl -X POST -g 'http://10.68.110.123:9090/api/v1/admin/tsdb/clean_tombstones'
在查看大小
[root@linuxea.com manifests]# kubectl -n monitoring exec -it prometheus-k8s-0 -- /bin/sh
Defaulting container name to prometheus.
Use 'kubectl describe pod/prometheus-k8s-0 -n monitoring' to see all of the containers in this pod.
/prometheus $ du -sh ./
986.6M ./
删除完成后,将--web.enable-admin-api关闭。修改enableAdminAPI: false
字段即可
延申参考
tsdb-admin-apis
Add flag to enable prometheus web admin API and fixes #1215
kube-prometheusspec
prometheus-prometheus.yaml