ELK批量删除索引及集群相关操作记录-运维笔记

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

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

[root@elk-node01 ~]# curl -XGET 'http://10.0.8.44:9200/_cat/shards'               

删除索引的命令
[root@elk-node01 ~]# curl -XDELETE  http://10.0.8.44:9200/索引

还可以根据需求,过滤出想要查看的索引,比如查看2018.08.02并且是10.0.52.22的索引
[root@elk-node01 ~]# curl -XGET 'http://10.0.8.44:9200/_cat/shards'  |grep "2018\.08\.02" |grep "10.0.52.22"|awk '{$1}'

2) 可以先将要删除的索引查看出来存到临时文件里, 然后进行批量删除

比如批量删除所有的索引
[root@elk-node01 ~]# curl -XGET 'http://10.0.8.44:9200/_cat/shards'|awk '{print $1}'|uniq > /root/elk-index.tmp
[root@elk-node01 ~]# for i in $(cat /root/elk-index.tmp);do curl -XDELETE  http://10.0.8.44:9200/$i;done

3) 为了方便可以在计划任务里面加定时任务删除30天之前的日志索引  (这里线上elk的索引名中带当天的日期, 日期格式为%Y.%m.%d.  具体看自己的索引命名规则)

[root@elk-node01 ~]# vim /home/scripts/del_elasticseatch_index.sh
#!/bin/bash
#The index 30 days ago
curl -XGET ‘http://10.0.8.44:9200/_cat/shards‘ |awk ‘{print $1}‘ |grep `date -d "30 days ago" +%Y.%m.%d` |uniq > /tmp/index_name.tmp

for index_name in `cat /tmp/index_name.tmp`  
do
    curl -XDELETE  http://10.0.8.44:9200/$index_name
    echo "${index_name} delete success" >> /home/scripts/del_elasticseatch_index.log
done

[root@elk-node01 ~]# crontab -l
0 3 * * * bash /home/scripts/del_elasticseatch_index.sh

  

猜你喜欢

转载自www.cnblogs.com/kevingrace/p/9994178.html