Elasticsearch:用Curator辅助Marvel,实现自动删除旧marvel索引

Elasticsearch:用Curator辅助Marvel,实现自动删除旧marvel索引

转载请注明原作链接:

http://blog.csdn.net/hereiskxm/article/details/47423715

Marvel几乎是所有Elasticsearch用户的标配。以前我常用bigdesk来做ES集群的监控,直到项目需要上线时,方感觉到marvel才是想要的:它可以把观测值和重要事件记录下来!当出问题时打开bigdesk已经迟了,因为我们往往想知道过去的两小时发生了什么造成现在的情况。marvel则不同,它时刻保留观测数据。

Marvel保留观测数据的代价是,它默认每天会新建一个index,命名规律像是这样:.marvel-2015-08-09。marvel自建的索引一天可以产生大概500M的数据,而且将会越来越多,占的容量也将越来越大。有没有什么办法能让它自动过期?比如说只保留最近三天的观测数据,其他的都抛弃掉。

果然这样想的不只我一个人,这位deepakas同学提了类似的问题:

http://elasticsearch-users.115913.n3.nabble.com/Marvel-Indices-taking-lot-of-space-Can-we-specify-automatic-delete-of-marvel-indice-td4055729.html

回答里Ivan Brusic和Boaz Leskes提到的curator解决了题主的问题,我们不妨参照这个做一个解决方案。

curator是什么?它是一个命令,可以帮助你管理你在Elasticsearch中的索引,帮你删除,关闭(close),打开(open)它们。当然这是比较片面的说法,更完整的命令说明见:

https://www.elastic.co/guide/en/elasticsearch/client/curator/current/index.html
github地址:https://github.com/elastic/curator

  1. 安装一个curator
    从github上下载一个curator-master:https://codeload.github.com/elastic/curator/zip/master
https://github.com/elastic/curator/archive/3.3.zip 由于软件变更,此软件是基于3.3版本 放置在服务器上某个目录下,而后解压并安装:

[plain] view plain copy print?
cd %curator-home%
python setup.py install
如果出现
[plain] view plain copy print?
Finished processing dependencies for elasticsearch-curator==3.3.0.dev4

字样说明安装成功了,你拥有了一个curator命令。

注:官方推荐使用pip安装,考虑到我本地用pip并不方便,使用以上安装形式。已安装pip命令的同学可以选择官方的安装方法。

  1. 删除过期的marvel索引

根据curator的文档,我写了以下语句来执行删除操作:

[plain] view plain copy print?
curator delete indices --index .marvel- --older-than 3 --time-unit days --timestring %Y.%m.%d --dry-run
索引名是.marvel-开头的,时间格式是%Y.%m.%d,删除三天前的索引。并使用调试模式(–dry-run),只模拟删除,并不真的做操作,这样方便我们调试。
执行的结果:

[plain] view plain copy print?
2015-08-11 12:09:33,937 INFO Job starting: delete indices
2015-08-11 12:09:33,948 INFO Pruning Kibana-related indices to prevent accidental deletion.
2015-08-11 12:09:33,948 WARNING .marvel- not found!
2015-08-11 12:09:33,948 INFO DRY RUN MODE. No changes will be made.
2015-08-11 12:09:33,959 INFO DRY RUN: delete: .marvel-2015.08.10
可以看到它模拟删除了.marvel-2015.08.10索引。

如果换成正式的语句,去掉–dry-run即可。若ES是需要验证访问的,可以使用http_auth选项:
[plain] view plain copy print?
curator --host 127.0.0.1 --http_auth {username}:{password} delete indices --index .marvel- --older-than 1 --time-unit days --timestring %Y.%m.%d
执行的结果:

[plain] view plain copy print?
2015-08-11 12:48:16,316 INFO Job starting: delete indices
2015-08-11 12:48:16,327 INFO Pruning Kibana-related indices to prevent accidental deletion.
2015-08-11 12:48:16,327 WARNING .marvel- not found!
2015-08-11 12:48:16,327 INFO Deleting indices as a batch operation:
2015-08-11 12:48:16,327 INFO —deleting index .marvel-2015.08.10
2015-08-11 12:48:16,670 INFO Job completed successfully.

  1. 设成定时任务

现在有了删除语句,我们只要把它设置成每天定时跑,就可以实现自动清理过期marvel数据了。

首先新建一个shell脚本:

[plain] view plain copy print?
#!/bin/sh

#always keep nearly two days marvel indics in cluster.

#author:kexm
#date:2015-08-11

curator --host 10.64.2.51 --http_auth username:password delete indices --index .marvel- --older-than 2 --time-unit days --timestring %Y.%m.%d

echo “delete successed!”
然后将其设置为每日定时执行:
[plain] view plain copy print?
crontab -e
增加一行:
[plain] view plain copy print?
0 2 * * * /bin/sh {your-path}/del-Marvel-Index.sh>>{your-path}/del-Marvel-Index.log 2>&1

每天凌晨两点执行curator语句,并将其执行情况输出到日志。
验证定时任务是否新建:

[plain] view plain copy print?
crontab -l
看到自己的定时任务则可。

假如shell脚本运行的时候报错 curator:conmand not found,则在定时任务(crontab -e)的文件头部增加:

[plain] view plain copy print?
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
其中包含你的curator命令所在位置即可。

如果其他的解决方案欢迎交流~

也希望marvel可以在未来的版本中增加对数据保留时间的设置,避免用户们使用外部指令曲线救国的方式来解决问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值