ilm-tutorial.asciidoc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[getting-started-index-lifecycle-management]]
  4. == Tutorial: Automate rollover with {ilm-init}
  5. ++++
  6. <titleabbrev>Automate rollover</titleabbrev>
  7. ++++
  8. This tutorial demonstrates how to use {ilm}
  9. ({ilm-init}) to manage indices that contain time-series data.
  10. When you continuously index timestamped documents into {es},
  11. you typically use an index alias so you can periodically roll over to a new index.
  12. This enables you to implement a hot-warm-cold architecture to meet your performance
  13. requirements for your newest data, control costs over time, enforce retention policies,
  14. and still get the most out of your data.
  15. To automate rollover and management of time-series indices with {ilm-init}, you:
  16. . <<ilm-gs-create-policy, Create a lifecycle policy>> that defines the appropriate
  17. phases and actions.
  18. . <<ilm-gs-apply-policy, Create an index template>> to apply the policy to each new index.
  19. . <<ilm-gs-bootstrap, Bootstrap an index>> as the initial write index.
  20. . <<ilm-gs-check-progress, Verify indices are moving through the lifecycle phases>>
  21. as expected.
  22. For an introduction to rolling indices, see <<index-rollover>>.
  23. IMPORTANT: When you enable {ilm} for {beats} or the {ls} {es} output plugin,
  24. lifecycle policies are set up automatically.
  25. You do not need bootstrap the initial index or take any other actions.
  26. You can modify the default policies through
  27. {kibana-ref}/example-using-index-lifecycle-policy.html[{kib} Management]
  28. or the {ilm-init} APIs.
  29. [discrete]
  30. [[ilm-gs-create-policy]]
  31. === Create a lifecycle policy
  32. A lifecycle policy specifies the phases in the index lifecycle
  33. and the actions to perform in each phase. A lifecycle can have up to four phases:
  34. `hot`, `warm`, `cold`, and `delete`.
  35. You can define and manage policies through {kib} Management or with the
  36. <<ilm-put-lifecycle, put policy>> API.
  37. For example, you might define a `timeseries_policy` that has two phases:
  38. * A `hot` phase that defines a rollover action to specify that an index rolls over when it
  39. reaches either a `max_size` of 50 gigabytes or a `max_age` of 30 days.
  40. * A `delete` phase that sets `min_age` to remove the index 90 days after rollover.
  41. Note that this value is relative to the rollover time, not the index creation time.
  42. The underlying put policy request looks like this:
  43. [source,console]
  44. ------------------------
  45. PUT _ilm/policy/timeseries_policy
  46. {
  47. "policy": {
  48. "phases": {
  49. "hot": { <1>
  50. "actions": {
  51. "rollover": {
  52. "max_size": "50GB", <2>
  53. "max_age": "30d"
  54. }
  55. }
  56. },
  57. "delete": {
  58. "min_age": "90d", <3>
  59. "actions": {
  60. "delete": {} <4>
  61. }
  62. }
  63. }
  64. }
  65. }
  66. ------------------------
  67. <1> The `min_age` defaults to `0ms`, so new indices enter the `hot` phase immediately.
  68. <2> Trigger the `rollover` action when either of the conditions are met.
  69. <3> Move the index into the `delete` phase 90 days after rollover.
  70. <4> Trigger the `delete` action when the index enters the delete phase.
  71. You can also invoke this API directly to add lifecycle policies.
  72. For the complete list of actions that {ilm} can perform, see <<ilm-actions>>.
  73. [discrete]
  74. [[ilm-gs-apply-policy]]
  75. === Create an index template to apply the lifecycle policy
  76. To automatically apply a lifecycle policy to the new write index on rollover,
  77. specify the policy in the index template used to create new indices.
  78. For example, you might create a `timeseries_template` that is applied to new indices
  79. whose names match the `timeseries-*` index pattern.
  80. To enable automatic rollover, the template configures two {ilm-init} settings:
  81. * `index.lifecycle.name` specifies the name of the lifecycle policy to apply to new indices
  82. that match the index pattern.
  83. * `index.lifecycle.rollover_alias` specifies the index alias to be rolled over
  84. when the rollover action is triggered for an index.
  85. You can use the {kib} Create template wizard to add the template.
  86. This wizard invokes the put template API to create the template with the options you specify.
  87. The underlying request looks like this:
  88. [source,console]
  89. -----------------------
  90. PUT _template/timeseries_template
  91. {
  92. "index_patterns": ["timeseries-*"], <1>
  93. "settings": {
  94. "number_of_shards": 1,
  95. "number_of_replicas": 1,
  96. "index.lifecycle.name": "timeseries_policy", <2>
  97. "index.lifecycle.rollover_alias": "timeseries" <3>
  98. }
  99. }
  100. -----------------------
  101. // TEST[continued]
  102. <1> Apply the template to a new index if its name starts with `timeseries-`.
  103. <2> The name of the lifecycle policy to apply to each new index.
  104. <3> The name of the alias used to reference these indices.
  105. Required for policies that use the rollover action.
  106. You can also invoke this API directly to add templates.
  107. //////////////////////////
  108. [source,console]
  109. --------------------------------------------------
  110. DELETE /_template/timeseries_template
  111. --------------------------------------------------
  112. // TEST[continued]
  113. //////////////////////////
  114. [discrete]
  115. [[ilm-gs-bootstrap]]
  116. === Bootstrap the initial time-series index
  117. To get things started, you need to bootstrap an initial index and
  118. designate it as the write index for the rollover alias specified in your index template.
  119. The name of this index must match the template's index pattern and end with a number.
  120. On rollover, this value is incremented to generate a name for the new index.
  121. For example, the following request creates an index called `timeseries-000001`
  122. and makes it the write index for the `timeseries` alias.
  123. [source,console]
  124. -----------------------
  125. PUT timeseries-000001
  126. {
  127. "aliases": {
  128. "timeseries": {
  129. "is_write_index": true
  130. }
  131. }
  132. }
  133. -----------------------
  134. // TEST[continued]
  135. When the rollover conditions are met, the `rollover` action:
  136. * Creates a new index called `timeseries-000002`.
  137. This matches the `timeseries-*` pattern, so the settings from `timeseries_template` are applied to the new index.
  138. * Designates the new index as the write index and makes the bootstrap index read-only.
  139. This process repeats each time rollover conditions are met.
  140. You can search across all of the indices managed by the `timeseries_policy` with the `timeseries` alias.
  141. Write operations are routed to the current write index.
  142. [discrete]
  143. [[ilm-gs-check-progress]]
  144. === Check lifecycle progress
  145. To get status information for managed indices, you use the {ilm-init} explain API.
  146. This lets you find out things like:
  147. * What phase an index is in and when it entered that phase.
  148. * The current action and what step is being performed.
  149. * If any errors have occurred or progress is blocked.
  150. For example, the following request gets information about the `timeseries` indices:
  151. [source,console]
  152. --------------------------------------------------
  153. GET timeseries-*/_ilm/explain
  154. --------------------------------------------------
  155. // TEST[continued]
  156. The response below shows that the bootstrap index is waiting in the `hot` phase's `rollover` action.
  157. It remains in this state and {ilm-init} continues to call `attempt-rollover`
  158. until the rollover conditions are met.
  159. // [[36818c6d9f434d387819c30bd9addb14]]
  160. [source,console-result]
  161. --------------------------------------------------
  162. {
  163. "indices": {
  164. "timeseries-000001": {
  165. "index": "timeseries-000001",
  166. "managed": true,
  167. "policy": "timeseries_policy", <1>
  168. "lifecycle_date_millis": 1538475653281,
  169. "age": "30s", <2>
  170. "phase": "hot",
  171. "phase_time_millis": 1538475653317,
  172. "action": "rollover",
  173. "action_time_millis": 1538475653317,
  174. "step": "attempt-rollover", <3>
  175. "step_time_millis": 1538475653317,
  176. "phase_execution": {
  177. "policy": "timeseries_policy",
  178. "phase_definition": { <4>
  179. "min_age": "0ms",
  180. "actions": {
  181. "rollover": {
  182. "max_size": "50gb",
  183. "max_age": "30d"
  184. }
  185. }
  186. },
  187. "version": 1,
  188. "modified_date_in_millis": 1539609701576
  189. }
  190. }
  191. }
  192. }
  193. --------------------------------------------------
  194. // TESTRESPONSE[skip:no way to know if we will get this response immediately]
  195. <1> The policy used to manage the index
  196. <2> The age of the index
  197. <3> The step {ilm-init} is performing on the index
  198. <4> The definition of the current phase (the `hot` phase)