Elasticsearch生命周期管理那些事儿-rollover

背景

  • Rollover - ES的某个index满足一定的policy后,这个index的alias将自动重定向到一个新的index中去写入数据。

  • 这个特性简化了一定的业务操作,不需要业务侧每次都通过自己设定规则、跟踪创建index,管理index生命周期。
    显而易见,rollover这个特性很适合用来处理时序化数据;当你使用Filebeat、Logstash或者其他方式将带时间戳的文档索引到ES中时,通过使用索引别名、可以方便的定期滚动到新索引中去。

如何使用Rollover:

1. 使用PUT policy API创建一个lifecycle policy

所谓的lifecycle policy就是指定索引生命周期的各个阶段以及每个阶段要执行的操作。
生命周期可以有四个阶段:hot、warm、code、delete。
操作策略在Json中定义并通过PUT policy APT添加。

例如:下面的例子创建了一个名称为datastream_policy的policy,这个policy有两个阶段

  • hot阶段定义了一个rollover action,这个rollover policy指定了当index的大小超过50GB或者创建时间超过30天后自动滚动到下一个index
  • delete阶段指定了当老的index超过rollover时间90天后Delete掉,注意这个时间与rollover时间有关,而不是index的创建时间
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
    }
  }
}
'
  1. 监测任务进程

使用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
      }
    }
  }
}

一个比较完整的policy例子

  • 例子中:首先该index立即进入hot阶段,当数据超过7d或者50GB之后(指的是主分片)开始rollover;该index超过30天之后进入warm阶段,进入warm阶段之后开始执行forcemerge,shrink到一个shard,副本数改为2;60天后,move到标签属性为code的nodes;90天后delete掉。
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": {}
        }
      }
    }
  }
}
'

你可能感兴趣的:(Elasticsearch)