最近更新时间:2024-07-18 15:54:42
本文介绍索引生命周期的四个阶段,具体如下:
Hot 索引正在被写入
Warm 索引基本上不再写入,但是会被查询
Cold 索引不再更新,很少被查询,即使查询速度较慢也可以接受
Delete 索引可以安全删除
这个四个阶段是按照顺序执行的,但它们并不是必须的,例如可以只包含Hot,Delete阶段。索引是否进入某一阶段通过参数min_age控制,默认值是0,min_age是相对索引的创建时间或者索引rollover的时间
每个阶段支持的action请参考: https://www.elastic.co/guide/en/elasticsearch/reference/6.8/_actions.html
这个策略包含4个阶段:
索引首先进入hot阶段,当7天或者索引达到50G将会触发roll_over
再过30天,索引进入warm阶段,然后进行三个操作,增加副本数,forcemerge,减少分片数shrink
60天后,进入cold阶段,数据分片会被迁移到cold node
90天后,索引被删除
curl -X PUT "localhost:9200/_ilm/policy/myl_policy?pretty" -H 'Content-Type: application/json' -d'
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_age": "7d",
"max_size": "50G"
}
}
},
"warm": {
"min_age": "30d",
"actions": {
"forcemerge": {
"max_num_segments": 1
},
"shrink": {
"number_of_shards": 1
},
"allocate": {
"number_of_replicas": 2
}
}
},
"cold": {
"min_age": "60d",
"actions": {
"allocate": {
"require": {
"type": "cold"
}
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
}
}
'
本文介绍应用策略到索引的两种方式
curl -X PUT "localhost:9200/_template/my_template?pretty" -H 'Content-Type: application/json' -d'
{
"index_patterns": ["test-*"],
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1,
"index.lifecycle.name": "my_policy",
"index.lifecycle.rollover_alias": "test-alias"
}
}
'
满足这个模板的新建索引会自动应用策略my_policy,如果策略中包含rollover,还需要初始化索引
curl -X PUT "localhost:9200/test-000001?pretty" -H 'Content-Type: application/json' -d'
{
"aliases": {
"test-alias":{
"is_write_index": true
}
}
}
'
curl -X PUT "localhost:9200/test-index?pretty" -H 'Content-Type: application/json' -d'
{
"settings": {
"index.lifecycle.name": "my_policy"
}
}
'
查看集群当前的ilm策略:
curl -XGET localhost:9200/_ilm/policy?pretty
查看索引test-index生命周期管理执行的状态:
curl -XGET localhost:9200/test-index/_ilm/explain?pretty
除了通过Rest API的方式,Kibana也提供了UI界面操作的方式配置ilm,以6.8.4版本为例
左侧导航栏选中Management
选中Index Lifecycle Policies,然后点击Create policy
填写Policy name,然后根据需要开启hot,warm,cold,delete phase,并设置相应的策略,假设我们只设置了delete phase,策略为7天后删除,点击Save as new policy
点击actions,选择add policy to index template
选中要应用的index template,点击Add policy确认,至此ilm设置完成,并将应用到符合模板的新建索引上
纯净模式