教程:将 ILM 管理的数据流迁移到数据流生命周期
编辑教程:将 ILM 管理的数据流迁移到数据流生命周期编辑
在本教程中,我们将介绍如何将现有数据流从索引生命周期管理 (ILM) 迁移到数据流生命周期。现有的 ILM 管理的后台索引将继续由 ILM 管理,直到它们过期并被 ILM 删除;但是,新的后台索引将由数据流生命周期管理。这样,数据流会逐渐从 ILM 管理迁移到数据流生命周期管理。正如我们将看到的,ILM 和数据流生命周期可以共同管理数据流;但是,一个索引一次只能由一个系统管理。
TL;DR编辑
要将数据流从 ILM 迁移到数据流生命周期,我们需要执行两个步骤
- 更新支持数据流的索引模板,将 prefer_ilm 设置为
false
,并配置数据流生命周期。 - 使用 生命周期 API 为现有数据流配置数据流生命周期。
有关更多详细信息,请参阅 迁移到数据流生命周期 部分。
设置 ILM 管理的数据流编辑
首先,我们创建一个数据流,其中包含两个由 ILM 管理的后台索引。我们首先创建一个 ILM 策略
response = client.ilm.put_lifecycle( policy: 'pre-dsl-ilm-policy', body: { policy: { phases: { hot: { actions: { rollover: { max_primary_shard_size: '50gb' } } }, delete: { min_age: '7d', actions: { delete: {} } } } } } ) puts response
PUT _ilm/policy/pre-dsl-ilm-policy { "policy": { "phases": { "hot": { "actions": { "rollover": { "max_primary_shard_size": "50gb" } } }, "delete": { "min_age": "7d", "actions": { "delete": {} } } } } }
然后,我们创建一个索引模板,该模板将支持数据流并配置 ILM
response = client.indices.put_index_template( name: 'dsl-data-stream-template', body: { index_patterns: [ 'dsl-data-stream*' ], data_stream: {}, priority: 500, template: { settings: { 'index.lifecycle.name' => 'pre-dsl-ilm-policy' } } } ) puts response
PUT _index_template/dsl-data-stream-template { "index_patterns": ["dsl-data-stream*"], "data_stream": { }, "priority": 500, "template": { "settings": { "index.lifecycle.name": "pre-dsl-ilm-policy" } } }
现在,我们将索引一个目标为 dsl-data-stream
的文档以创建数据流,并且我们还将手动滚动数据流以创建另一个世代索引
response = client.index( index: 'dsl-data-stream', body: { "@timestamp": '2023-10-18T16:21:15.000Z', message: '192.0.2.42 - - [06/May/2099:16:21:15 +0000] "GET /images/bg.jpg HTTP/1.0" 200 24736' } ) puts response
POST dsl-data-stream/_doc? { "@timestamp": "2023-10-18T16:21:15.000Z", "message": "192.0.2.42 - - [06/May/2099:16:21:15 +0000] \"GET /images/bg.jpg HTTP/1.0\" 200 24736" }
response = client.indices.rollover( alias: 'dsl-data-stream' ) puts response
POST dsl-data-stream/_rollover
我们将使用 GET _data_stream API 检查数据流的状态
response = client.indices.get_data_stream( name: 'dsl-data-stream' ) puts response
GET _data_stream/dsl-data-stream
检查响应,我们将看到两个后台索引都由 ILM 管理,并且下一代索引也将由 ILM 管理
{ "data_streams": [ { "name": "dsl-data-stream", "timestamp_field": { "name": "@timestamp" }, "indices": [ { "index_name": ".ds-dsl-data-stream-2023.10.19-000001", "index_uuid": "xCEhwsp8Tey0-FLNFYVwSg", "prefer_ilm": true, "ilm_policy": "pre-dsl-ilm-policy", "managed_by": "Index Lifecycle Management" }, { "index_name": ".ds-dsl-data-stream-2023.10.19-000002", "index_uuid": "PA_JquKGSiKcAKBA8DJ5gw", "prefer_ilm": true, "ilm_policy": "pre-dsl-ilm-policy", "managed_by": "Index Lifecycle Management" } ], "generation": 2, "status": "GREEN", "template": "dsl-data-stream-template", "next_generation_managed_by": "Index Lifecycle Management", "prefer_ilm": true, "ilm_policy": "pre-dsl-ilm-policy", "hidden": false, "system": false, "allow_custom_routing": false, "replicated": false, "rollover_on_write": false } ] }
后台索引的名称。 |
|
对于每个后台索引,我们都会显示 prefer_ilm 配置的值,该值将指示如果两个系统都为索引配置,则 ILM 是否优先于数据流生命周期。 |
|
为此索引配置的 ILM 策略。 |
|
管理此索引的系统(可能的值为“索引生命周期管理”、“数据流生命周期”或“未管理”)。 |
|
管理下一代索引的系统(此数据流的新的写入索引,一旦数据流滚动)。可能的值为“索引生命周期管理”、“数据流生命周期”或“未管理”。 |
|
在支持数据流的索引模板中配置的 prefer_ilm 值。此值将为所有新的后台索引配置。如果未在索引模板中配置,则后台索引将接收 |
|
在支持此数据流的索引模板中配置的 ILM 策略(只要它存在于索引模板中,它就会在所有新的后台索引上配置)。 |
将数据流迁移到数据流生命周期编辑
要将 dsl-data-stream
迁移到数据流生命周期,我们需要执行两个步骤
- 更新支持数据流的索引模板,将 prefer_ilm 设置为
false
,并配置数据流生命周期。 - 使用 生命周期 API 为现有
dsl-data-stream
配置数据流生命周期。
添加到索引模板中的数据流生命周期配置,作为数据流配置,仅适用于新的数据流。我们的数据流已经存在,因此即使我们在索引模板中添加了数据流生命周期配置,它也不会应用于 dsl-data-stream
。
response = client.indices.put_index_template( name: 'dsl-data-stream-template', body: { index_patterns: [ 'dsl-data-stream*' ], data_stream: {}, priority: 500, template: { settings: { 'index.lifecycle.name' => 'pre-dsl-ilm-policy', 'index.lifecycle.prefer_ilm' => false }, lifecycle: { data_retention: '7d' } } } ) puts response
PUT _index_template/dsl-data-stream-template { "index_patterns": ["dsl-data-stream*"], "data_stream": { }, "priority": 500, "template": { "settings": { "index.lifecycle.name": "pre-dsl-ilm-policy", "index.lifecycle.prefer_ilm": false }, "lifecycle": { "data_retention": "7d" } } }
我们现在已经确保新的数据流将由数据流生命周期管理。
让我们更新我们现有的 dsl-data-stream
并配置数据流生命周期
response = client.indices.put_data_lifecycle( name: 'dsl-data-stream', body: { data_retention: '7d' } ) puts response
PUT _data_stream/dsl-data-stream/_lifecycle { "data_retention": "7d" }
我们可以检查数据流以确认下一代确实将由数据流生命周期管理
response = client.indices.get_data_stream( name: 'dsl-data-stream' ) puts response
GET _data_stream/dsl-data-stream
{ "data_streams": [ { "name": "dsl-data-stream", "timestamp_field": { "name": "@timestamp" }, "indices": [ { "index_name": ".ds-dsl-data-stream-2023.10.19-000001", "index_uuid": "xCEhwsp8Tey0-FLNFYVwSg", "prefer_ilm": true, "ilm_policy": "pre-dsl-ilm-policy", "managed_by": "Index Lifecycle Management" }, { "index_name": ".ds-dsl-data-stream-2023.10.19-000002", "index_uuid": "PA_JquKGSiKcAKBA8DJ5gw", "prefer_ilm": true, "ilm_policy": "pre-dsl-ilm-policy", "managed_by": "Index Lifecycle Management" } ], "generation": 2, "status": "GREEN", "template": "dsl-data-stream-template", "lifecycle": { "enabled": true, "data_retention": "7d" }, "ilm_policy": "pre-dsl-ilm-policy", "next_generation_managed_by": "Data stream lifecycle", "prefer_ilm": false, "hidden": false, "system": false, "allow_custom_routing": false, "replicated": false, "rollover_on_write": false } ] }
现有的后台索引将继续由 ILM 管理 |
|
现有的后台索引将继续由 ILM 管理 |
|
下一代索引将由数据流生命周期管理 |
|
我们在索引模板中配置的 |
我们现在将滚动数据流以查看由数据流生命周期管理的新一代索引
response = client.indices.rollover( alias: 'dsl-data-stream' ) puts response
POST dsl-data-stream/_rollover
response = client.indices.get_data_stream( name: 'dsl-data-stream' ) puts response
GET _data_stream/dsl-data-stream
{ "data_streams": [ { "name": "dsl-data-stream", "timestamp_field": { "name": "@timestamp" }, "indices": [ { "index_name": ".ds-dsl-data-stream-2023.10.19-000001", "index_uuid": "xCEhwsp8Tey0-FLNFYVwSg", "prefer_ilm": true, "ilm_policy": "pre-dsl-ilm-policy", "managed_by": "Index Lifecycle Management" }, { "index_name": ".ds-dsl-data-stream-2023.10.19-000002", "index_uuid": "PA_JquKGSiKcAKBA8DJ5gw", "prefer_ilm": true, "ilm_policy": "pre-dsl-ilm-policy", "managed_by": "Index Lifecycle Management" }, { "index_name": ".ds-dsl-data-stream-2023.10.19-000003", "index_uuid": "PA_JquKGSiKcAKBA8abcd1", "prefer_ilm": false, "ilm_policy": "pre-dsl-ilm-policy", "managed_by": "Data stream lifecycle" } ], "generation": 3, "status": "GREEN", "template": "dsl-data-stream-template", "lifecycle": { "enabled": true, "data_retention": "7d" }, "ilm_policy": "pre-dsl-ilm-policy", "next_generation_managed_by": "Data stream lifecycle", "prefer_ilm": false, "hidden": false, "system": false, "allow_custom_routing": false, "replicated": false, "rollover_on_write": false } ] }
在滚动之前存在的后台索引将继续由 ILM 管理 |
|
在滚动之前存在的后台索引将继续由 ILM 管理 |
|
新的写入索引为 |
|
新的写入索引由 |
将数据流迁移回 ILM编辑
我们可以轻松地将此数据流更改为由 ILM 管理,因为我们在 更新索引模板 时没有删除 ILM 策略。
我们可以通过两种方式实现这一点
- 从数据流中删除生命周期
- 通过将
enabled
标志配置为false
来禁用数据流生命周期。
让我们实现选项 2 并禁用数据流生命周期
response = client.indices.put_data_lifecycle( name: 'dsl-data-stream', body: { data_retention: '7d', enabled: false } ) puts response
response = client.indices.get_data_stream( name: 'dsl-data-stream' ) puts response
GET _data_stream/dsl-data-stream
{ "data_streams": [ { "name": "dsl-data-stream", "timestamp_field": { "name": "@timestamp" }, "indices": [ { "index_name": ".ds-dsl-data-stream-2023.10.19-000001", "index_uuid": "xCEhwsp8Tey0-FLNFYVwSg", "prefer_ilm": true, "ilm_policy": "pre-dsl-ilm-policy", "managed_by": "Index Lifecycle Management" }, { "index_name": ".ds-dsl-data-stream-2023.10.19-000002", "index_uuid": "PA_JquKGSiKcAKBA8DJ5gw", "prefer_ilm": true, "ilm_policy": "pre-dsl-ilm-policy", "managed_by": "Index Lifecycle Management" }, { "index_name": ".ds-dsl-data-stream-2023.10.19-000003", "index_uuid": "PA_JquKGSiKcAKBA8abcd1", "prefer_ilm": false, "ilm_policy": "pre-dsl-ilm-policy", "managed_by": "Index Lifecycle Management" } ], "generation": 3, "status": "GREEN", "template": "dsl-data-stream-template", "lifecycle": { "enabled": false, "data_retention": "7d" }, "ilm_policy": "pre-dsl-ilm-policy", "next_generation_managed_by": "Index Lifecycle Management", "prefer_ilm": false, "hidden": false, "system": false, "allow_custom_routing": false, "replicated": false, "rollover_on_write": false } ] }
如果我们在 更新 索引模板时从索引模板中删除了 ILM 策略,则数据流的写入索引现在将为 Unmanaged
,因为索引将没有配置的 ILM 策略可以回退。