索引生命周期管理ILM
索引生命周期
對於時間序列的索引,生命周期有4個階段:
-
hot: 索引被頻繁寫入和查詢
-
warm: 索引不再寫入,但是仍在查詢
-
cold: 索引很久不被更新,同時很少被查詢。但現在考慮刪除數據還為時過早,仍然有需要這些數據的可能,但是可以接受較慢的查詢響應。
-
delete: 索引不再需要,可以刪除。
索引根據時間參數min_age進入生命周期階段,若未設置,默認是0ms。min_age通常是從創建索引的時間開始計算,如果索引被設置為滾動索引,那么min_age是從索引滾動開始計算。注意,在檢查min_age參數並進入下一個階段前,當前階段的操作必須完成。
各個階段允許的action
優先級設置
這個action等同於設置索引屬性index.priority的值。具有較高優先級的索引將在節點重啟后優先恢復。通常,熱階段的指數應具有最高值,而冷階段的指數應具有最低值。未設置此值的指標的隱含默認優先級為1。索引的優先級。必須為0或更大。也可以設置為null以刪除優先級。
{
"set_priority" : {
"priority": 50
}
}
模擬過程
創建ilm的過渡策略
curl --user elastic:superuser@elastic -XPUT "http://$IP:9200/_ilm/policy/ilm_policy" -H 'Content-Type: application/json' -d '
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_docs": "10"
}
}
},
"warm": {
"min_age": "10s",
"actions": {
"forcemerge" : {
"max_num_segments": 1
},
"allocate": {
"number_of_replicas": 0
}
}
},
"cold": {
"min_age": "30s",
"actions": {
"allocate": {
"include": {
"box_type": "cold"
}
}
}
},
"delete": {
"min_age": "2m",
"actions": {
"delete": {}
}
}
}
}
}'
創建模板,索引使用ilm
curl --user elastic:superuser@elastic -XPUT "http://$IP:9200/_template/ilm_template" -H 'Content-Type: application/json' -d '
{
"index_patterns": ["ilm-namespace-*"],
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1,
"index.routing.allocation.include.box_type": "hot",
"index.lifecycle.name": "ilm_policy",
"index.lifecycle.rollover_alias": "ilm-namespace"
}
}'
- index.lifecycle.name 指明該索引應用的 ILM Policy
- index.lifecycle.rollover_alias 指明在 Rollover 的時候使用的 alias
- index.routing.allocation.include.box_type 指明新建的索引都分配在 hot 節點上
創建用於ilm策略管理的初始索引
curl --user elastic:superuser@elastic -XPUT "http://$IP:9200/ilm-namespace-000001" -H 'Content-Type: application/json' -d '
{
"aliases": {
"ilm-namespace":{
"is_write_index": true
}
}
}'
修改 ILM Polling Interval
ILM Service 會在后台輪詢執行 Policy,默認間隔時間為 10 分鍾,為了更快地看到效果,我們將其修改為10秒。
curl --user elastic:superuser@elastic -XPUT "http://$IP:9200/_cluster/settings" -H 'Content-Type: application/json' -d '
{
"persistent": {
"indices.lifecycle.poll_interval":"10s"
}
}'
更新策略
- 如果沒有index應用這份策略,那么我們可以直接更新該策略。
- 如果有index應用了這份策略,那么當前正在執行的階段不會同步修改,當當前階段結束后,會進入新版本策略的下個階段。
- 如果更換了策略,當前正在執行的階段不會變化,在結束當前階段后,將會由新的策略管理下一個生命周期。
查看策略執行結果
#如果是別名則返回所有索引的策略執行結果,如果是單個索引則返回該索引的執行結果
curl --user elastic:superuser@elastic -XGET "http://$IP:9200/ilm-namespace/_ilm/explain?pretty"