天天看點

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

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

1、痛點

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

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

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

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

如果沒有基于時間建立索引,單一索引借助delete_by_query結合時間戳,會越删磁盤空間越緊張,

以至于對自己都産生了懷疑?

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

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

1個定期delete腳本,

1個定期force_merge腳本,

1個定期shrink腳本,

1個定期快照腳本。

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

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

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

2、curator是什麼?

2.1 被Elastic收編的曆史

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

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

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

Git位址:

https://github.com/elastic/curator

2.2 收編後功能強大

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

從别名添加或删除索引(或兩者!)

更改分片路由配置設定更改分片路由配置設定

關閉索引關閉索引

建立索引建立索引

删除索引删除索引

删除快照删除快照

打開被關閉的索引打開被關閉的索引

對索引執行forcemerge段合并操作對索引執行forcemerge段合并操作

reindex索引,包括來自遠端叢集的索引reindex索引,包括來自遠端叢集的索引

更改索引的每個分片的副本數 更改索引的每個分片的副本數

rollover索引rollover索引

生成索引的快照(備份)生成索引的快照(備份)

還原快照還原快照

3、curator 版本

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

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

簡化記錄如下:

6.XES使用 curator 5;

5.XES可以使用curator5 或者 curator4 ,具體參考官網:

https://www.elastic.co/guide/en/elasticsearch/client/curator/current/version-compatibility.html

還等什麼,趕緊用起來!

4、Curator使用指南

4.1 curator安裝

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

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

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

Step1:安裝:

pip install elasticsearch-curator

1

centos6/7使用者更簡潔:

yum install elasticsearch-curator

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

pip install -U elasticsearch-curator

驗證執行成功方法1:

curator_cli show_indices

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

4.2 curator用法講解

4.2.1 用法1:curator_cli 指令行

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

curator_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指令行

用法舉例:

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

解釋:

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

CONFIG.YML樣例:

[root@localhost .curator]# cat curator.yml

# Remember, leave a key empty if there is no value.  None will be a string,

## not a Python "NoneType"

client:

 hosts: 192.168.1.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: /home/curator/logs

 logformat: default

 blacklist: ['elasticsearch', 'urllib3']

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

核心配置:

1)叢集IP;

2)安全認證資訊;

3)日志資訊。

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

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

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

拿删除曆史索引舉例:

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

[root@localhost .curator]# cat action.yml

---

# not a Python "NoneType"

#

# Also remember that all examples have 'disable_action' set to True.  If you

# want to use this action as a template, be sure to set this to False after

# copying it.

actions:

 1:

   action: delete_indices

   description: >-

     Delete indices older than 20 days (based on index name), for logstash-

     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: logs_

   - filtertype: age

     source: name

     direction: older

     timestring: '%Y.%m.%d'

     unit: days

     unit_count: 30

23

24

25

26

27

28

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

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

2:執行操作

3:執行操作

4:執行操作

N:執行操作

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

4.3 curator 實踐

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

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

4.4 周期性執行

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

$ crontab -e

加上如下的指令:

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

5、小結

切記:

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

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

思考:

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

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

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

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