ilm-tutorial.asciidoc 14 KB

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