1,爲什麼要對elasticsearch進行生命週期管理?
ES索引存活數量過多,會給ES集羣帶來較大壓力,不僅嚴重影響數據錄入和數據查詢效率,而且導致磁盤、CPU佔用比過高,加大節點“駕崩”的風險。對ES進行索引生命週期管理意義重大,不僅能提高服務器性能,降低內存和磁盤使用率,而且能夠優化數據結構,提升讀寫、查詢效率,避免數據丟失情況出現。
2,如何對elasticsearch進行生命週期管理?
針對索引,根據時間、空間、類型、優先級進行不同種類的管理策略。推薦以時間序列爲導向對數據進行應用策略操作。
對於時間序列索引,索引生命週期中有五個階段:
熱數據–索引正在積極更新和查詢。
暖數據–索引不再更新,但仍需要查詢。
冷數據–索引不再被更新,且很少被查詢。數據仍然需要搜索,但查詢速度較慢也沒關係。
數據歸檔–索引不在被更新和查詢,但需要存儲以作備份。
數據刪除–不再需要索引,可以安全刪除。
以時間序列爲導向,控制索引的老化時間,將3天以內的數據定義爲熱數據,3天以上7天以內的數據定義爲暖數據,7天以上30天以內的數據定義爲冷數據,30天以上數據進行歸檔,無需歸檔的數據進行刪除。經過優化處理,可極大提高ES的可用性,避免數據丟失。
3,用什麼對elasticsearch進行生命週期管理?
進行ES索引生命週期管理可藉助工具。ES 7.0版本提供ILM策略進行elasticsearch-stack的組件生命週期管理功能;ES 6.x及以下版本使用elasticsearch-curator進行索引策略動作。使用ILM策略優先級比使用curator優先級更高,爲了避免衝突,使用ILM進行索引管理後儘量避免使用curator再次進行管理。在此推薦使用elasticsearch-curator,本文提供Centos7及Centos6兩個安裝包的獲取方法。
系統名 | 下載地址 |
---|---|
Centos7 | https://packages.elastic.co/curator/5/centos/7/Packages/elasticsearch-curator-5.7.6-1.x86_64.rpm |
Centos6 | https://packages.elastic.co/curator/5/centos/6/Packages/elasticsearch-curator-5.7.6-1.x86_64.rpm |
elasticsearch-curator使用時需要注意版本問題,如下圖所示,目前Curator最新版本是5.7.6,支持ES 5.x及以上版本。
使用elasticsearch-curator進行時間序列爲導向的索引生命週期管理,主要分爲三個步驟:
1,Curator部署
Curator只需要安裝在可訪問Elasticsearch集羣中機器上就可以運行, 它不需要安裝在羣集中的一個節點上。
用wget獲取rpm包後直接安裝:
rpm -ivh elasticsearch-curator*.rpm
軟件安裝完畢後,得到安裝目錄:/opt/elasticsearch-curator
在安裝目錄配置文件夾下,新建一個主配置文件curator.yml(粘貼下述代碼,注意修改es集羣hosts與port):
client:
hosts:
- rjjd-node01.xx.com
- rjjd-node02.xx.com
- rjjd-node03.xx.com
port: 9200
url_prefix:
use_ssl: False
timeout: 60
master_only:
logging:
loglevel: INFO
logfile: /var/log/curator.log
logformat: json
blacklist: ['elasticsearch', 'urllib3']
2,Curator策略編寫
單個策略有以下四部分組成:
1.動作名稱,例如close、open、shrink等。
2.動作參數,例如忽略空索引、發生錯誤後繼續執行後面的動作等。
3.過濾器,例如按照時間、空間、分片等維度類型來過濾。
4.過濾參數,例如設置時間、空間、索引狀態模式進行過濾器的增強參數。
示例(關閉索引策略):
action: close #動作類型
description:‘默認關閉30天前的大於100g的通達信索引’ #動作描述
options:
ignore_empty_list: True #忽略空索引
continue_if_exception: True #發生錯誤後繼續執行
disable_action: False #啓用動作
filters:
- filtertype: pattern #過濾類型1
kind: prefix #類型模式使用前綴
value: ‘tdx_’ #前綴名稱
exclude: #是否排除
- filtertype: age #過濾類型2,按照時間
source: creation_date #時間選擇參數,按照索引生成時間
direction: older #時間模式
unit: days #時間單位
unit_count: 30 #時間具體值
- filtertype: space #過濾類型3
disk_space: 100 #類型大小
threshold_behavior: less_than #判斷條件,大於或者小於
reverse: True #是否反向判斷
3,Curator啓動
創建yml文件(以上文關閉索引策略爲例):
[root@localhost .curator]# cat close.yml
actions:
action: close
description:‘默認關閉30天前的大於100g的通達信索引’
options:
ignore_empty_list: True
continue_if_exception: True
disable_action: False
filters:
- filtertype: pattern
kind: prefix
value: ‘tdx_’
exclude:
- filtertype: age
source: creation_date
direction: older
unit: days
unit_count: 30
- filtertype: space
disk_space: 100
threshold_behavior: less_than
reverse: True
命令啓動:
curator –config curator.yml config/close.yml &
配置定時任務:
Crontab -e
#加上如下的命令,表示每日22點定時執行close.yml:
#0 22 * * * /usr/bin/curator –config /opt/elasticsearch-curator/curator.yml /opt/elasticsearch/config/close.yml
定時任務解析參數解析:
{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}
如上文定時任務:0代表minute,區間0-59;22代表hour,區間0-23;三個“”的佔位依次代表day-of-month、month、day-of-week,day-of-month區間1-31,month區間1-12,day-of-week區間0-7,週日可以是0或7,“”意味着全選;full-path-to-shell-script指向配置文件路徑。
如有未盡事宜,請參考Curator官方鏈接:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/actions.html