ilm-tutorial.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. When you continuously index timestamped documents into {es},
  9. you typically use a <<data-streams, data stream>> so you can periodically roll over to a
  10. new index.
  11. This enables you to implement a hot-warm-cold architecture to meet your performance
  12. requirements for your newest data, control costs over time, enforce retention policies,
  13. and still get the most out of your data.
  14. TIP: Data streams are best suited for
  15. <<data-streams-append-only,append-only>> use cases. If you need to frequently
  16. update or delete existing documents across multiple indices, we recommend
  17. using an index alias and index template instead. You can still use ILM to
  18. manage and rollover the alias's indices. Skip to
  19. <<manage-time-series-data-without-data-streams>>.
  20. To automate rollover and management of a data stream with {ilm-init}, you:
  21. . <<ilm-gs-create-policy, Create a lifecycle policy>> that defines the appropriate
  22. phases and actions.
  23. . <<ilm-gs-apply-policy, Create an index template>> to create the data stream and
  24. apply the ILM policy and the indices settings and mappings configurations for the backing
  25. indices.
  26. . <<ilm-gs-check-progress, Verify indices are moving through the lifecycle phases>>
  27. as expected.
  28. For an introduction to rolling indices, see <<index-rollover>>.
  29. IMPORTANT: When you enable {ilm} for {beats} or the {ls} {es} output plugin,
  30. lifecycle policies are set up automatically.
  31. You do not need to take any other actions.
  32. You can modify the default policies through
  33. {kibana-ref}/example-using-index-lifecycle-policy.html[{kib} Management]
  34. or the {ilm-init} APIs.
  35. [discrete]
  36. [[ilm-gs-create-policy]]
  37. === Create a lifecycle policy
  38. A lifecycle policy specifies the phases in the index lifecycle
  39. and the actions to perform in each phase. A lifecycle can have up to four phases:
  40. `hot`, `warm`, `cold`, and `delete`.
  41. For example, you might define a `timeseries_policy` that has two phases:
  42. * A `hot` phase that defines a rollover action to specify that an index rolls over when it
  43. reaches either a `max_size` of 50 gigabytes or a `max_age` of 30 days.
  44. * A `delete` phase that sets `min_age` to remove the index 90 days after rollover.
  45. Note that this value is relative to the rollover time, not the index creation time.
  46. You can create the policy through {kib} Management or with the
  47. <<ilm-put-lifecycle, put policy>> API.
  48. To create the policy from {kib}, go to Management and click **Index Lifecycle Policies**.
  49. [role="screenshot"]
  50. image:images/ilm/create-policy.png[]
  51. .API example
  52. [%collapsible]
  53. ====
  54. [source,console]
  55. ------------------------
  56. PUT _ilm/policy/timeseries_policy
  57. {
  58. "policy": {
  59. "phases": {
  60. "hot": { <1>
  61. "actions": {
  62. "rollover": {
  63. "max_size": "50GB", <2>
  64. "max_age": "30d"
  65. }
  66. }
  67. },
  68. "delete": {
  69. "min_age": "90d", <3>
  70. "actions": {
  71. "delete": {} <4>
  72. }
  73. }
  74. }
  75. }
  76. }
  77. ------------------------
  78. <1> The `min_age` defaults to `0ms`, so new indices enter the `hot` phase immediately.
  79. <2> Trigger the `rollover` action when either of the conditions are met.
  80. <3> Move the index into the `delete` phase 90 days after rollover.
  81. <4> Trigger the `delete` action when the index enters the delete phase.
  82. ====
  83. [discrete]
  84. [[ilm-gs-apply-policy]]
  85. === Create an index template to create the data stream and apply the lifecycle policy
  86. To set up a data stream, first create an index template to specify the lifecycle policy. Because
  87. the template is for a data stream, it must also include a `data_stream` definition.
  88. For example, you might create a `timeseries_template` to use for a future data stream
  89. named `timeseries`.
  90. To enable the {ilm-init} to manage the data stream, the template configures one {ilm-init} setting:
  91. * `index.lifecycle.name` specifies the name of the lifecycle policy to apply to the data stream.
  92. You can use the {kib} Create template wizard to add the template.
  93. This wizard invokes the put _index_template API to create the <<indices-templates,index template>>
  94. with the options you specify.
  95. .API example
  96. [%collapsible]
  97. ====
  98. [source,console]
  99. -----------------------
  100. PUT _index_template/timeseries_template
  101. {
  102. "index_patterns": ["timeseries"], <1>
  103. "data_stream": { },
  104. "template": {
  105. "settings": {
  106. "number_of_shards": 1,
  107. "number_of_replicas": 1,
  108. "index.lifecycle.name": "timeseries_policy" <2>
  109. }
  110. }
  111. }
  112. -----------------------
  113. // TEST[continued]
  114. <1> Apply the template when a document is indexed into the `timeseries` target.
  115. <2> The name of the {ilm-init} policy used to manage the data stream.
  116. ====
  117. [discrete]
  118. [[ilm-gs-create-the-data-stream]]
  119. === Create the data stream
  120. To get things started, index a document into the name or wildcard pattern defined
  121. in the `index_patterns` of the <<indices-templates,index template>>. As long
  122. as an existing data stream, index, or index alias does not already use the name, the index
  123. request automatically creates a corresponding data stream with a single backing index.
  124. {es} automatically indexes the request's documents into this backing index, which also
  125. acts as the stream's <<data-stream-write-index,write index>>.
  126. For example, the following request creates the `timeseries` data stream and the first generation
  127. backing index called `.ds-timeseries-000001`.
  128. [source,console]
  129. -----------------------
  130. POST timeseries/_doc
  131. {
  132. "message": "logged the request",
  133. "@timestamp": "1591890611"
  134. }
  135. -----------------------
  136. // TEST[continued]
  137. When a rollover condition in the lifecycle policy is met, the `rollover` action:
  138. * Creates the second generation backing index, named `.ds-timeseries-000002`.
  139. Because it is a backing index of the `timeseries` data stream, the configuration from the `timeseries_template` index template is applied to the new index.
  140. * As it is the latest generation index of the `timeseries` data stream, the newly created
  141. backing index `.ds-timeseries-000002` becomes the data stream's write index.
  142. This process repeats each time a rollover condition is met.
  143. You can search across all of the data stream's backing indices, managed by the `timeseries_policy`,
  144. with the `timeseries` data stream name.
  145. Write operations are routed to the current write index. Read operations will be handled by all
  146. backing indices.
  147. [discrete]
  148. [[ilm-gs-check-progress]]
  149. === Check lifecycle progress
  150. To get status information for managed indices, you use the {ilm-init} explain API.
  151. This lets you find out things like:
  152. * What phase an index is in and when it entered that phase.
  153. * The current action and what step is being performed.
  154. * If any errors have occurred or progress is blocked.
  155. For example, the following request gets information about the `timeseries` data stream's
  156. backing indices:
  157. [source,console]
  158. --------------------------------------------------
  159. GET .ds-timeseries-*/_ilm/explain
  160. --------------------------------------------------
  161. // TEST[continued]
  162. The following response shows the data stream's first generation backing index is waiting for the `hot`
  163. phase's `rollover` action.
  164. It remains in this state and {ilm-init} continues to call `check-rollover-ready` until a rollover condition
  165. is met.
  166. // [[36818c6d9f434d387819c30bd9addb14]]
  167. [source,console-result]
  168. --------------------------------------------------
  169. {
  170. "indices": {
  171. ".ds-timeseries-000001": {
  172. "index": ".ds-timeseries-000001",
  173. "managed": true,
  174. "policy": "timeseries_policy", <1>
  175. "lifecycle_date_millis": 1538475653281,
  176. "age": "30s", <2>
  177. "phase": "hot",
  178. "phase_time_millis": 1538475653317,
  179. "action": "rollover",
  180. "action_time_millis": 1538475653317,
  181. "step": "check-rollover-ready", <3>
  182. "step_time_millis": 1538475653317,
  183. "phase_execution": {
  184. "policy": "timeseries_policy",
  185. "phase_definition": { <4>
  186. "min_age": "0ms",
  187. "actions": {
  188. "rollover": {
  189. "max_size": "50gb",
  190. "max_age": "30d"
  191. }
  192. }
  193. },
  194. "version": 1,
  195. "modified_date_in_millis": 1539609701576
  196. }
  197. }
  198. }
  199. }
  200. --------------------------------------------------
  201. // TESTRESPONSE[skip:no way to know if we will get this response immediately]
  202. <1> The policy used to manage the index
  203. <2> The age of the index
  204. <3> The step {ilm-init} is performing on the index
  205. <4> The definition of the current phase (the `hot` phase)
  206. //////////////////////////
  207. [source,console]
  208. --------------------------------------------------
  209. DELETE /_data_stream/timeseries
  210. --------------------------------------------------
  211. // TEST[continued]
  212. //////////////////////////
  213. //////////////////////////
  214. [source,console]
  215. --------------------------------------------------
  216. DELETE /_index_template/timeseries_template
  217. --------------------------------------------------
  218. // TEST[continued]
  219. //////////////////////////
  220. [discrete]
  221. [[manage-time-series-data-without-data-streams]]
  222. === Manage time-series data without data streams
  223. Even though <<data-streams, data streams>> are a convenient way to scale
  224. and manage time-series data, they are designed to be append-only. We recognise there
  225. might be use-cases where data needs to be updated or deleted in place and the
  226. data streams don't support delete and update requests directly,
  227. so the index APIs would need to be used directly on the data stream's backing indices.
  228. In these cases, you can use an index alias to manage indices containing the time-series data
  229. and periodically roll over to a new index.
  230. To automate rollover and management of time-series indices with {ilm-init} using an index
  231. alias, you:
  232. . Create a lifecycle policy that defines the appropriate phases and actions.
  233. See <<ilm-gs-create-policy, Create a lifecycle policy>> above.
  234. . <<ilm-gs-alias-apply-policy, Create an index template>> to apply the policy to each new index.
  235. . <<ilm-gs-alias-bootstrap, Bootstrap an index>> as the initial write index.
  236. . <<ilm-gs-alias-check-progress, Verify indices are moving through the lifecycle phases>>
  237. as expected.
  238. [discrete]
  239. [[ilm-gs-alias-apply-policy]]
  240. === Create a legacy index template to apply the lifecycle policy
  241. To automatically apply a lifecycle policy to the new write index on rollover,
  242. specify the policy in the index template used to create new indices.
  243. For example, you might create a `timeseries_template` that is applied to new indices
  244. whose names match the `timeseries-*` index pattern.
  245. To enable automatic rollover, the template configures two {ilm-init} settings:
  246. * `index.lifecycle.name` specifies the name of the lifecycle policy to apply to new indices
  247. that match the index pattern.
  248. * `index.lifecycle.rollover_alias` specifies the index alias to be rolled over
  249. when the rollover action is triggered for an index.
  250. You can use the {kib} Create template wizard to add the template.
  251. To access the wizard, go to Management, click **Index Management**,
  252. and select the **Index Templates** view.
  253. [role="screenshot"]
  254. image:images/ilm/create-template-wizard.png[]
  255. The create template request for the example template looks like this:
  256. [source,console]
  257. -----------------------
  258. PUT _template/timeseries_template
  259. {
  260. "index_patterns": ["timeseries-*"], <1>
  261. "settings": {
  262. "number_of_shards": 1,
  263. "number_of_replicas": 1,
  264. "index.lifecycle.name": "timeseries_policy", <2>
  265. "index.lifecycle.rollover_alias": "timeseries" <3>
  266. }
  267. }
  268. -----------------------
  269. // TEST[continued]
  270. <1> Apply the template to a new index if its name starts with `timeseries-`.
  271. <2> The name of the lifecycle policy to apply to each new index.
  272. <3> The name of the alias used to reference these indices.
  273. Required for policies that use the rollover action.
  274. //////////////////////////
  275. [source,console]
  276. --------------------------------------------------
  277. DELETE /_template/timeseries_template
  278. --------------------------------------------------
  279. // TEST[continued]
  280. //////////////////////////
  281. [discrete]
  282. [[ilm-gs-alias-bootstrap]]
  283. === Bootstrap the initial time-series index with a write index alias
  284. To get things started, you need to bootstrap an initial index and
  285. designate it as the write index for the rollover alias specified in your index template.
  286. The name of this index must match the template's index pattern and end with a number.
  287. On rollover, this value is incremented to generate a name for the new index.
  288. For example, the following request creates an index called `timeseries-000001`
  289. and makes it the write index for the `timeseries` alias.
  290. [source,console]
  291. -----------------------
  292. PUT timeseries-000001
  293. {
  294. "aliases": {
  295. "timeseries": {
  296. "is_write_index": true
  297. }
  298. }
  299. }
  300. -----------------------
  301. // TEST[continued]
  302. When the rollover conditions are met, the `rollover` action:
  303. * Creates a new index called `timeseries-000002`.
  304. This matches the `timeseries-*` pattern, so the settings from `timeseries_template` are applied to the new index.
  305. * Designates the new index as the write index and makes the bootstrap index read-only.
  306. This process repeats each time rollover conditions are met.
  307. You can search across all of the indices managed by the `timeseries_policy` with the `timeseries` alias.
  308. Write operations are routed to the current write index.
  309. [discrete]
  310. [[ilm-gs-alias-check-progress]]
  311. === Check lifecycle progress
  312. Retrieving the status information for managed indices is very similar to the data stream case.
  313. See the data stream <<ilm-gs-check-progress, check progress section>> for more information.
  314. The only difference is the indices namespace, so retrieving the progress will entail the following
  315. api call:
  316. [source,console]
  317. --------------------------------------------------
  318. GET timeseries-*/_ilm/explain
  319. --------------------------------------------------
  320. // TEST[continued]
  321. //////////////////////////
  322. [source,console]
  323. --------------------------------------------------
  324. DELETE /timeseries-000001
  325. --------------------------------------------------
  326. // TEST[continued]
  327. //////////////////////////