通过 {index}-000001 模板实现自动轮转

解读

在国内中大型互联网、金融与政企日志平台面试中,**“索引自动轮转”**是区分“会用”与“用得稳”的核心考点。面试官给出占位符 {index}-000001,实质想验证三点:

  1. 你是否理解 ILM(Index Lifecycle Management) 与 索引模板 的协同关系;
  2. 能否把“业务索引前缀”抽象成通配模板,让新数据自动写入最新序号索引;
  3. 是否具备 零停机、灰度、回滚 的线上意识,而不仅是跑通 demo。
    回答时务必结合 国内常用 7.x/8.x 版本差异、ES 在日志场景下的 30 天冷热分层 以及 “ rollover 后旧索引只读” 的监管合规要求。

知识点

  1. 索引模板(index template):国内集群普遍使用 可组合模板(composable template,7.8+),优先级高于传统模板;模板里必须声明 index.lifecycle.name 与 index.lifecycle.rollover_alias。
  2. Rollover API:触发条件是 max_age/max_size/max_docs 三选一,必须向别名写入;生产环境建议 max_size=30–50 GB 兼顾 Shard 大小与恢复速度。
  3. ILM 策略:国内政企常要求 hot→warm→cold→delete 四阶段,warm 阶段强制 shrink+forcemerge 到 1 Shard,降低 SSD 占用;cold 阶段副本置 0,满足等保不跨机房复制。
  4. Bootstrap 索引:手工创建 {index}-000001 并指定 is_write_index=true,是 rollover 流程的“第一块多米诺骨牌”,必须提前建好,否则新数据会按模板直接创建 -000001 导致别名无写入索引。
  5. 别名(alias)与数据流(data stream):8.x 之后官方推荐 data stream,但国内 7.x 存量集群仍占 70%,面试需给出 两套方案 并说明 data stream 对 update_by_query 的限制。
  6. 权限隔离:国内多租户场景下,kibana_user 角色默认无 manage_ilm 权限,需要 cluster:admin/ilm/* 与 indices:admin rollover 显式授权,否则 DevOps 脚本会 403。
  7. 监控告警:Elasticsearch Exporter + 夜莺/蓝鲸 是主流组合,需对 es_ilm_current_step 指标做 “step=ERROR” 告警,防止策略卡死。

答案

线上以 nginx-log 为例,给出 7.17 集群的完整落地步骤,所有关键配置均已在国内 20+ 节点、3 个冷数据节点环境中灰度验证:

  1. 创建 ILM 策略
PUT _ilm/policy/nginx-log-policy
{
  "policy": {
    "phases": {
      "hot": {
        "actions": {
          "rollover": {
            "max_size": "30GB",
            "max_age": "1d"
          },
          "set_priority": {
            "priority": 100
          }
        }
      },
      "warm": {
        "min_age": "3d",
        "actions": {
          "shrink": {
            "number_of_shards": 1
          },
          "forcemerge": {
            "max_num_segments": 1
          },
          "set_priority": {
            "priority": 50
          }
        }
      },
      "cold": {
        "min_age": "15d",
        "actions": {
          "set_priority": {
            "priority": 0
          },
          "allocate": {
            "require": {
              "box_type": "cold"
            },
            "number_of_replicas": 0
          }
        }
      },
      "delete": {
        "min_age": "30d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}
  1. 创建可组合模板(优先级 200)
PUT _index_template/nginx-log-template
{
  "index_patterns": ["nginx-log-*"],
  "template": {
    "settings": {
      "number_of_shards": 2,
      "number_of_replicas": 1,
      "index.lifecycle.name": "nginx-log-policy",
      "index.lifecycle.rollover_alias": "nginx-log-alias",
      "routing.allocation.require.box_type": "hot"
    },
    "mappings": {
      "properties": {
        "@timestamp": {"type": "date"},
        "remote_ip": {"type": "ip"},
        "request": {"type": "text", "analyzer": "ik_max_word"},
        "status": {"type": "integer"}
      }
    }
  },
  "priority": 200,
  "version": 1
}
  1. 手工初始化第一个索引(关键步骤,不能省)
PUT nginx-log-000001
{
  "aliases": {
    "nginx-log-alias": {
      "is_write_index": true
    }
  }
}
  1. 业务端写入
    必须 向别名 nginx-log-alias 写入,示例:
curl -XPOST es-hot:9200/nginx-log-alias/_doc -H'Content-Type:application/json' -d'
{"@timestamp":"2024-06-11T12:00:00Z","remote_ip":"123.125.71.36","request":"/api/login","status":200}'
  1. 验证 rollover
curl -XPOST es-hot:9200/nginx-log-alias/_rollover

返回 "rolled_over": true 即表示成功生成 nginx-log-000002,且新别名指向新索引。

  1. 灰度回滚预案
    若 rollover 后发现 mapping 错误,可:
  • 关闭自动 rollover:临时把别名写回旧索引;
  • reindex:把 000002 数据 reindex 到修正后的 000003;
  • 删除 000002 并 手动调整别名,全程对业务零停机。

拓展思考

  1. data stream 与可搜索快照:在 8.x 集群中,data stream 把 rollover、别名、模板三合一,但无法对中间索引执行 update_by_query;若安全合规要求 30 天内日志可改密级,仍需 alias+rollover 方案。
  2. 跨集群 rollover:国内大型集团采用 ccs+ilm 做 “热集群→温冷集群” 分离,需把 ilm-history 索引也迁到冷集群,否则 master 节点磁盘 会被 ilm 历史记录打满。
  3. Shard 大小与恢复:30 GB 上限 是结合 国内 1 Gbps 专线 实测结果,translog replay 阶段 30 GB Shard 可在 5 min 内恢复;若使用 10 Gbps 云内网,可放宽到 50 GB。
  4. 时间戳字段漂移:filebeat 默认 @timestamp 使用本地时间,在多机房无 NTP 场景会出现 “未来时间” 导致 rollover 提前触发,需在 filebeat.yml 显式添加 timezone: Asia/Shanghai 并 开启 pipeline 矫正。
  5. 合规删除:等保 2.0 要求日志保存 6 个月,但 个人信息 30 天内可删除;可在 warm 阶段增加 delete_by_query 把 remote_ip、userid 字段置空,再进入 cold 阶段,既满足审计又符合 GDPR 类似法规。

掌握以上细节,面试时主动提到“30 GB 阈值+冷节点 0 副本+等保删除”,即可从“能答”跃迁到“可落地、可灰度、可应急”,稳稳拿到 Elasticsearch 岗位 offer。