set-up-lifecycle-policy.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. [role="xpack"]
  2. [[set-up-lifecycle-policy]]
  3. == Configure a lifecycle policy [[ilm-policy-definition]]
  4. For {ilm-init} to manage an index, a valid policy
  5. must be specified in the `index.lifecycle.name` index setting.
  6. To configure a lifecycle policy for <<index-rollover, rolling indices>>,
  7. you create the policy and add it to the <<index-templates, index template>>.
  8. To use a policy to manage an index that doesn't roll over,
  9. you can specify a lifecycle policy when you create the index,
  10. or apply a policy directly to an existing index.
  11. {ilm-init} policies are stored in the global cluster state and can be included in snapshots
  12. by setting `include_global_state` to `true` when you <<snapshots-take-snapshot, take the snapshot>>.
  13. When the snapshot is restored, all of the policies in the global state are restored and
  14. any local policies with the same names are overwritten.
  15. IMPORTANT: When you enable {ilm} for {beats} or the {ls} {es} output plugin,
  16. the necessary policies and configuration changes are applied automatically.
  17. You can modify the default policies, but you do not need to explicitly configure a policy or
  18. bootstrap an initial index.
  19. [discrete]
  20. [[ilm-create-policy]]
  21. === Create lifecycle policy
  22. To create a lifecycle policy from {kib}, open the menu and go to *Stack
  23. Management > Index Lifecycle Policies*. Click *Create policy*.
  24. [role="screenshot"]
  25. image:images/ilm/create-policy.png[Create policy page]
  26. You specify the lifecycle phases for the policy and the actions to perform in each phase.
  27. The <<ilm-put-lifecycle,create or update policy>> API is invoked to add the
  28. 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_primary_shard_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. You can use the {kib} Create template wizard to create a template. To access the
  65. wizard, open the menu and go to *Stack Management > Index Management*. In the
  66. *Index Templates* tab, click *Create template*.
  67. [role="screenshot"]
  68. image:images/ilm/create-template-wizard-my_template.png[Create template page]
  69. The wizard invokes the <<indices-put-template,create or update index template
  70. API>> to add templates to a cluster.
  71. .API example
  72. [%collapsible]
  73. ====
  74. [source,console]
  75. -----------------------
  76. PUT _index_template/my_template
  77. {
  78. "index_patterns": ["test-*"], <1>
  79. "template": {
  80. "settings": {
  81. "number_of_shards": 1,
  82. "number_of_replicas": 1,
  83. "index.lifecycle.name": "my_policy", <2>
  84. "index.lifecycle.rollover_alias": "test-alias" <3>
  85. }
  86. }
  87. }
  88. -----------------------
  89. <1> Use this template for all new indices whose names begin with `test-`
  90. <2> Apply `my_policy` to new indices created with this template
  91. <3> Define an index alias for referencing indices managed by `my_policy`
  92. ====
  93. //////////////////////////
  94. [source,console]
  95. --------------------------------------------------
  96. DELETE _index_template/my_template
  97. --------------------------------------------------
  98. // TEST[continued]
  99. //////////////////////////
  100. [discrete]
  101. [[create-initial-index]]
  102. ==== Create an initial managed index
  103. When you set up policies for your own rolling indices, you need to manually create the first index
  104. managed by a policy and designate it as the write index.
  105. IMPORTANT: When you enable {ilm} for {beats} or the {ls} {es} output plugin,
  106. the necessary policies and configuration changes are applied automatically.
  107. You can modify the default policies, but you do not need to explicitly configure a policy or
  108. bootstrap an initial index.
  109. The name of the index must match the pattern defined in the index template and end with a number.
  110. This number is incremented to generate the name of indices created by the rollover action.
  111. For example, the following request creates the `test-00001` index.
  112. Because it matches the index pattern specified in `my_template`,
  113. {es} automatically applies the settings from that template.
  114. [source,console]
  115. -----------------------
  116. PUT test-000001
  117. {
  118. "aliases": {
  119. "test-alias":{
  120. "is_write_index": true <1>
  121. }
  122. }
  123. }
  124. -----------------------
  125. <1> Set this initial index to be the write index for this alias.
  126. Now you can start indexing data to the rollover alias specified in the lifecycle policy.
  127. With the sample `my_policy` policy, the rollover action is triggered once the initial
  128. index exceeds 25GB.
  129. {ilm-init} then creates a new index that becomes the write index for the `test-alias`.
  130. [discrete]
  131. [[apply-policy-manually]]
  132. === Apply lifecycle policy manually
  133. You can specify a policy when you create an index or
  134. apply a policy to an existing index through {kib} Management or
  135. the <<indices-update-settings, update settings API>>.
  136. When you apply a policy, {ilm-init} immediately starts managing the index.
  137. IMPORTANT: Do not manually apply a policy that uses the rollover action.
  138. Policies that use rollover must be applied by the <<apply-policy-template, index template>>.
  139. Otherwise, the policy is not carried forward when the rollover action creates a new index.
  140. The `index.lifecycle.name` setting specifies an index's policy.
  141. .API example
  142. [%collapsible]
  143. ====
  144. [source,console]
  145. -----------------------
  146. PUT test-index
  147. {
  148. "settings": {
  149. "number_of_shards": 1,
  150. "number_of_replicas": 1,
  151. "index.lifecycle.name": "my_policy" <1>
  152. }
  153. }
  154. -----------------------
  155. <1> Sets the lifecycle policy for the index.
  156. ====
  157. [discrete]
  158. [[apply-policy-multiple]]
  159. ==== Apply a policy to multiple indices
  160. You can apply the same policy to multiple indices by using wildcards in the index name
  161. when you call the <<indices-update-settings,update settings>> API.
  162. WARNING: Be careful that you don't inadvertently match indices that you don't want to modify.
  163. //////////////////////////
  164. [source,console]
  165. -----------------------
  166. PUT _index_template/mylogs_template
  167. {
  168. "index_patterns": [
  169. "mylogs-*"
  170. ],
  171. "template": {
  172. "settings": {
  173. "number_of_shards": 1,
  174. "number_of_replicas": 1
  175. },
  176. "mappings": {
  177. "properties": {
  178. "message": {
  179. "type": "text"
  180. },
  181. "@timestamp": {
  182. "type": "date"
  183. }
  184. }
  185. }
  186. }
  187. }
  188. -----------------------
  189. [source,console]
  190. -----------------------
  191. POST mylogs-pre-ilm-2019.06.24/_doc
  192. {
  193. "@timestamp": "2019-06-24T10:34:00",
  194. "message": "this is one log message"
  195. }
  196. -----------------------
  197. // TEST[continued]
  198. [source,console]
  199. -----------------------
  200. POST mylogs-pre-ilm-2019.06.25/_doc
  201. {
  202. "@timestamp": "2019-06-25T17:42:00",
  203. "message": "this is another log message"
  204. }
  205. -----------------------
  206. // TEST[continued]
  207. [source,console]
  208. --------------------------------------------------
  209. DELETE _index_template/mylogs_template
  210. --------------------------------------------------
  211. // TEST[continued]
  212. //////////////////////////
  213. [source,console]
  214. -----------------------
  215. PUT mylogs-pre-ilm*/_settings <1>
  216. {
  217. "index": {
  218. "lifecycle": {
  219. "name": "mylogs_policy_existing"
  220. }
  221. }
  222. }
  223. -----------------------
  224. // TEST[continued]
  225. <1> Updates all indices with names that start with `mylogs-pre-ilm`
  226. [discrete]
  227. [[switch-lifecycle-policies]]
  228. ==== Switch lifecycle policies
  229. To switch an index's lifecycle policy, follow these steps:
  230. . Remove the existing policy using the <<ilm-remove-policy,remove policy API>>.
  231. Target a data stream or alias to remove the policies of all its indices.
  232. +
  233. [source,console]
  234. ----
  235. POST logs-my_app-default/_ilm/remove
  236. ----
  237. // TEST[continued]
  238. // TEST[s/^/PUT _data_stream\/logs-my_app-default\n/]
  239. . The remove policy API removes all {ilm-init} metadata from the index and
  240. doesn't consider the index's lifecycle status. This can leave indices in an
  241. undesired state.
  242. +
  243. --
  244. For example, the <<ilm-forcemerge,`forcemerge`>> action temporarily closes an
  245. index before reopening it. Removing an index's {ilm-init} policy during a
  246. `forcemerge` can leave the index closed indefinitely.
  247. After policy removal, use the <<indices-get-index,get index API>> to check an
  248. index's state . Target a data stream or alias to get the state of all its
  249. indices.
  250. [source,console]
  251. ----
  252. GET logs-my_app-default
  253. ----
  254. // TEST[continued]
  255. You can then change the index as needed. For example, you can re-open any
  256. closed indices using the <<indices-open-close,open index API>>.
  257. [source,console]
  258. ----
  259. POST logs-my_app-default/_open
  260. ----
  261. // TEST[continued]
  262. --
  263. . Assign a new policy using the <<indices-update-settings,update settings API>>.
  264. Target a data stream or alias to assign a policy to all its indices.
  265. +
  266. --
  267. WARNING: Don't assign a new policy without first removing the existing policy.
  268. This can cause <<ilm-phase-execution,phase execution>> to silently fail.
  269. [source,console]
  270. ----
  271. PUT logs-my_app-default/_settings
  272. {
  273. "index": {
  274. "lifecycle": {
  275. "name": "new-lifecycle-policy"
  276. }
  277. }
  278. }
  279. ----
  280. // TEST[continued]
  281. // TEST[s/new-lifecycle-policy/mylogs_policy_existing/]
  282. --