Elasticsearch索引生命周期管理 #
一、ILM概述 #
1.1 ILM作用 #
text
ILM作用
├── 自动化管理
│ ├── 自动滚动索引
│ ├── 自动调整设置
│ └── 自动删除过期数据
├── 数据分层
│ ├── 热数据层
│ ├── 温数据层
│ └── 冷数据层
└── 降低成本
└── 自动迁移和删除
1.2 生命周期阶段 #
| 阶段 | 说明 |
|---|---|
| Hot | 热数据,频繁写入和查询 |
| Warm | 温数据,较少查询,只读 |
| Cold | 冷数据,偶尔查询,归档 |
| Delete | 删除数据 |
二、创建ILM策略 #
2.1 基本策略 #
bash
PUT /_ilm/policy/logs_policy
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "1d"
},
"set_priority": {
"priority": 100
}
}
},
"warm": {
"min_age": "7d",
"actions": {
"forcemerge": {
"max_num_segments": 1
},
"shrink": {
"number_of_shards": 1
},
"readonly": {},
"set_priority": {
"priority": 50
}
}
},
"cold": {
"min_age": "30d",
"actions": {
"freeze": {},
"set_priority": {
"priority": 0
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
}
}
2.2 查看策略 #
bash
GET /_ilm/policy/logs_policy
GET /_ilm/policy
2.3 删除策略 #
bash
DELETE /_ilm/policy/logs_policy
三、阶段详解 #
3.1 Hot阶段 #
json
{
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "1d",
"max_docs": 1000000000
},
"set_priority": {
"priority": 100
}
}
}
}
rollover条件:
| 条件 | 说明 |
|---|---|
| max_size | 最大索引大小 |
| max_age | 最大索引年龄 |
| max_docs | 最大文档数 |
3.2 Warm阶段 #
json
{
"warm": {
"min_age": "7d",
"actions": {
"forcemerge": {
"max_num_segments": 1
},
"shrink": {
"number_of_shards": 1
},
"readonly": {},
"allocate": {
"require": {
"data": "warm"
}
},
"set_priority": {
"priority": 50
}
}
}
}
可用操作:
| 操作 | 说明 |
|---|---|
| forcemerge | 强制合并段 |
| shrink | 收缩分片数 |
| readonly | 设置只读 |
| allocate | 分配到特定节点 |
| set_priority | 设置优先级 |
3.3 Cold阶段 #
json
{
"cold": {
"min_age": "30d",
"actions": {
"freeze": {},
"allocate": {
"require": {
"data": "cold"
}
},
"set_priority": {
"priority": 0
}
}
}
}
可用操作:
| 操作 | 说明 |
|---|---|
| freeze | 冻结索引 |
| allocate | 分配到特定节点 |
| set_priority | 设置优先级 |
3.4 Delete阶段 #
json
{
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
四、应用ILM策略 #
4.1 创建索引模板 #
bash
PUT /_index_template/logs_template
{
"index_patterns": ["logs-*"],
"template": {
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"lifecycle.name": "logs_policy",
"lifecycle.rollover_alias": "logs"
},
"aliases": {
"logs": {
"is_write_index": true
}
}
}
}
4.2 创建初始索引 #
bash
PUT /logs-000001
{
"aliases": {
"logs": {
"is_write_index": true
}
}
}
4.3 写入数据 #
bash
POST /logs/_doc
{
"message": "Log entry",
"@timestamp": "2024-01-01T00:00:00Z"
}
五、节点角色配置 #
5.1 节点角色 #
yaml
node.roles: [data_hot, data_content]
node.roles: [data_warm, data_content]
node.roles: [data_cold, data_content]
5.2 节点属性 #
yaml
node.attr.data: hot
node.attr.data: warm
node.attr.data: cold
5.3 分配策略 #
json
{
"allocate": {
"require": {
"data": "warm"
}
}
}
六、ILM监控 #
6.1 查看ILM状态 #
bash
GET /_ilm/status
6.2 查看索引ILM状态 #
bash
GET /logs-000001/_ilm/explain
响应:
json
{
"indices": {
"logs-000001": {
"index": "logs-000001",
"managed": true,
"policy": "logs_policy",
"phase": "hot",
"action": "rollover",
"step": "check-rollover-ready",
"phase_time": 1704067200000,
"action_time": 1704067200000,
"step_time": 1704067200000
}
}
}
6.3 重试ILM #
bash
POST /logs-000001/_ilm/retry
七、ILM API #
7.1 启动ILM #
bash
POST /_ilm/start
7.2 停止ILM #
bash
POST /_ilm/stop
7.3 移除ILM策略 #
bash
PUT /logs-000001/_settings
{
"lifecycle.name": null
}
八、热温冷架构 #
8.1 架构图 #
text
热温冷架构
┌─────────────────────────────────────────────┐
│ Hot Layer │
│ ├── 高性能存储(SSD) │
│ ├── 频繁写入和查询 │
│ └── 多副本 │
├─────────────────────────────────────────────┤
│ Warm Layer │
│ ├── 普通存储(HDD) │
│ ├── 只读查询 │
│ └── 减少副本 │
├─────────────────────────────────────────────┤
│ Cold Layer │
│ ├── 归档存储 │
│ ├── 偶尔查询 │
│ └── 冻结索引 │
└─────────────────────────────────────────────┘
8.2 完整策略示例 #
bash
PUT /_ilm/policy/hot_warm_cold_policy
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "1d"
},
"set_priority": {
"priority": 100
}
}
},
"warm": {
"min_age": "7d",
"actions": {
"forcemerge": {
"max_num_segments": 1
},
"shrink": {
"number_of_shards": 1
},
"allocate": {
"require": {
"data": "warm"
}
},
"readonly": {},
"set_priority": {
"priority": 50
}
}
},
"cold": {
"min_age": "30d",
"actions": {
"freeze": {},
"allocate": {
"require": {
"data": "cold"
}
},
"set_priority": {
"priority": 0
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
}
}
九、最佳实践 #
9.1 策略设计 #
text
策略设计建议
├── 合理设置阶段时间
│ └── 根据数据访问模式
├── 合理设置滚动条件
│ └── 避免过小或过大
├── 使用forcemerge
│ └── 减少段数量
└── 使用shrink
└── 减少分片数量
9.2 监控建议 #
text
监控建议
├── 监控ILM执行状态
├── 监控索引大小
├── 监控存储空间
└── 设置告警
十、总结 #
本章介绍了Elasticsearch索引生命周期管理:
- ILM自动化管理索引生命周期
- 四个阶段:hot、warm、cold、delete
- 滚动操作创建新索引
- 热温冷架构优化成本
- 合理配置策略和监控
- 与索引模板配合使用
下一步,我们将学习集群架构。
最后更新:2026-03-27