Rollover - ES的某个index满足一定的policy后,这个index的alias将自动重定向到一个新的index中去写入数据。
这个特性简化了一定的业务操作,不需要业务侧每次都通过自己设定规则、跟踪创建index,管理index生命周期。
显而易见,rollover这个特性很适合用来处理时序化数据;当你使用Filebeat、Logstash或者其他方式将带时间戳的文档索引到ES中时,通过使用索引别名、可以方便的定期滚动到新索引中去。
1. 使用PUT policy API创建一个lifecycle policy
所谓的lifecycle policy就是指定索引生命周期的各个阶段以及每个阶段要执行的操作。
生命周期可以有四个阶段:hot、warm、code、delete。
操作策略在Json中定义并通过PUT policy APT添加。
例如:下面的例子创建了一个名称为datastream_policy的policy,这个policy有两个阶段
curl -X PUT "localhost:9200/_ilm/policy/datastream_policy?pretty" -H 'Content-Type: application/json' -d'
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_size": "50GB",
"max_age": "30d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
}
}
'
2. 创建一个index template应用到每一个新建的index
为了自动的将一个lifecycle policy应用到每一个新建的index上,最好给集群的index template绑定一下这个policy。
例如:下面的例子创建了一个datastream_template,所有名称满足正则datastream-*的index,将会应用这个policy。
curl -X PUT "localhost:9200/_template/datastream_template?pretty" -H 'Content-Type: application/json' -d'
{
"index_patterns": ["datastream-*"],
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1,
"index.lifecycle.name": "datastream_policy",
"index.lifecycle.rollover_alias": "datastream"
}
}
'
3. Boostrap一个时序index(后面的index都将根据模板自动创建)
为了使得你的rollover生效,首先得初始化一个满足rollover规则的index,指定一下aliases。
index名称必须满足policy里面的正则表达式模式(datastream-*),并且以一个number结尾。
这样,rollover特性将自动递增的创建以后的index。
例如:下面的例子,当满足rollover policy之后,下一个新建的index将是datastream-000002,以此类推。
curl -X PUT "localhost:9200/datastream-000001?pretty" -H 'Content-Type: application/json' -d'
{
"aliases": {
"datastream": {
"is_write_index": true
}
}
}
'
使用ILM explain API来监测任务进程
curl -X GET "localhost:9200/datastream-*/_ilm/explain?pretty"
响应结果如下:
{
"indices": {
"datastream-000001": {
"index": "datastream-000001",
"managed": true,
"policy": "datastream_policy",
"lifecycle_date_millis": 1538475653281,
"age": "30s",
"phase": "hot",
"phase_time_millis": 1538475653317,
"action": "rollover",
"action_time_millis": 1538475653317,
"step": "attempt-rollover",
"step_time_millis": 1538475653317,
"phase_execution": {
"policy": "datastream_policy",
"phase_definition": {
"min_age": "0ms", // default to 0ms, 表示立即进入hot phase
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
},
"version": 1,
"modified_date_in_millis": 1539609701576
}
}
}
}
curl -X PUT "localhost:9200/_ilm/policy/full_policy?pretty" -H 'Content-Type: application/json' -d'
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_age": "7d",
"max_size": "50G"
}
}
},
"warm": {
"min_age": "30d",
"actions": {
"forcemerge": {
"max_num_segments": 1
},
"shrink": {
"number_of_shards": 1
},
"allocate": {
"number_of_replicas": 2
}
}
},
"cold": {
"min_age": "60d",
"actions": {
"allocate": {
"require": {
"type": "cold"
}
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
}
}
'