getting-started-ilm.asciidoc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[getting-started-index-lifecycle-management]]
  4. == Getting started with {ilm}
  5. Let's jump into {ilm} ({ilm-init}) by working through a hands-on scenario.
  6. This section will leverage many new concepts unique to {ilm-init} that
  7. you may not be familiar with. The following sections will explore
  8. these in more details.
  9. The goal of this example is to set up a set of indices that will encapsulate
  10. the data from a time series data source. We can imagine there is a system
  11. like {filebeat-ref}[Filebeat] that continuously indexes documents into
  12. our writing index. We wish to roll over the index after it reaches a size
  13. of 50 gigabytes, or has been created 30 days ago, and then delete the index
  14. after 90 days.
  15. [float]
  16. [[ilm-gs-create-policy]]
  17. === Setting up a policy
  18. There are many new features introduced by {ilm-init}, but we will only focus on
  19. a few that are needed for our example. For starters, we will use the
  20. <<ilm-put-lifecycle,Put Policy>> API to define our first policy. Lifecycle
  21. policies are defined in JSON and include specific
  22. <<ilm-policy-definition,phases and actions>>.
  23. [source,js]
  24. ------------------------
  25. PUT _ilm/policy/datastream_policy <1>
  26. {
  27. "policy": { <2>
  28. "phases": {
  29. "hot": { <3>
  30. "actions": {
  31. "rollover": { <4>
  32. "max_size": "50GB",
  33. "max_age": "30d"
  34. }
  35. }
  36. },
  37. "delete": {
  38. "min_age": "90d", <5>
  39. "actions": {
  40. "delete": {} <6>
  41. }
  42. }
  43. }
  44. }
  45. }
  46. ------------------------
  47. // CONSOLE
  48. // TEST
  49. <1> call to the <<ilm-put-lifecycle,put lifecycle API>> endpoint to create
  50. a new policy named "datastream_policy"
  51. <2> policy definition sub-object
  52. <3> the hot phase defined in the "phases" section. Optional `min_age` field
  53. not defined -- defaults to `0ms`
  54. <4> rollover action definition
  55. <5> delete phase begins after 90 days
  56. <6> delete action definition
  57. Here we created the policy called `datastream_policy` which rolls over
  58. the index being written to after it reaches 50 gigabytes, or it is 30
  59. days old. The rollover will occur when either of these conditions is true.
  60. The index will be deleted 90 days after it is rolled over.
  61. [float]
  62. [[ilm-gs-apply-policy]]
  63. === Applying a policy to our index
  64. There are <<set-up-lifecycle-policy,a few ways>> to associate a
  65. policy to an index. Since we wish specific settings to be applied to
  66. the new index created from Rollover, we will set the policy via
  67. index templates.
  68. [source,js]
  69. -----------------------
  70. PUT _template/datastream_template
  71. {
  72. "index_patterns": ["datastream-*"], <1>
  73. "settings": {
  74. "number_of_shards": 1,
  75. "number_of_replicas": 1,
  76. "index.lifecycle.name": "datastream_policy", <2>
  77. "index.lifecycle.rollover_alias": "datastream" <3>
  78. }
  79. }
  80. -----------------------
  81. // CONSOLE
  82. // TEST[continued]
  83. <1> match all indices starting with "datastream-". These will include all
  84. newly created indices from actions like rollover
  85. <2> the name of the lifecycle policy managing the index
  86. <3> alias to use for the rollover action, required since a rollover action is
  87. defined in the policy.
  88. The above index template introduces a few new settings specific to {ilm-init}.
  89. The first being `index.lifecycle.name`. This setting will configure
  90. the "datastream_policy" to the index applying this template. This means
  91. that all newly created indices prefixed "datastream-" will be managed by
  92. our policy. The other setting used here is `index.lifecycle.rollover_alias`.
  93. This setting is required when using a policy containing the rollover
  94. action and specifies which alias to rollover on behalf of this index.
  95. The intention here is that the rollover alias is also defined on the index.
  96. To begin, we will want to bootstrap our first index to write to.
  97. [source,js]
  98. -----------------------
  99. PUT datastream-000001
  100. {
  101. "aliases": {
  102. "datastream": {
  103. "is_write_index": true
  104. }
  105. }
  106. }
  107. -----------------------
  108. // CONSOLE
  109. // TEST[continued]
  110. When creating our index, we have to consider a few important configurations
  111. that tie our index and our policy together correctly. We need to make sure
  112. that our index name matches our index template pattern of "datastream-*",
  113. which it does. We are using the <<ilm-rollover-action, Rollover Action>> in our policy, which
  114. requires that our index name ends with a number. In our case, we used
  115. `000001`. This is important so that Rollover can increment this number when
  116. naming the new index created from rolling over.
  117. Our index creation request leverages its template to apply our settings,
  118. but we must also configure our rollover alias: "datastream". To do this,
  119. we take advantage of <<aliases-write-index,write indices>>. This is a way
  120. to define an alias to be used for both reading and writing, with only one
  121. index being the index that is being written to at a time. Rollover swaps
  122. the write index to be the new index created from rollover, and sets the
  123. alias to be read-only for the source index.
  124. [float]
  125. [[ilm-gs-check-progress]]
  126. === Checking progress
  127. Now that we have an index managed by our policy, how do we tell what is going
  128. on? Which phase are we in? Is something broken? This section will go over a
  129. few APIs and their responses to help us inspect our indices with respect
  130. to {ilm-init}.
  131. With the help of the <<ilm-explain-lifecycle,Explain API>>, we can know
  132. things like which phase we're in and when we entered that phase. The API
  133. will also provide further info if errors occurred, or if we are blocked on
  134. certain checks within actions.
  135. [source,js]
  136. --------------------------------------------------
  137. GET datastream-*/_ilm/explain
  138. --------------------------------------------------
  139. // CONSOLE
  140. // TEST[continued]
  141. The above request will retrieve {ilm-init} execution information for all our
  142. managed indices.
  143. [source,js]
  144. --------------------------------------------------
  145. {
  146. "indices": {
  147. "datastream-000001": {
  148. "index": "datastream-000001",
  149. "managed": true, <1>
  150. "policy": "datastream_policy", <2>
  151. "lifecycle_date_millis": 1538475653281,
  152. "phase": "hot", <3>
  153. "phase_time_millis": 1538475653317,
  154. "action": "rollover", <4>
  155. "action_time_millis": 1538475653317,
  156. "step": "attempt-rollover", <5>
  157. "step_time_millis": 1538475653317,
  158. "phase_execution": {
  159. "policy": "datastream_policy",
  160. "phase_definition": { <6>
  161. "min_age": "0ms",
  162. "actions": {
  163. "rollover": {
  164. "max_size": "50gb",
  165. "max_age": "30d"
  166. }
  167. }
  168. },
  169. "version": 1, <7>
  170. "modified_date_in_millis": 1539609701576
  171. }
  172. }
  173. }
  174. }
  175. --------------------------------------------------
  176. // CONSOLE
  177. // TESTRESPONSE[skip:no way to know if we will get this response immediately]
  178. <1> this index is managed by ILM
  179. <2> the policy in question, in this case, "datastream_policy"
  180. <3> what phase the index is currently in
  181. <4> what action the index is currently on
  182. <5> what step the index is currently on
  183. <6> the definition of the phase
  184. (in this case, the "hot" phase) that the index is currently on
  185. <7> the version of the policy being used to execute the current phase
  186. You can read about the full details of this response in the
  187. <<ilm-explain-lifecycle, explain API docs>>. For now, let's focus on how
  188. the response details which phase, action, and step we're in. We are in the
  189. "hot" phase, and "rollover" action. Rollover will continue to be called
  190. by {ilm-init} until its conditions are met and it rolls over the index.
  191. Afterwards, the original index will stay in the hot phase until 90 more
  192. days pass and it is deleted in the delete phase.
  193. As time goes on, new indices will be created and deleted.
  194. With `datastream-000002` being created when the index mets the rollover
  195. conditions and `datastream-000003` created after that. We will be able
  196. to search across all of our managed indices using the "datastream" alias,
  197. and we will be able to write to our to-be-rolled-over write indices using
  198. that same alias.
  199. That's it! We have our first use-case managed by {ilm-init}.
  200. To learn more about all our APIs,
  201. check out <<index-lifecycle-management-api,ILM APIs>>.