getting-started-ilm.asciidoc 8.3 KB

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