ILM
(Index Lifecycle Management
,索引生命周期管理)是 Elasticsearch 提供的一种自动化管理索引生命周期的机制。它允许用户根据预先定义的策略,自动执行索引的 滚动更新(rollover
)、分片分配、冻结和删除 等操作,无需人工干预。
rollover
)条件。forcemerge
)、只读设置等。freeze
)以减少资源占用。PUT /_ilm/policy/logs_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_size": "50GB",
"max_age": "30d",
"max_docs": 10000000
},
"set_priority": {
"priority": 100
}
}
},
"warm": {
"min_age": "7d",
"actions": {
"forcemerge": {
"max_num_segments": 1
},
"allocate": {
"number_of_replicas": 1,
"require": {
"data": "warm"
}
},
"set_priority": {
"priority": 50
}
}
},
"cold": {
"min_age": "30d",
"actions": {
"allocate": {
"require": {
"data": "cold"
}
},
"freeze": {},
"set_priority": {
"priority": 0
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
}
}
作用:当索引达到指定条件时自动创建新索引。
常见触发条件:
max_age
:自索引创建后的最长时间。max_docs
:索引包含的最大文档数。max_size
:索引的最大主分片大小。使用示例:
PUT /_ilm/policy/my_rollover_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_age": "7d",
"max_size": "50GB"
}
}
}
}
}
}
作用:减少主分片数量。
适用场景:
示例配置:
"warm": {
"actions": {
"shrink": {
"number_of_shards": 1
}
}
}
作用:合并分段以减少资源占用和提高查询性能.
示例配置:
"warm": {
"actions": {
"forcemerge": {
"max_num_segments": 1
}
}
}
作用:将索引设为只读并减少内存占用。
示例配置:
"cold": {
"actions": {
"freeze": {}
}
}
需求:
ILM 策略:
PUT /_ilm/policy/logs_management_policy_v2
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms", # 明确从索引创建开始
"actions": {
"rollover": {
"max_age": "7d", # 更合理的rollover周期
"max_size": "50GB",
"max_docs": 10000000
},
"set_priority": {
"priority": 100
}
}
},
"warm": {
"min_age": "7d", # 从索引创建后7天进入warm
"actions": {
"forcemerge": {
"max_num_segments": 1
},
"allocate": {
"number_of_replicas": 1,
"require": {
"data": "warm" # 明确要求warm节点
}
},
"set_priority": {
"priority": 50
}
}
},
"cold": {
"min_age": "30d", # 从创建后30天进入cold
"actions": {
"allocate": {
"require": {
"data": "cold" # 明确要求cold节点
}
},
"freeze": {}, # 冻结索引节省资源
"set_priority": {
"priority": 0
}
}
},
"delete": {
"min_age": "90d", # 从创建后90天删除
"actions": {
"delete": {
"delete_searchable_snapshot": true # 可选:删除前检查快照
}
}
}
}
}
}
配套的索引模板示例
PUT /_index_template/logs_template
{
"index_patterns": ["logs-*"],
"template": {
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"index.lifecycle.name": "logs_management_policy_v2",
"index.lifecycle.rollover_alias": "logs"
},
"mappings": {
"properties": {
"@timestamp": { "type": "date" },
"log_level": { "type": "keyword" },
"message": { "type": "text" }
}
}
}
}
需求:
ILM 策略:
PUT /_ilm/policy/orders_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_age": "7d"
},
"set_priority": {
"priority": 100
}
}
},
"warm": {
"min_age": "30d",
"actions": {
"allocate": {
"require": {
"data": "warm"
}
}
}
},
"cold": {
"min_age": "365d",
"actions": {
"allocate": {
"require": {
"data": "cold"
}
}
}
},
"delete": {
"min_age": "1095d", // 3年
"actions": {
"delete": {}
}
}
}
}
}
# 在elasticsearch.yml中
node.attr.data: warm
# 或
node.attr.data: cold
GET /_ilm/status
GET /_ilm/policy/logs_policy
GET logs-*/_ilm/explain
min_age
设置(默认是上次 rollover
后时间)。GET _ilm/explain
诊断。rollover
(会产生大量小索引)。PUT /_index_template/logs_template
{
"index_patterns": ["logs-*"],
"template": {
"settings": {
"index.lifecycle.name": "logs_policy",
"index.lifecycle.rollover_alias": "logs"
}
}
}
POST /_ilm/move/logs-000001
{
"current_step": {
"phase": "new",
"action": "complete",
"name": "complete"
},
"next_step": {
"phase": "hot",
"action": "rollover",
"name": "attempt-rollover"
}
}
min_age
。"set_priority": {
"priority": 100 // 热阶段最高
}
Snapshot Lifecycle Management
)。rollover
条件预估索引数量。通过合理配置 ILM 策略,可以实现索引的全生命周期自动化管理,显著降低运维成本,同时优化存储和查询性能。