ES索引的生命周期管理

ES索引的生命周期管理

介绍

​ ES可用于索引日志类数据,在此场景下,数据是源源不断地被写入到索引中。为了使索引的文档不会过多,查询的性能更好,我们希望索引可以根据大小文档数量索引已创建时长等指标进行自动回滚,可以自定义将超过一定时间的数据进行自动删除。ES为我们提供了索引的生命周期管理来帮助处理此场景下的问题。

​ 索引的生命周期分为四个阶段:HOT->WARM->COLD->DELETE

​ HOT为必须的阶段外,其他为非必须阶段,可以任意选择配置。在日志类场景下不需要WARN和COLD阶段,下文只配置了HOT与DELETE阶段。

环境

  • Elasticsearch: 6.8
  • Kibana:6.8

配置过程

  1. 配置生命周期管理策略
PUT _ilm/policy/datastream_policy   
{
  "policy": {                       
    "phases": {
      "hot": {                      
        "actions": {
          "rollover": {             
            "max_docs" : 1
          }
        }
      },
      "delete": {
        "min_age": "30s",           
        "actions": {
          "delete": {}              
        }
      }
    }
  }
}

创建一个名为“datastream_policy“的策略,包含HOT和DELETE阶段。

  • HOT阶段:当日志索引文档数量超过1条时就会发生rollover。
  • DELETE阶段:超过30秒时执行删除。

另外,hot->actions->rollover还支持其他维度的控制,比如:

"max_size": "50GB"
"max_age": "30d"
  1. 配置索引模板
PUT _template/datastream_template
{
  "index_patterns": ["datastream-*"],                 
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "index.lifecycle.name": "datastream_policy",      
    "index.lifecycle.rollover_alias": "datastream"    
  }
}

创建一个索引模版,以datastream-开头的索引自动采用settings的配置。

  • "index.lifecycle.name": "datastream_policy":使用的策略为“datastream_policy”
  • "index.lifecycle.rollover_alias": "datastream":使用该模版创建的索引统一用”datastream“的别名进行管理。
  1. 创建索引
PUT datastream-000001
{
  "aliases": {
    "datastream": {
      "is_write_index": true
    }
  }
}

创建一个起始索引,设置索引别名,并设置索引别名可写入。

  1. 配置lifecycle检测时间
PUT /_cluster/settings{
  "transient": {
    "indices.lifecycle.poll_interval": "10s" 
  }
}

es会根据设置时间定期检测,默认为十分钟,为了测试看效果,改为10秒。

验证

使用索引别名,写入一条数据

PUT /datastream/_doc
{
	"message":"hello"
}

根据lifecycle时间配置,写入数据10秒后,使用以下命令检测进度

GET datastream-*/_ilm/explain

自动创建了一个新的索引(datastream-000002),返回数据:

{
  "indices" : {
    "datastream-000002" : {
      "index" : "datastream-000002",
      "managed" : true,
      "policy" : "datastream_policy",
      "lifecycle_date_millis" : 1631874381522,
      "phase" : "new",
      "phase_time_millis" : 1631874381659,
      "action" : "complete",
      "action_time_millis" : 1631874381659,
      "step" : "complete",
      "step_time_millis" : 1631874381659
    },
    "datastream-000001" : {
      "index" : "datastream-000001",
      "managed" : true,
      "policy" : "datastream_policy",
      "lifecycle_date_millis" : 1631874367828,
      "phase" : "hot",
      "phase_time_millis" : 1631874367975,
      "action" : "rollover",
      "action_time_millis" : 1631874371733,
      "step" : "attempt-rollover",
      "step_time_millis" : 1631874381440,
      "phase_execution" : {
        "policy" : "datastream_policy",
        "phase_definition" : {
          "min_age" : "0ms",
          "actions" : {
            "rollover" : {
              "max_docs" : 1
            }
          }
        },
        "version" : 1,
        "modified_date_in_millis" : 1631874359862
      }
    }
  }
}

