データ分析をする際にはログの保存期間は無期限であるとかいう話をよく聞きますが、現実はコストに跳ね返ってくるのでそう簡単にはいきません。 そんなわけでelasticsearchに蓄積されたデータをローテートするお話。
環境は以下の通り。
- CentOS7.3
- elasticsearch-5.4.3-1
- kibana-5.4.3-1
- rsyslog-elasticsearch-7.4.7-16
前回の記事の設定により、rsyslogからsyslog-YYYY-MM-DDのインデックスでデータが蓄積されてます。
curatorをインストールする
今回はelastic社が提供している運用支援ツールのcuratorを使います。インデックスに対する操作を支援してくれるツールで、バックアップ等にも活用できそうです。
まずはリポジトリを作成してインストールします。公式サイトではpipからのインストールが推奨されてますが、今回はyumから入れます。
# vi /etc/yum.repos.d/curator.repo [curator-5] name=CentOS/RHEL 7 repository for Elasticsearch Curator 5.x packages baseurl=http://packages.elastic.co/curator/5/centos/7 gpgcheck=1 gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch enabled=1 # yum install elasticsearch-curator 読み込んだプラグイン:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: ftp.iij.ad.jp * extras: ftp.iij.ad.jp * updates: ftp.iij.ad.jp 依存性の解決をしています --> トランザクションの確認を実行しています。 ---> パッケージ elasticsearch-curator.x86_64 0:5.1.1-1 を インストール --> 依存性解決を終了しました。 依存性を解決しました ======================================================================================= Package アーキテクチャー バージョン リポジトリー 容量 ======================================================================================= インストール中: elasticsearch-curator x86_64 5.1.1-1 curator-5 10 M トランザクションの要約 ======================================================================================= インストール 1 パッケージ 総ダウンロード容量: 10 M インストール容量: 26 M Is this ok [y/d/N]: y Downloading packages: 警告: /var/cache/yum/x86_64/7/curator-5/packages/elasticsearch-curator-5.1.1-1.x86_64.rpm: ヘッダー V4 RSA/SHA256 Signature、鍵 ID d88e42b4: NOKEY elasticsearch-curator-5.1.1-1.x86_64.rpm の公開鍵がインストールされていません elasticsearch-curator-5.1.1-1.x86_64.rpm | 10 MB 00:00:31 http://packages.elastic.co/GPG-KEY-elasticsearch から鍵を取得中です。 Importing GPG key 0xD88E42B4: Userid : "Elasticsearch (Elasticsearch Signing Key) <dev_ops@elasticsearch.org>" Fingerprint: 4609 5acc 8548 582c 1a26 99a9 d27d 666c d88e 42b4 From : http://packages.elastic.co/GPG-KEY-elasticsearch 上記の処理を行います。よろしいでしょうか? [y/N]y Running transaction check Running transaction test Transaction test succeeded Running transaction インストール中 : elasticsearch-curator-5.1.1-1.x86_64 1/1 検証中 : elasticsearch-curator-5.1.1-1.x86_64 1/1 インストール: elasticsearch-curator.x86_64 0:5.1.1-1 完了しました!
curatorのconfigファイルを作成する
curatorはデフォルトではユーザのホームディレクトリ配下のファイル(~/.curator/curator.yml)を読み込みます。 今回は意図的に/etc/配下に以下のフォルダ構成で作成しています。
/etc/ └ curator/ # curator用の設定フォルダ ├ curator.yml # configファイル └ actionfiles/ # actionファイル格納フォルダ └ delete_indices.yml # actionファイル
configファイルの作成
今回はローカルホスト上のインデックスを操作するだけなので、公式サイトのサンプルの通りで問題ありません。
# vi /etc/curator/curator.yml --- # Remember, leave a key empty if there is no value. None will be a string, # not a Python "NoneType" client: hosts: - 127.0.0.1 port: 9200 url_prefix: use_ssl: False certificate: client_cert: client_key: ssl_no_validate: False http_auth: timeout: 30 master_only: False logging: loglevel: INFO logfile: logformat: default blacklist: ['elasticsearch', 'urllib3']
actionファイルの作成
こちらも基本的には公式サイトのサンプルの通りで問題ありませんが、以下の編集が必要です。
- disable_actionをFalseに変更する(Trueだと文字通り実行されません)
- filtertype: patternにあるvalueの値を適切な値に変更する
- filtertype: sourceにあるtimestringを適切な値に変更する
- timestring: sourceにあるunit_countの値を適切な値に変更する
- (任意)コメントを修正する
今回は1週間以上前のインデックスを削除するよう、以下の内容としました。
# vi /etc/curator/actionfiles/delete_indices.yml --- actions: 1: action: delete_indices description: >- Delete indices older than 7 days (based on index name), for syslog- prefixed indices. Ignore the error if the filter does not result in an actionable list of indices (ignore_empty_list) and exit cleanly. options: ignore_empty_list: True disable_action: False filters: - filtertype: pattern kind: prefix value: syslog- - filtertype: age source: name direction: older timestring: '%Y-%m-%d' unit: days unit_count: 7
curatorの動作確認を行う
インデックスの一覧を確認する
インデックスの一覧は以下のコマンドで確認可能です。 コマンド結果から、7月10日から18日までの9日分のインデックスがあることがわかります。
# curl localhost:9200/_cat/indices |sort yellow open .kibana lCPWOQLCRLi1LfLEWlUEUA 1 1 8 0 46.6kb 46.6kb yellow open syslog-2017-07-10 1eC8AkrdTla0bukomGdSKw 5 1 3621 0 1mb 1mb yellow open syslog-2017-07-11 OwyX4hIORAOK2SCwYyxcfQ 5 1 4827 0 1.3mb 1.3mb yellow open syslog-2017-07-12 GZxQ5ovIS7SiXOb_tdigRQ 5 1 6753 0 1.6mb 1.6mb yellow open syslog-2017-07-13 F3m03dfaRHK4kk4he1JK8w 5 1 6748 0 1.5mb 1.5mb yellow open syslog-2017-07-14 KghQYLy8QFyvtxqznDOmPQ 5 1 3306 0 895.8kb 895.8kb yellow open syslog-2017-07-15 cc7-q_xuQWqPNhEhr3YKYw 5 1 3627 0 996.5kb 996.5kb yellow open syslog-2017-07-16 l3apSUIqQm6oqjoy4k8NDg 5 1 5452 0 1.4mb 1.4mb yellow open syslog-2017-07-17 4Od66p2LR4aYihsUlyhatQ 5 1 3933 0 1mb 1mb yellow open syslog-2017-07-18 yO9lZsTySWuRSrAFg1X-Tw 5 1 5596 0 1.6mb 1.6mb
curatorをドライランする
curatorには実際に実行せず動作を確認するドライランができるので、動作を確認します。 ドライランではインデックスは削除されていないことがわかります。
# curator --dry-run --config /etc/curator/curator.yml /etc/curator/actionfiles/delete_indices.yml 2017-07-18 22:55:11,915 INFO Preparing Action ID: 1, "delete_indices" 2017-07-18 22:55:11,951 INFO Trying Action ID: 1, "delete_indices": Delete indices older than 7 days (based on index name), for syslog- prefixed indices. Ignore the error if the filter does not result in an actionable list of indices (ignore_empty_list) and exit cleanly. 2017-07-18 22:55:12,064 INFO DRY-RUN MODE. No changes will be made. 2017-07-18 22:55:12,065 INFO (CLOSED) indices may be shown that may not be acted on by action "delete_indices". 2017-07-18 22:55:12,065 INFO DRY-RUN: delete_indices: syslog-2017-07-10 with arguments: {} 2017-07-18 22:55:12,065 INFO DRY-RUN: delete_indices: syslog-2017-07-11 with arguments: {} 2017-07-18 22:55:12,065 INFO Action ID: 1, "delete_indices" completed. 2017-07-18 22:55:12,065 INFO Job completed. # curl localhost:9200/_cat/indices |sort yellow open .kibana lCPWOQLCRLi1LfLEWlUEUA 1 1 8 0 46.6kb 46.6kb yellow open syslog-2017-07-10 1eC8AkrdTla0bukomGdSKw 5 1 3621 0 1mb 1mb yellow open syslog-2017-07-11 OwyX4hIORAOK2SCwYyxcfQ 5 1 4827 0 1.3mb 1.3mb yellow open syslog-2017-07-12 GZxQ5ovIS7SiXOb_tdigRQ 5 1 6753 0 1.6mb 1.6mb yellow open syslog-2017-07-13 F3m03dfaRHK4kk4he1JK8w 5 1 6748 0 1.5mb 1.5mb yellow open syslog-2017-07-14 KghQYLy8QFyvtxqznDOmPQ 5 1 3306 0 895.8kb 895.8kb yellow open syslog-2017-07-15 cc7-q_xuQWqPNhEhr3YKYw 5 1 3627 0 996.5kb 996.5kb yellow open syslog-2017-07-16 l3apSUIqQm6oqjoy4k8NDg 5 1 5452 0 1.4mb 1.4mb yellow open syslog-2017-07-17 4Od66p2LR4aYihsUlyhatQ 5 1 3933 0 1mb 1mb yellow open syslog-2017-07-18 yO9lZsTySWuRSrAFg1X-Tw 5 1 5597 0 1.3mb 1.3mb
実行し、結果を確認する
ドライランオプションを外して、削除されていることを確認します。
# curator --config /etc/curator/curator.yml /etc/curator/actionfiles/delete_indices.yml 2017-07-18 22:59:00,082 INFO Preparing Action ID: 1, "delete_indices" 2017-07-18 22:59:00,096 INFO Trying Action ID: 1, "delete_indices": Delete indices older than 7 days (based on index name), for syslog- prefixed indices. Ignore the error if the filter does not result in an actionable list of indices (ignore_empty_list) and exit cleanly. 2017-07-18 22:59:00,308 INFO Deleting selected indices: ['syslog-2017-07-11', 'syslog-2017-07-10'] 2017-07-18 22:59:00,309 INFO ---deleting index syslog-2017-07-11 2017-07-18 22:59:00,309 INFO ---deleting index syslog-2017-07-10 2017-07-18 22:59:00,832 INFO Action ID: 1, "delete_indices" completed. 2017-07-18 22:59:00,832 INFO Job completed. # curl localhost:9200/_cat/indices |sort yellow open .kibana lCPWOQLCRLi1LfLEWlUEUA 1 1 8 0 46.6kb 46.6kb yellow open syslog-2017-07-12 GZxQ5ovIS7SiXOb_tdigRQ 5 1 6753 0 1.6mb 1.6mb yellow open syslog-2017-07-13 F3m03dfaRHK4kk4he1JK8w 5 1 6748 0 1.5mb 1.5mb yellow open syslog-2017-07-14 KghQYLy8QFyvtxqznDOmPQ 5 1 3306 0 895.8kb 895.8kb yellow open syslog-2017-07-15 cc7-q_xuQWqPNhEhr3YKYw 5 1 3627 0 996.5kb 996.5kb yellow open syslog-2017-07-16 l3apSUIqQm6oqjoy4k8NDg 5 1 5452 0 1.4mb 1.4mb yellow open syslog-2017-07-17 4Od66p2LR4aYihsUlyhatQ 5 1 3933 0 1mb 1mb yellow open syslog-2017-07-18 yO9lZsTySWuRSrAFg1X-Tw 5 1 5597 0 1.3mb 1.3mb
cronで毎日実行するようにする
先ほどのcronを毎日実行するようcron.dailyに追加します。 多少の事故崩れとかは気にしないので、anacronをそのまま使います。
# vi /etc/cron.daily/curator_delete_indices #!/bin/sh /usr/bin/curator --config /etc/curator/curator.yml /etc/curator/actionfiles/delete_indices.yml >> /var/log/curator.log # chmod +x /etc/cron.daily/curator_delete_indices