Carpe Diem

備忘録

CuratorでElasticsearchのインデックスを削除

概要

Kibanaを使う上でデータ容量が問題になってきます。
可視化やログ調査に必要な分のみ残し、古いインデックスを定期的に削除する必要があります。
Curatorはそのためのツールです。

環境

  • Elasticsearch 2.2.0
  • Curator 3.4.1

インストール

pipからのインストールを推奨しているため、まずはpipをインストールします。

$ wget https://bootstrap.pypa.io/get-pip.py

$ sudo python get-pip.py

curatorをインストールします。

$ sudo pip install elasticsearch-curator

インデックスの削除

$ curator --host example.com delete indices --prefix logstash --older-than 30 --time-unit days --timestring %Y.%m.%d

主に使うオプションは以下です。

オプション 説明
host ホスト名 example.com
prefix 削除したいインデックスのprefix logstash
time-unit 時間の単位 days
older-than 指定した日数分残す。例なら30日分残す 30
timestring インデックスの時刻のフォーマット %Y.%m.%d

動作検証

f:id:quoll00:20160219140658p:plain

上記のようなログがあったとします。
現在の日付は2016/02/19です。

ここで以下を実行してみます。

$ curator --host localhost delete indices --prefix logstash --older-than 30 --time-unit days --timestring %Y.%m.%d
2016-02-19 05:01:47,021 INFO      Job starting: delete indices
2016-02-19 05:01:47,036 INFO      Pruning Kibana-related indices to prevent accidental deletion.
2016-02-19 05:01:47,037 INFO      Action delete will be performed on the following indices: [u'logstash-2015.10.01', u'logstash-2015.10.03', u'logstash-2015.10.04-1']
2016-02-19 05:01:47,041 INFO      Deleting indices as a batch operation:
2016-02-19 05:01:47,042 INFO      ---deleting index logstash-2015.10.01
2016-02-19 05:01:47,042 INFO      ---deleting index logstash-2015.10.03
2016-02-19 05:01:47,042 INFO      ---deleting index logstash-2015.10.04-1
2016-02-19 05:01:47,067 INFO      Job completed successfully.

結果として f:id:quoll00:20160219140820p:plain

logstash-2015.10.01
logstash-2015.10.03
logstash-2015.10.04-1

のログが削除されました。

定期バッチに組み込む

Curator自身は単なるコマンドツールで、定期的に実行するdaemonではないためCronJenkinsで定期的に実行してください。

Bloom filterについて

https://www.elastic.co/guide/en/elasticsearch/client/curator/current/bloom.html

Beginning with version 1.4, Elasticsearch no longer enables bloom filters for search. This command is unnecessary if your Elasticsearch version is 1.4 or higher.

とあるように、1.4以降はデフォルトで使っていないのでcurator bloomは必要ありません。

ソース