再等20秒后,旧的索引(datastream-000001)被标记为删除,查看进度返回数据:

{
  "indices" : {
    "datastream-000002" : {
      "index" : "datastream-000002",
      "managed" : true,
      "policy" : "datastream_policy",
      "lifecycle_date_millis" : 1631874381522,
      "phase" : "hot",
      "phase_time_millis" : 1631874381863,
      "action" : "rollover",
      "action_time_millis" : 1631874391733,
      "step" : "check-rollover-ready",
      "step_time_millis" : 1631874391733,
      "phase_execution" : {
        "policy" : "datastream_policy",
        "phase_definition" : {
          "min_age" : "0ms",
          "actions" : {
            "rollover" : {
              "max_docs" : 1
            }
          }
        },
        "version" : 1,
        "modified_date_in_millis" : 1631874359862
      }
    },
    "datastream-000001" : {
      "index" : "datastream-000001",
      "managed" : true,
      "policy" : "datastream_policy",
      "lifecycle_date_millis" : 1631874381943,
      "phase" : "delete",
      "phase_time_millis" : 1631874421434,
      "action" : "delete",
      "action_time_millis" : 1631874421434,
      "step" : "wait-for-shard-history-leases",
      "step_time_millis" : 1631874421434,
      "phase_execution" : {
        "policy" : "datastream_policy",
        "phase_definition" : {
          "min_age" : "30s",
          "actions" : {
            "delete" : { }
          }
        },
        "version" : 1,
        "modified_date_in_millis" : 1631874359862
      }
    }
  }
}

再次等待10秒后,旧的索引已被删除,查看进度返回数据:

{
  "indices" : {
    "datastream-000002" : {
      "index" : "datastream-000002",
      "managed" : true,
      "policy" : "datastream_policy",
      "lifecycle_date_millis" : 1631874381522,
      "phase" : "hot",
      "phase_time_millis" : 1631874381863,
      "action" : "rollover",
      "action_time_millis" : 1631874391733,
      "step" : "check-rollover-ready",
      "step_time_millis" : 1631874391733,
      "phase_execution" : {
        "policy" : "datastream_policy",
        "phase_definition" : {
          "min_age" : "0ms",
          "actions" : {
            "rollover" : {
              "max_docs" : 1
            }
          }
        },
        "version" : 1,
        "modified_date_in_millis" : 1631874359862
      }
    }
  }
}

其他

  1. 如果一个索引的生命周期管理已运行完成,那么对应策略的修改不会重新触发索引生命周期管理的重新运行。 若想更新后的策略在已完成生命周期管理的索引上生效,需要单独的命令干预。如下:

    • 通过将索引生命周期的运行状态调整到修改阶段之前,以下两个示例都将状态调整HOT阶段。(官网:https://www.elastic.co/guide/en/elasticsearch/reference/6.8/ilm-move-to-step.html)

      # 调整到hot阶段-回滚之前
      POST _ilm/move/datastream-000001
      {
        "current_step": { 
          "phase": "completed",
          "action": "completed",
          "name": "completed"
        },
        "next_step": { 
          "phase": "hot",
          "action": "rollover",
          "name": "check-rollover-ready"
        }
      }
      
      # 调整到hot阶段-回滚之后
      POST _ilm/move/datastream-000001
      {
        "current_step": { 
          "phase": "completed",
          "action": "completed",
          "name": "completed"
        },
        "next_step": { 
          "phase": "hot",
          "action": "complete",
          "name": "complete"
        }
      }
      
  2. 索引的生命周期管理中,利用到了索引别名,新建的索引会被自动关联到既定的别名上。向索引中写入文档和从索引中查询文档,都通过别名指定,所以保证了整个过程对于客户端透明。

参考:

https://www.elastic.co/guide/en/elasticsearch/reference/6.8/index-lifecycle-management.html

https://www.cnblogs.com/runnerjack/p/12758658.html

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值