使用 ILM 将 10s 精度降为 1h 的配置示例

解读

面试官真正想考察的是:

  1. 你是否理解 时间序列数据在热→温→冷阶段对精度需求的差异;
  2. 能否用 ILM + Rollup + Index Template 组合,把 10s 粒度的原始索引在“温”阶段无损降精度为 1h,并保证下游业务无感;
  3. 是否熟悉国内常用版本(7.10 开源版为主,部分公司用 8.x 基础版)的功能边界,知道 Rollup 在 8.11 之后被 Downsampling 替代的兼容细节;
  4. 配置落地时要考虑 集群分片上限、存储成本、SLA 等工程约束,不能纸上谈兵。

知识点

  • ILM 四阶段(hot/warm/cold/delete)与 Transition 条件
  • Rollup API 的 metric 与 date_histogram 语法,以及 groupby 字段必须 keyword 化
  • Index Template 的 priority、index.routing.allocation.require.box_type 标签
  • 数据流(Data Stream)与别名(Alias)的读写分离机制
  • 国内云厂商热温冷节点标签差异:阿里云 "warm",腾讯云 "data_warm",需与 cluster.routing.allocation.awareness.attributes 对齐
  • 降精度后查询回退策略:Kibana 通过 "index_pattern": "metric-*-rollup" 自动路由,或业务侧 bool should 双索引查询
  • 降精度任务并发度:persistent task 线程池为 xpack.rollup.task_thread_pool.size,默认 4,大集群需调大避免堆积

答案

以下示例基于 Elasticsearch 7.10 开源版,3 节点热温冷架构,目标:

  • 原始索引 metric-10s-2025.06 10s 精度,保留 7 天;
  • 第 8 天起生成 rollup-metric-1h-2025.06,1h 精度,保留 90 天;
  • 原始索引第 8 天自动冷存,第 91 天删除。

步骤 1:节点打标签
热节点 elasticsearch.yml
node.attr.box_type: hot
温节点
node.attr.box_type: warm
冷节点
node.attr.box_type: cold

步骤 2:创建 ILM 策略
PUT _ilm/policy/metric-10s-1h-policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_age": "1d",
"max_size": "50gb"
},
"set_priority": {
"priority": 100
}
}
},
"warm": {
"min_age": "7d",
"actions": {
"allocate": {
"require": {
"box_type": "warm"
}
},
"rollup": {
"config": {
"groups": {
"date_histogram": {
"field": "@timestamp",
"fixed_interval": "1h"
},
"terms": {
"fields": [ "host.keyword", "region.keyword" ]
}
},
"metrics": [
{
"field": "cpu_usage",
"metrics": [ "avg", "max" ]
},
{
"field": "mem_usage",
"metrics": [ "avg" ]
}
],
"timeout": "20m",
"page_size": 1000
}
},
"set_priority": {
"priority": 50
}
}
},
"cold": {
"min_age": "8d",
"actions": {
"allocate": {
"require": {
"box_type": "cold"
}
},
"freeze": {},
"set_priority": {
"priority": 0
}
}
},
"delete": {
"min_age": "91d",
"actions": {
"delete": {}
}
}
}
}
}

步骤 3:索引模板绑定
PUT _index_template/metric-template
{
"index_patterns": [ "metric-10s-*" ],
"data_stream": {},
"priority": 200,
"template": {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1,
"index.lifecycle.name": "metric-10s-1h-policy",
"index.routing.allocation.require.box_type": "hot",
"index.codec": "best_compression"
},
"mappings": {
"properties": {
"@timestamp": { "type": "date" },
"host": { "type": "keyword" },
"region": { "type": "keyword" },
"cpu_usage": { "type": "double" },
"mem_usage": { "type": "double" }
}
}
}
}

步骤 4:写入数据
POST metric-10s-2025.06/_bulk
{ "create":{ } }
{ "@timestamp":"2025-06-20T14:00:10Z","host":"web01","region":"cn-north-1","cpu_usage":23.5,"mem_usage":55.2 }
...

步骤 5:验证
GET metric-10s-*/_ilm/explain
观察 "phase": "warm" 且 "action": "rollup" 成功后,出现新索引 rollup-metric-1h-2025.06;
查询降精度数据:
GET rollup-metric-1h-2025.06/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"cpu": {
"avg": { "field": "cpu_usage.avg" }
}
}
}

拓展思考

  1. Rollup 与 Downsampling 差异:8.11 之后开源版移除 Rollup,改用 "_downsample" API,需 TSDB 索引+mappings._tsid,配置入口变为 "downsample" 动作,interval 最小单位由 "calendar_interval" 改为 "fixed_interval",且支持 runtime field 二次计算。
  2. 降精度不等于降采样:国内金融场景要求 “可回滚”,可在 warm 阶段先 snapshot 到 COS/OSS,再执行 Rollup,出现误差时 restore 回滚。
  3. 分片上限治理:降精度后索引数翻倍,需用 "_tier_preference" 把冷阶段设为 "data_cold,data_warm",减少 cluster.max_shards_per_node 风险。
  4. 查询一致性:Kibana 默认不识别 rollup 字段,需创建 "rollup_index_pattern" 并勾选 "Show rollup indices",或在业务侧封装 "search_after" 双索引合并逻辑,避免 "doc_count" 误差导致监控误报警。