Elasticsearch 索引清理操作

线上部署了ELK+Redis日志分析平台环境, 随着各类日志数据源源不断的收集, 发现过了一段时间之后, ELK查看会原来越慢, 重启elasticsearch服务器节点之前同步时间也会很长, 这是因为长期以来ELK收集的索引没有删除引起的! 以下是ELK批量删除索引的操作记录:

1、 访问head插件(http://10.22.86.129:9200/_plugin/head/) 或者在elasticsearch节点上使用下面命令查看elk的索引(10.22.86.129是elk集群中的任意一个节点)

[root@elk-node01 ~]# curl -XGET 'http://10.22.86.129:9200/_cat/shards'              
删除索引的命令
[root@elk-node01 ~]# curl -XDELETE  http://10.22.86.129:9200/索引名
还可以根据需求,过滤出想要查看的索引,比如查看2018.08.02并且是10.22.86.129的索引
[root@elk-node01 ~]# curl -XGET 'http://10.22.86.129:9200/_cat/shards'  |grep "2018\.08\.02" |grep "10.22.86.129"|awk '{print $1}'
  1. 可以先将要删除的索引查看出来存到临时文件里, 然后进行批量删除 (索引多的话, 删除操作会耗费一点时间)

比如批量删除所有的索引(但不会删除kibana.yml文件中配置的kibana.index索引,就是那些带.的索引) (cat /root/elk-index.tmp|wc -l 可以查看要删除的索引有多少个)

[root@elk-node01 ~]# curl -u elastic:Elk@123 -XGET 'http://10.22.86.129:9200/_cat/shards' | grep -E 'test|uat' | awk '{print $1}'  | uniq > /data/scripts/tmp/index_name.tmp
[root@elk-node01 ~]# for i in $(cat /data/scripts/tmp/index_name.tmp);do curl -u elastic:Elk@123 -XDELETE http://10.22.86.129:9200/$i;done
  1. 为了方便可以在计划任务里面加定时任务删除30天之前的日志索引 (这里线上elk的索引名中带当天的日期, 日期格式为%Y.%m.%d. 具体看自己的索引命名规则)
#!/bin/bash
#Delete:The index 7 days ago
curl -u elastic:Elk@123 -XGET 'http://10.22.85.129:9200/_cat/shards' | grep -E 'test|uat' | awk '{print $1}' | grep `date -d "7 days ago" +%Y.%m.%d` | uniq > /data/scripts/tmp/index_name.tmp

for index_name in `cat /data/scripts/tmp/index_name.tmp` 
do
    curl -u elastic:Elk@123 -XDELETE  http://10.22.85.129:9200/$index_name
    echo -e "[`date  +%F` `date  +%T `" $((10#$(date +%N)/1000000))ms] "${index_name} delete success" >> /data/scripts/del_elasticseatch_index.log
done


[root@elk-node01 ~]# crontab -l
0 3 * * * bash /home/scripts/del_elasticseatch_index.sh
上述脚本执行之后, 访问http://10.0.8.44:9200/_plugin/head/ ,就会发现,ELK索引已被批量删除了.
posted @   Rocky_940120  阅读(2173)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示