按原文讲透Elasticsearch生命周期策略ILM

目录

前言

基础概念

重点

阶段的概念

操作的概念

1.Allocate分配

2.Delete删除

3.Force merge强制合并

4.Freeze冻结

5.Migrate迁移

6.Read only只读

7.Rollover滚动

8.Searchable snapshot可搜索的快照

9.Set priority设施优先级

10.Shrink收缩

11.Unfollow反跟随

12.Wait for snapshot等待快照

Template模板的概念

制作生命周期策略

前言

参考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,对应关系如下表。

   HOTWARMCOLDDELETE
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"
	}
}

每次刷新会检查当前索引是否可以进入下一阶段

ILM(Index Lifecycle Management)是Elasticsearch提供的一种功能,用于管理索引的生命周期ILM的目标是通过自动化索引的创建、删除和转换,以及自动化数据的热、温和冷存储来简化索引的管理。 ILM生命周期可以分为以下几个阶段: 1. 索引创建阶段(Index Creation Phase):在此阶段,定义索引的模板并创建初始索引。可以指定索引的名称、分片和副本数等参数。 2. 热阶段(Hot Phase):在此阶段,索引处于活动状态,接收并处理实时数据。可以定义索引在何时进入热阶段并设置要保留的文档数量或时间范围。 3. 温阶段(Warm Phase):当索引进入温阶段时,可以对其进行一些优化,例如合并段、优化存储结构等,以提高查询性能。可以定义何时将索引迁移到温节点,并设置何时触发优化操作。 4. 冷阶段(Cold Phase):在冷阶段,将索引从温节点迁移到冷节点,以节省存储成本。可以定义何时将索引迁移到冷节点,并设置何时触发归档或删除操作。 5. 删除阶段(Delete Phase):在此阶段,根据设定的策略删除索引。可以根据索引的年龄、文档数量或其他条件设置何时删除索引。 ILM的配置可以通过Elasticsearch的API或者Kibana界面进行操作,可以根据需求灵活地定义和调整索引的生命周期。通过使用ILM,可以自动化和简化索引管理,提高系统的可靠性和可用性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值