天天看點

幹貨 | Elasticsearch索引管理利器——Curator深入詳解

1、痛點

Elasticsearch叢集管理中索引的管理非常重要。

資料量少的時候,一個或者幾個索引就能滿足問題。

但是一旦資料量每天幾TB甚至幾十TB的增長時,索引的生命周期管理顯得尤為重要。

痛點1:你是否遇到過磁盤不夠,要删除幾個月前甚至更早時間資料的情況?

如果沒有基于時間建立索引,單一索引借助delete_by_query結合時間戳,會越删磁盤空間越緊張,以至于對自己都産生了懷疑?

痛點2:你是否還在通過複雜的腳本管理索引?

1個增量rollover動态更新腳本,

1個定期delete腳本,

1個定期force_merge腳本,

1個定期shrink腳本,

1個定期快照腳本。

索引多了或者叢集規模大了,腳本的維護是一筆不菲的開銷。

如果以上痛點,你都遇到過了。

那麼,客官别走,本文利器curator會給你答案。

2、curator是什麼?

幹貨 | Elasticsearch索引管理利器——Curator深入詳解

2.1 被Elastic收編的曆史

curator最早被稱為clearESindices.py。 它的唯一功能是删除索引,

而後重命名:logstash_index_cleaner.py。它在logstash存儲庫下作用:過期日志清理。

此後不久,原作者加入Elastic,它成為了Elasticsearch Curator,

Git位址:https://github.com/elastic/curator

2.2 收編後功能強大

curator允許對索引和快照執行許多不同的操作,包括:

  1. 從别名添加或删除索引(或兩者!)
  2. 更改分片路由配置設定更改分片路由配置設定
  3. 關閉索引關閉索引
  4. 建立索引建立索引
  5. 删除索引删除索引
  6. 删除快照删除快照
  7. 打開被關閉的索引打開被關閉的索引
  8. 對索引執行forcemerge段合并操作對索引執行forcemerge段合并操作
  9. reindex索引,包括來自遠端叢集的索引reindex索引,包括來自遠端叢集的索引
  10. 更改索引的每個分片的副本數 更改索引的每個分片的副本數
  11. rollover索引rollover索引
  12. 生成索引的快照(備份)生成索引的快照(備份)
  13. 還原快照還原快照

3、curator 版本

不同于Elasticsearch甚至ELKB的版本統一規範,curator有自己的一套版本規範。

幹貨 | Elasticsearch索引管理利器——Curator深入詳解

簡化記錄如下:

6.XES使用 curator 5;

5.XES可以使用curator5 或者 curator4 ,

具體參考官網:http://t.cn/EGi2nLX

還等什麼,趕緊用起來!

4、Curator使用指南

4.1 curator安裝

curator可以通過多種方式安裝,具體取決于您的需求。

值得注意的是,Curator隻需要安裝在可通路Elasticsearch叢集中機器上就可以運作。 它不需要安裝在群集中的一個節點上。

我的機器是5.X版本,使用如下操作ok。

Step1:安裝:

1pip install elasticsearch-curator           

centos6/7使用者更簡潔:

1yum install elasticsearch-curator           

Step 2:更新至最新版本(非必須,根據自己需要):

1pip install -U elasticsearch-curator           

驗證執行成功方法1:

1curator_cli show_indices           

若成功,會顯示索引資訊。

4.2 curator用法講解

4.2.1 用法1:curator_cli 指令行

用法舉例:curatorcli 關閉全部一天前建立的索引名稱為logs*開頭的索引。

1curator_cli --host 192.168.1.2 --port 9200 close --filter_list '[{"filtertype":"age","source":"creation_date","direction":"older","unit":"days","unit_count":1},{"filtertype":"pattern","kind":"prefix","value":"logs_"}]'           

好處:無需配置檔案,一行指令即可成功。

壞處:不能便捷的适應複雜的操作。

4.2.2 用法2:curator指令行

用法舉例:

1curator [--config CONFIG.YML] [--dry-run] ACTION_FILE.YML           

解釋:

1、CONFIG.YML是配置檔案,用于配置ES叢集資訊。

CONFIG.YML樣例:

1[root@localhost .curator]# cat curator.yml 
 2# Remember, leave a key empty if there is no value.  None will be a string,
 3## not a Python "NoneType"
 4
 5client:
 6  hosts: 192.168.1.1
 7  port: 9200
 8  url_prefix:
 9  use_ssl: False
10  certificate:
11  client_cert:
12  client_key:
13  ssl_no_validate: False
14  http_auth:
15  timeout: 30
16  master_only: False
17
18logging:
19  loglevel: INFO
20  logfile: /home/curator/logs
21  logformat: default
22  blacklist: ['elasticsearch', 'urllib3']           

核心配置:

1)叢集IP;

2)安全認證資訊;

3)日志資訊。

2、ACTION_FILE.YML 執行索引操作的配置資訊

由于支援的操作非常多,建議直接參考官網配置即可:

http://t.cn/EGiLwyk

http://t.cn/EGiL4EF

拿删除曆史索引舉例:

以下指令删除了30天前,以logs_*開頭的索引。

1[root@localhost .curator]# cat action.yml 
 2---
 3# Remember, leave a key empty if there is no value.  None will be a string,
 4# not a Python "NoneType"
 5#
 6# Also remember that all examples have 'disable_action' set to True.  If you
 7# want to use this action as a template, be sure to set this to False after
 8# copying it.
 9actions:
10  1:
11    action: delete_indices
12    description: >-
13      Delete indices older than 20 days (based on index name), for logstash-
14      prefixed indices. Ignore the error if the filter does not result in an
15      actionable list of indices (ignore_empty_list) and exit cleanly.
16    options:
17      ignore_empty_list: True
18      disable_action: False
19    filters:
20    - filtertype: pattern
21      kind: prefix
22      value: logs_
23    - filtertype: age
24      source: name
25      direction: older
26      timestring: '%Y.%m.%d'
27      unit: days
28      unit_count: 30           

如果執行多個任務怎麼辦呢?

注意:actions: 後面的,依次類推:

2:執行操作

3:執行操作

4:執行操作

N:執行操作

好處:1個配置搞定10+處操作,不費勁!

4.3 curator 實踐

經過4.2的配置,實踐執行如下:

curator --config config.yml 指定路徑/action.yml

4.4 周期性執行

借助crontab,每天零點5分執行

1$ crontab -e           

加上如下的指令:

15 0 * * * curator --config config.yml action.yml           

5、小結

  • 切記:

    curator适用于基于時間或者template其他方式建立的索引,

    不适合單一索引存儲N久曆史資料的操作的場景。

  • 思考:

    遇到通用問題,不要重複造輪子,看看官方或者别人是否已經實作了,已經有更好的方案了。

如果有,就“拿來主義”,和自己業務不一緻,可以考慮優化。

比如:類似curator,有很多公司已經進一步加工為可視化工具,極大提高效率。

幹貨 | Elasticsearch索引管理利器——Curator深入詳解