ilm-tutorial.asciidoc 14 KB

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