簡介
curator 是一個官方的,可以管理elasticsearch索引的工具,可以實現創建,刪除,段合並等等操作。詳見官方文檔
功能
curator允許對索引和快照執行許多不同的操作,包括:
- 從別名添加或刪除索引(或兩者!)
- 更改分片路由分配
- 關閉索引
- 創建索引
- 刪除索引
- 刪除快照
- 打開被關閉的索引
- 對索引執行forcemerge段合並操作
- reindex索引,包括來自遠程集群的索引
- 更改索引的每個分片的副本數
- rollover索引
- 生成索引的快照(備份)
- 還原快照
版本
安裝
有多種安裝方法,本人采用yun安裝,官網下載安裝包:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/yum-repository.html
執行以下命令進行安裝:
rpm -ivh elasticsearch-curator-5.8.1-1.x86_64.rpm
默認安裝路徑為:/opt/elasticsearch-curator,且不能移動到其它位置,命令會識別不了。
執行以下命令驗證是否安裝成功:
curator --help
創建配置文件
curator運行需兩個配置文件config.yml(用於連接ES集群配置)、action.yml(用於配置要執行的操作),文件名稱可以隨意命名。
config.yml樣例如下: 配置說明參考官網說明:config.yml
# Rmember, leave a key empty if there is no value. None will be a string, # not a Python "NoneType" client: hosts: ["10.0.101.100", "10.0.101.101", "10.0.101.102"] # es集群地址 port: 9200 # es端口 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: /opt/elasticsearch-curator/logs/run.log # 日志路徑 logformat: default blacklist: ['elasticsearch', 'urllib3']
action.yml樣例如下: 配置說明參考官網說明:action.yml
# Remember, leave a key empty if there is no value. None will be a string, # 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 metric indices older than 3 days (based on index name), for zou_data-2018-05-01 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 filters: - filtertype: pattern kind: prefix value: business-logs- # 這里是指匹配前綴為 “order_” 的索引,還可以支持正則匹配等,詳見官方文檔 - filtertype: age # 這里匹配時間 source: name # 這里根據索引name來匹配,還可以根據字段等,詳見官方文檔 direction: older timestring: '%Y-%m-%d' # 用於匹配和提取索引或快照名稱中的時間戳 unit: days # 這里定義的是days,還有weeks,months等,總時間為unit * unit_count unit_count: 3
以上命令刪除了3天前,以business-logs-*開頭的索引。
執行多個任務
注意:actions: 后面的,依次類推
:
2:執行操作 3:執行操作 4:執行操作 N:執行操作
運行curator
單次運行:
curator --config config.yml action.yml
實際生成環境中我們添加一個linux的cron定時任務:
crontab -e
加上如下命令(每天0時運行一次):
0 0 */1 * * curator --config /opt/elasticsearch-curator/config.yml /opt/elasticsearch-curator/action.yml
運行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_"}]'
總結
curator適用於基於時間或者template其他方式創建的索引
,不適合單一索引存儲N久歷史數據的操作的場景。
例如以下場景:
elasticsearch要想實現只保留固定時間的數據,這里以7天為例,要想每個索引的數據都只保留最近7天的數據,大於7天的則刪除,有兩種方法:
1. 看你的索引是怎么樣的,如果你的索引名稱中有時間,比如logstash-2019-01-02 這樣,就是每天都會生成一個新的索引,這樣的話可以使用官方的Curator 工具
2. 如果你的索引中不帶時間,比如,如果是根據應用或者服務名來命名的,那么注意,Curator是無法實現刪除索中的某一段數據的!! 這里需要特別注意,網上很多說可以實現的,那是因為他們的索引如上面1 所說,是根據時間日期來生成的。
但實際上,很多索引都不是這樣的,按正常的思維,更容易用服務名或應用名作為索引,以此來區分日志所屬應用,方便日志的分析對應指定的應用。這種時候需要使用elasticsearch的api:delete_by_query來進行刪除指定數據。
具體實現請參考:https://blog.csdn.net/weixin_41004350/article/details/85620572
https://juejin.im/post/58e5de06ac502e006c254145
參考: