Configure a lifecycle policy

For ILM to manage an index, a valid policy must be specified in the index.lifecycle.name index setting.

To configure a lifecycle policy for rolling indices, you create the policy and add it to the index template.

To use a policy to manage an index that doesn’t roll over, you can specify a lifecycle policy when you create the index, or apply a policy directly to an existing index.

ILM policies are stored in the global cluster state and can be included in snapshots by setting include_global_state to true when you take the snapshot. When the snapshot is restored, all of the policies in the global state are restored and any local policies with the same names are overwritten.

When you enable index lifecycle management for Beats or the Logstash Elasticsearch output plugin, the necessary policies and configuration changes are applied automatically. You can modify the default policies, but you do not need to explicitly configure a policy or bootstrap an initial index.

Create lifecycle policy

To create lifecycle policies through Kibana Management go to Management and click Index Lifecycle Policies.

create policy

You specify the lifecycle phases for the policy and the actions to perform in each phase.

The put policy API is invoked to add the policy to the Elasticsearch cluster.

API example

  1. PUT _ilm/policy/my_policy
  2. {
  3. "policy": {
  4. "phases": {
  5. "hot": {
  6. "actions": {
  7. "rollover": {
  8. "max_size": "25GB"
  9. }
  10. }
  11. },
  12. "delete": {
  13. "min_age": "30d",
  14. "actions": {
  15. "delete": {}
  16. }
  17. }
  18. }
  19. }
  20. }

Roll over the index when it reaches 25GB in size

Delete the index 30 days after rollover

Apply lifecycle policy with an index template

To use a policy that triggers the rollover action, you need to configure the policy in the index template used to create each new index. You specify the name of the policy and the alias used to reference the rolling indices.

You can use the Kibana Create template wizard to create a template. To access the wizard, open the menu and go to Stack Management > Index Management. In the the Index Templates tab, click Create template.

Create template page

The wizard invokes the put template API to add templates to a cluster.

API example

  1. PUT _index_template/my_template
  2. {
  3. "index_patterns": ["test-*"],
  4. "template": {
  5. "settings": {
  6. "number_of_shards": 1,
  7. "number_of_replicas": 1,
  8. "index.lifecycle.name": "my_policy",
  9. "index.lifecycle.rollover_alias": "test-alias"
  10. }
  11. }
  12. }

Use this template for all new indices whose names begin with test-

Apply my_policy to new indices created with this template

Define an index alias for referencing indices managed by my_policy

Create an initial managed index

When you set up policies for your own rolling indices, you need to manually create the first index managed by a policy and designate it as the write index.

When you enable index lifecycle management for Beats or the Logstash Elasticsearch output plugin, the necessary policies and configuration changes are applied automatically. You can modify the default policies, but you do not need to explicitly configure a policy or bootstrap an initial index.

The name of the index must match the pattern defined in the index template and end with a number. This number is incremented to generate the name of indices created by the rollover action.

For example, the following request creates the test-00001 index. Because it matches the index pattern specified in my_template, Elasticsearch automatically applies the settings from that template.

  1. PUT test-000001
  2. {
  3. "aliases": {
  4. "test-alias":{
  5. "is_write_index": true
  6. }
  7. }
  8. }

Set this initial index to be the write index for this alias.

Now you can start indexing data to the rollover alias specified in the lifecycle policy. With the sample my_policy policy, the rollover action is triggered once the initial index exceeds 25GB. ILM then creates a new index that becomes the write index for the test-alias.

Apply lifecycle policy manually

You can specify a policy when you create an index or apply a policy to an existing index through Kibana Management or the update settings API. When you apply a policy, ILM immediately starts managing the index.

Do not manually apply a policy that uses the rollover action. Policies that use rollover must be applied by the index template. Otherwise, the policy is not carried forward when the rollover action creates a new index.

The index.lifecycle.name setting specifies an index’s policy.

API example

  1. PUT test-index
  2. {
  3. "settings": {
  4. "number_of_shards": 1,
  5. "number_of_replicas": 1,
  6. "index.lifecycle.name": "my_policy"
  7. }
  8. }

Sets the lifecycle policy for the index.

Apply a policy to multiple indices

You can apply the same policy to multiple indices by using wildcards in the index name when you call the update settings API.

Be careful that you don’t inadvertently match indices that you don’t want to modify.

  1. PUT mylogs-pre-ilm*/_settings
  2. {
  3. "index": {
  4. "lifecycle": {
  5. "name": "mylogs_policy_existing"
  6. }
  7. }
  8. }

Updates all indices with names that start with mylogs-pre-ilm