目录
前言
参考7.10的官方文档,7版本以下可能不适用。
Index lifecycle management索引生命周期管理缩写为ILM。
很多文章看了以后还是没讲清楚,主要是主要逻辑没有体系化,在此抛砖引玉。
1.基础概念
1.1重点
理解下面几个概念,你就完全搞懂了ILM的运作,这也是这篇文章最重要的部分!
1、ILM由action(操作)、phase(阶段)和minimum age(年龄限制)三要素组成。
2、ILM管理对象是索引,不是别名或者其他的东西,索引在这里也有年龄(age)这个属性。
3、当一个索引,完成了一个阶段内的所有操作,并满足下个阶段的年龄限制时,ILM会将它推到下一个阶段,开始执行下个阶段的操作。
原文:
LM moves indices through the lifecycle according to their age. To control the timing of these transitions, you set a minimum age for each phase. For an index to move to the next phase, all actions in the current phase must be complete and the index must be older than the minimum age of the next phase.(必须完成当前阶段的所有操作,且年龄大于下个阶段的最小年龄限制)
The minimum age defaults to zero, which causes ILM to move indices to the next phase as soon as all actions in the current phase complete.(如果年龄限制为0,则完成所有操作后立即进入下个阶段)
If an index has unallocated shards and the cluster health status is yellow, the index can still transition to the next phase according to its index lifecycle management policy. However, because Elasticsearch can only perform certain clean up tasks on a green cluster, there might be unexpected side effects.
To avoid increased disk usage and reliability issues, address any cluster health problems in a timely fashion.
所以我们可以看到一个阶段的典型定义如下
{
"phases": {
#阶段
"hot": {
#年龄限制
"min_age":"0ms",
#操作目录
"actions": {
#操作1
"rollover": {
#操作1的option
"max_docs": "5"
}
}
}
}
}
1.2阶段的概念
阶段按顺序分为hot,warm,cold,delete。
索引经历4个阶段,最后消失,每个索引某一时刻只能处于一种阶段。
HOT:索引被激活,更新,和查询。原文:The index is actively being updated and queried
WARM:索引不能被更新(不支持增删改),可以查询。原文:The index is no longer being updated but is still being queried
COLD:索引不能被更新,很少被查询。查询速度变慢。原文:The index is no longer being updated and is seldom queried. The information still needs to be searchable, but it’s okay if those queries are slower.
DELETE:索引不再被需要了~。原文:The index is no longer needed and can safely be removed.
1.3操作的概念
action目录下,目前有12种操作,每个阶段只能执行其中的部分操作,比如在HOT阶段不能执行delete,对应关系如下表。
| HOT | WARM | COLD | DELETE | |
| Allocate | √ | √ | ||
| Delete | √ | |||
| Force merge | √ | √ | ||
| Freeze | √ | |||
| Migrate | √ | √ | ||
| Read only | √ | |||
| Rollover | √ | |||
| Searchable snapshot | √ | √ | √ | √ |
| Set priority | √ | √ | √ | |
| Shrink | √ | |||
| Unfollow | √ | √ | √ | |
| Wait for snapshot | √ |
1.3.1.Allocate分配
更新索引设置以更改代理分片的节点,并更改副本的数量。
1.3.2.Delete删除
删除索引而非数据
1.3.3.Force merge强制合并
强制将索引合并为指定的segment数量,并且索引变为Read only。
1.3.4.Freeze冻结
冻结索引使内存占用达到最小。
1.3.5.Migrate迁移
将索引移动到与当前阶段相对应的数据层。如果在allocate操作中没有指定分配选项,ILM会自动在warm和cold阶段注入迁移操作。
1.3.6.Read only只读
将索引变为只读。
1.3.7.Rollover滚动
根据模板创建新的索引,将当前写入的索引变为旧索引。
1.3.8.Searchable snapshot可搜索的快照
在配置的存储库中获取被管理的索引的快照,并将其挂载为可搜索的快照。如果索引是数据流的一部分,则将挂载的索引将替换流中的原始索引。这个比较多逻辑,建议看原文。
原文:Takes a snapshot of the managed index in the configured repository and mounts it as a searchable snapshot. If the index is part of a data stream, the mounted index replaces the original index in the stream.
1.3.9.Set priority设施优先级
设置阶段的优先级,在节点断电重启后,将按优先级高低启动阶段。所以需要从高到到低设置。Hot最高,以此类推。
1.3.10.Shrink收缩
将索引设置为只读,并将其缩小为具有较少初始分片的新索引。(创建索引时依赖template参数,可能会有较多分片)
1.3.11.Unfollow反跟随
将跨群集复制的跟随索引转换为常规索引。这使收缩、滚动和可搜索的快照操作能够在跟随索引上安全执行。
1.3.12.Wait for snapshot等待快照
删除前对索引做快照。不会完全丢失。
1.4Template模板的概念
模板是rollover操作创建索引时的依据。它阐述了1、索引自身的属性 2、索引关联的生命周期策略。
如下,
PUT _index_template/my_template
{
#索引的名称样式均为text-xxx
"index_patterns": ["test-*"],
"template": {
"settings": {
#索引的分区分片数量
"number_of_shards": 1,
"number_of_replicas": 1,
#索引绑定的生命周期策略
"index.lifecycle.name": "my_policy",
#索引指向的别名
"index.lifecycle.rollover_alias": "test-alias-all"
}
}
}
2.制作生命周期策略
以上概念了解后,就可以开始写一个生命周期策略了。
1、创建生命周期策略
PUT /_ilm/policy/test-policy
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"set_priority": {
"priority": 100
},
"rollover": {
"max_age": "30d"
}
}
},
"warm": {
"min_age": "31d",
"actions": {
"forcemerge": {
"max_num_segments": 1
},
"set_priority": {
"priority": 50
},
"allocate": {
"number_of_replicas": 1
}
}
},
"cold": {
"min_age": "60d",
"actions": {
"set_priority": {
"priority": 0
}
}
},
"delete": {
"min_age": "360d",
"actions": {
"delete": {
"delete_searchable_snapshot": true
}
}
}
}
}
}
2、创建模板
见1.4定义
3、创建别名下得第一个索引
PUT /test-index-000001
{
"aliases": {
"test-alias-all": {
"is_write_index": true
}
}
}
4、设置刷新时间
PUT /_cluster/settings
{
"transient": {
"indices.lifecycle.poll_interval": "5m"
}
}
每次刷新会检查当前索引是否可以进入下一阶段

被折叠的 条评论
为什么被折叠?



