set-up-lifecycle-policy.asciidoc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[set-up-lifecycle-policy]]
  4. == Configure a lifecycle policy [[ilm-policy-definition]]
  5. For {ilm-init} to manage an index, a valid policy
  6. must be specified in the `index.lifecycle.name` index setting.
  7. To configure a lifecycle policy for <<index-rollover, rolling indices>>,
  8. you create the policy and add it to the <<index-templates, index template>>.
  9. To use a policy to manage an index that doesn't roll over,
  10. you can specify a lifecycle policy when you create the index,
  11. or apply a policy directly to an existing index.
  12. {ilm-init} policies are stored in the global cluster state and can be included in snapshots
  13. by setting `include_global_state` to `true` when you <<snapshots-take-snapshot, take the snapshot>>.
  14. When the snapshot is restored, all of the policies in the global state are restored and
  15. any local policies with the same names are overwritten.
  16. IMPORTANT: When you enable {ilm} for {beats} or the {ls} {es} output plugin,
  17. the necessary policies and configuration changes are applied automatically.
  18. You can modify the default policies, but you do not need to explicitly configure a policy or
  19. bootstrap an initial index.
  20. [discrete]
  21. [[ilm-create-policy]]
  22. === Create lifecycle policy
  23. To create a lifecycle policy from {kib}, open the menu and go to *Stack
  24. Management > Index Lifecycle Policies*. Click *Create policy*.
  25. [role="screenshot"]
  26. image:images/ilm/create-policy.png[Create policy page]
  27. You specify the lifecycle phases for the policy and the actions to perform in each phase.
  28. The <<ilm-put-lifecycle,create or update policy>> API is invoked to add the
  29. policy to the {es} cluster.
  30. .API example
  31. [%collapsible]
  32. ====
  33. [source,console]
  34. ------------------------
  35. PUT _ilm/policy/my_policy
  36. {
  37. "policy": {
  38. "phases": {
  39. "hot": {
  40. "actions": {
  41. "rollover": {
  42. "max_primary_shard_size": "25GB" <1>
  43. }
  44. }
  45. },
  46. "delete": {
  47. "min_age": "30d",
  48. "actions": {
  49. "delete": {} <2>
  50. }
  51. }
  52. }
  53. }
  54. }
  55. ------------------------
  56. <1> Roll over the index when it reaches 25GB in size
  57. <2> Delete the index 30 days after rollover
  58. ====
  59. [discrete]
  60. [[apply-policy-template]]
  61. === Apply lifecycle policy with an index template
  62. To use a policy that triggers the rollover action,
  63. you need to configure the policy in the index template used to create each new index.
  64. You specify the name of the policy and the alias used to reference the rolling indices.
  65. You can use the {kib} Create template wizard to create a template. To access the
  66. wizard, open the menu and go to *Stack Management > Index Management*. In the
  67. *Index Templates* tab, click *Create template*.
  68. [role="screenshot"]
  69. image:images/ilm/create-template-wizard-my_template.png[Create template page]
  70. The wizard invokes the <<indices-put-template,create or update index template
  71. API>> to add templates to a cluster.
  72. .API example
  73. [%collapsible]
  74. ====
  75. [source,console]
  76. -----------------------
  77. PUT _index_template/my_template
  78. {
  79. "index_patterns": ["test-*"], <1>
  80. "template": {
  81. "settings": {
  82. "number_of_shards": 1,
  83. "number_of_replicas": 1,
  84. "index.lifecycle.name": "my_policy", <2>
  85. "index.lifecycle.rollover_alias": "test-alias" <3>
  86. }
  87. }
  88. }
  89. -----------------------
  90. <1> Use this template for all new indices whose names begin with `test-`
  91. <2> Apply `my_policy` to new indices created with this template
  92. <3> Define an index alias for referencing indices managed by `my_policy`
  93. ====
  94. //////////////////////////
  95. [source,console]
  96. --------------------------------------------------
  97. DELETE _index_template/my_template
  98. --------------------------------------------------
  99. // TEST[continued]
  100. //////////////////////////
  101. [discrete]
  102. [[create-initial-index]]
  103. ==== Create an initial managed index
  104. When you set up policies for your own rolling indices, you need to manually create the first index
  105. managed by a policy and designate it as the write index.
  106. IMPORTANT: When you enable {ilm} for {beats} or the {ls} {es} output plugin,
  107. the necessary policies and configuration changes are applied automatically.
  108. You can modify the default policies, but you do not need to explicitly configure a policy or
  109. bootstrap an initial index.
  110. The name of the index must match the pattern defined in the index template and end with a number.
  111. This number is incremented to generate the name of indices created by the rollover action.
  112. For example, the following request creates the `test-00001` index.
  113. Because it matches the index pattern specified in `my_template`,
  114. {es} automatically applies the settings from that template.
  115. [source,console]
  116. -----------------------
  117. PUT test-000001
  118. {
  119. "aliases": {
  120. "test-alias":{
  121. "is_write_index": true <1>
  122. }
  123. }
  124. }
  125. -----------------------
  126. <1> Set this initial index to be the write index for this alias.
  127. Now you can start indexing data to the rollover alias specified in the lifecycle policy.
  128. With the sample `my_policy` policy, the rollover action is triggered once the initial
  129. index exceeds 25GB.
  130. {ilm-init} then creates a new index that becomes the write index for the `test-alias`.
  131. [discrete]
  132. [[apply-policy-manually]]
  133. === Apply lifecycle policy manually
  134. You can specify a policy when you create an index or
  135. apply a policy to an existing index through {kib} Management or
  136. the <<indices-update-settings, update settings API>>.
  137. When you apply a policy, {ilm-init} immediately starts managing the index.
  138. IMPORTANT: Do not manually apply a policy that uses the rollover action.
  139. Policies that use rollover must be applied by the <<apply-policy-template, index template>>.
  140. Otherwise, the policy is not carried forward when the rollover action creates a new index.
  141. The `index.lifecycle.name` setting specifies an index's policy.
  142. .API example
  143. [%collapsible]
  144. ====
  145. [source,console]
  146. -----------------------
  147. PUT test-index
  148. {
  149. "settings": {
  150. "number_of_shards": 1,
  151. "number_of_replicas": 1,
  152. "index.lifecycle.name": "my_policy" <1>
  153. }
  154. }
  155. -----------------------
  156. <1> Sets the lifecycle policy for the index.
  157. ====
  158. [discrete]
  159. [[apply-policy-multiple]]
  160. ==== Apply a policy to multiple indices
  161. You can apply the same policy to multiple indices by using wildcards in the index name
  162. when you call the <<indices-update-settings,update settings>> API.
  163. WARNING: Be careful that you don't inadvertently match indices that you don't want to modify.
  164. //////////////////////////
  165. [source,console]
  166. -----------------------
  167. PUT _index_template/mylogs_template
  168. {
  169. "index_patterns": [
  170. "mylogs-*"
  171. ],
  172. "template": {
  173. "settings": {
  174. "number_of_shards": 1,
  175. "number_of_replicas": 1
  176. },
  177. "mappings": {
  178. "properties": {
  179. "message": {
  180. "type": "text"
  181. },
  182. "@timestamp": {
  183. "type": "date"
  184. }
  185. }
  186. }
  187. }
  188. }
  189. -----------------------
  190. [source,console]
  191. -----------------------
  192. POST mylogs-pre-ilm-2019.06.24/_doc
  193. {
  194. "@timestamp": "2019-06-24T10:34:00",
  195. "message": "this is one log message"
  196. }
  197. -----------------------
  198. // TEST[continued]
  199. [source,console]
  200. -----------------------
  201. POST mylogs-pre-ilm-2019.06.25/_doc
  202. {
  203. "@timestamp": "2019-06-25T17:42:00",
  204. "message": "this is another log message"
  205. }
  206. -----------------------
  207. // TEST[continued]
  208. [source,console]
  209. --------------------------------------------------
  210. DELETE _index_template/mylogs_template
  211. --------------------------------------------------
  212. // TEST[continued]
  213. //////////////////////////
  214. [source,console]
  215. -----------------------
  216. PUT mylogs-pre-ilm*/_settings <1>
  217. {
  218. "index": {
  219. "lifecycle": {
  220. "name": "mylogs_policy_existing"
  221. }
  222. }
  223. }
  224. -----------------------
  225. // TEST[continued]
  226. <1> Updates all indices with names that start with `mylogs-pre-ilm`