set-up-lifecycle-policy.asciidoc 7.3 KB

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