ilm-tutorial.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 five phases:
  40. `hot`, `warm`, `cold`, `frozen`, 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_primary_shard_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,create or update policy>> API.
  48. To create the policy from {kib}, open the menu and go to *Stack Management >
  49. Index Lifecycle Policies*. Click *Create policy*.
  50. [role="screenshot"]
  51. image::images/ilm/create-policy.png[Create policy page]
  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_primary_shard_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,create or update index template
  98. API>> to create 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 <<index-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
  131. first generation backing index called `.ds-timeseries-2099.03.08-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
  143. `.ds-timeseries-2099.03.08-000002`. Because it is a backing index of the
  144. `timeseries` data stream, the configuration from the `timeseries_template` index
  145. template is applied to the new index.
  146. * As it is the latest generation index of the `timeseries` data stream, the
  147. newly created backing index `.ds-timeseries-2099.03.08-000002` becomes the data
  148. stream's write index.
  149. This process repeats each time a rollover condition is met.
  150. You can search across all of the data stream's backing indices, managed by the `timeseries_policy`,
  151. with the `timeseries` data stream name.
  152. Write operations are routed to the current write index. Read operations will be handled by all
  153. backing indices.
  154. [discrete]
  155. [[ilm-gs-check-progress]]
  156. === Check lifecycle progress
  157. To get status information for managed indices, you use the {ilm-init} explain API.
  158. This lets you find out things like:
  159. * What phase an index is in and when it entered that phase.
  160. * The current action and what step is being performed.
  161. * If any errors have occurred or progress is blocked.
  162. For example, the following request gets information about the `timeseries` data stream's
  163. backing indices:
  164. [source,console]
  165. --------------------------------------------------
  166. GET .ds-timeseries-*/_ilm/explain
  167. --------------------------------------------------
  168. // TEST[continued]
  169. The following response shows the data stream's first generation backing index is waiting for the `hot`
  170. phase's `rollover` action.
  171. It remains in this state and {ilm-init} continues to call `check-rollover-ready` until a rollover condition
  172. is met.
  173. // [[36818c6d9f434d387819c30bd9addb14]]
  174. [source,console-result]
  175. --------------------------------------------------
  176. {
  177. "indices": {
  178. ".ds-timeseries-2099.03.07-000001": {
  179. "index": ".ds-timeseries-2099.03.07-000001",
  180. "managed": true,
  181. "policy": "timeseries_policy", <1>
  182. "lifecycle_date_millis": 1538475653281,
  183. "age": "30s", <2>
  184. "phase": "hot",
  185. "phase_time_millis": 1538475653317,
  186. "action": "rollover",
  187. "action_time_millis": 1538475653317,
  188. "step": "check-rollover-ready", <3>
  189. "step_time_millis": 1538475653317,
  190. "phase_execution": {
  191. "policy": "timeseries_policy",
  192. "phase_definition": { <4>
  193. "min_age": "0ms",
  194. "actions": {
  195. "rollover": {
  196. "max_primary_shard_size": "50gb",
  197. "max_age": "30d"
  198. }
  199. }
  200. },
  201. "version": 1,
  202. "modified_date_in_millis": 1539609701576
  203. }
  204. }
  205. }
  206. }
  207. --------------------------------------------------
  208. // TESTRESPONSE[skip:no way to know if we will get this response immediately]
  209. <1> The policy used to manage the index
  210. <2> The age of the index
  211. <3> The step {ilm-init} is performing on the index
  212. <4> The definition of the current phase (the `hot` phase)
  213. //////////////////////////
  214. [source,console]
  215. --------------------------------------------------
  216. DELETE /_data_stream/timeseries
  217. --------------------------------------------------
  218. // TEST[continued]
  219. //////////////////////////
  220. //////////////////////////
  221. [source,console]
  222. --------------------------------------------------
  223. DELETE /_index_template/timeseries_template
  224. --------------------------------------------------
  225. // TEST[continued]
  226. //////////////////////////
  227. [discrete]
  228. [[manage-time-series-data-without-data-streams]]
  229. === Manage time series data without data streams
  230. Even though <<data-streams, data streams>> are a convenient way to scale
  231. and manage time series data, they are designed to be append-only. We recognise there
  232. might be use-cases where data needs to be updated or deleted in place and the
  233. data streams don't support delete and update requests directly,
  234. so the index APIs would need to be used directly on the data stream's backing indices.
  235. In these cases, you can use an index alias to manage indices containing the time series data
  236. and periodically roll over to a new index.
  237. To automate rollover and management of time series indices with {ilm-init} using an index
  238. alias, you:
  239. . Create a lifecycle policy that defines the appropriate phases and actions.
  240. See <<ilm-gs-create-policy, Create a lifecycle policy>> above.
  241. . <<ilm-gs-alias-apply-policy, Create an index template>> to apply the policy to each new index.
  242. . <<ilm-gs-alias-bootstrap, Bootstrap an index>> as the initial write index.
  243. . <<ilm-gs-alias-check-progress, Verify indices are moving through the lifecycle phases>>
  244. as expected.
  245. [discrete]
  246. [[ilm-gs-alias-apply-policy]]
  247. === Create an index template to apply the lifecycle policy
  248. To automatically apply a lifecycle policy to the new write index on rollover,
  249. specify the policy in the index template used to create new indices.
  250. For example, you might create a `timeseries_template` that is applied to new indices
  251. whose names match the `timeseries-*` index pattern.
  252. To enable automatic rollover, the template configures two {ilm-init} settings:
  253. * `index.lifecycle.name` specifies the name of the lifecycle policy to apply to new indices
  254. that match the index pattern.
  255. * `index.lifecycle.rollover_alias` specifies the index alias to be rolled over
  256. when the rollover action is triggered for an index.
  257. You can use the {kib} Create template wizard to add the template. To access the
  258. wizard, open the menu and go to *Stack Management > Index Management*. In the
  259. *Index Templates* tab, click *Create template*.
  260. [role="screenshot"]
  261. image:images/ilm/create-template-wizard.png[Create template page]
  262. The create template request for the example template looks like this:
  263. [source,console]
  264. -----------------------
  265. PUT _index_template/timeseries_template
  266. {
  267. "index_patterns": ["timeseries-*"], <1>
  268. "template": {
  269. "settings": {
  270. "number_of_shards": 1,
  271. "number_of_replicas": 1,
  272. "index.lifecycle.name": "timeseries_policy", <2>
  273. "index.lifecycle.rollover_alias": "timeseries" <3>
  274. }
  275. }
  276. }
  277. -----------------------
  278. // TEST[continued]
  279. <1> Apply the template to a new index if its name starts with `timeseries-`.
  280. <2> The name of the lifecycle policy to apply to each new index.
  281. <3> The name of the alias used to reference these indices.
  282. Required for policies that use the rollover action.
  283. //////////////////////////
  284. [source,console]
  285. --------------------------------------------------
  286. DELETE _index_template/timeseries_template
  287. --------------------------------------------------
  288. // TEST[continued]
  289. //////////////////////////
  290. [discrete]
  291. [[ilm-gs-alias-bootstrap]]
  292. === Bootstrap the initial time series index with a write index alias
  293. To get things started, you need to bootstrap an initial index and
  294. designate it as the write index for the rollover alias specified in your index template.
  295. The name of this index must match the template's index pattern and end with a number.
  296. On rollover, this value is incremented to generate a name for the new index.
  297. For example, the following request creates an index called `timeseries-000001`
  298. and makes it the write index for the `timeseries` alias.
  299. [source,console]
  300. -----------------------
  301. PUT timeseries-000001
  302. {
  303. "aliases": {
  304. "timeseries": {
  305. "is_write_index": true
  306. }
  307. }
  308. }
  309. -----------------------
  310. // TEST[continued]
  311. When the rollover conditions are met, the `rollover` action:
  312. * Creates a new index called `timeseries-000002`.
  313. This matches the `timeseries-*` pattern, so the settings from `timeseries_template` are applied to the new index.
  314. * Designates the new index as the write index and makes the bootstrap index read-only.
  315. This process repeats each time rollover conditions are met.
  316. You can search across all of the indices managed by the `timeseries_policy` with the `timeseries` alias.
  317. Write operations are routed to the current write index.
  318. [discrete]
  319. [[ilm-gs-alias-check-progress]]
  320. === Check lifecycle progress
  321. Retrieving the status information for managed indices is very similar to the data stream case.
  322. See the data stream <<ilm-gs-check-progress, check progress section>> for more information.
  323. The only difference is the indices namespace, so retrieving the progress will entail the following
  324. api call:
  325. [source,console]
  326. --------------------------------------------------
  327. GET timeseries-*/_ilm/explain
  328. --------------------------------------------------
  329. // TEST[continued]
  330. //////////////////////////
  331. [source,console]
  332. --------------------------------------------------
  333. DELETE /timeseries-000001
  334. --------------------------------------------------
  335. // TEST[continued]
  336. //////////////////////////