ilm-tutorial.asciidoc 15 KB

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