ilm-with-existing-indices.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[ilm-with-existing-indices]]
  4. == Using {ilm-init} with existing indices
  5. While it is recommended to use {ilm-init} to manage the index lifecycle from
  6. start to finish, it may be useful to use {ilm-init} with existing indices,
  7. particularly when transitioning from an alternative method of managing the index
  8. lifecycle such as Curator, or when migrating from daily indices to
  9. rollover-based indices. Such use cases are fully supported, but there are some
  10. configuration differences from when {ilm-init} can manage the complete index
  11. lifecycle.
  12. This section describes strategies to leverage {ilm-init} for existing periodic
  13. indices when migrating to fully {ilm-init}-manged indices, which can be done in
  14. a few different ways, each providing different tradeoffs. As an example, we'll
  15. walk through a use case of a very simple logging index with just a field for the
  16. log message and a timestamp.
  17. First, we need to create a template for these indices:
  18. [source,js]
  19. -----------------------
  20. PUT _template/mylogs_template
  21. {
  22. "index_patterns": [
  23. "mylogs-*"
  24. ],
  25. "settings": {
  26. "number_of_shards": 1,
  27. "number_of_replicas": 1
  28. },
  29. "mappings": {
  30. "properties": {
  31. "message": {
  32. "type": "text"
  33. },
  34. "@timestamp": {
  35. "type": "date"
  36. }
  37. }
  38. }
  39. }
  40. -----------------------
  41. // CONSOLE
  42. // TEST
  43. And we'll ingest a few documents to create a few daily indices:
  44. [source,js]
  45. -----------------------
  46. POST mylogs-pre-ilm-2019.06.24/_doc
  47. {
  48. "@timestamp": "2019-06-24T10:34:00",
  49. "message": "this is one log message"
  50. }
  51. -----------------------
  52. // CONSOLE
  53. // TEST[continued]
  54. [source,js]
  55. -----------------------
  56. POST mylogs-pre-ilm-2019.06.25/_doc
  57. {
  58. "@timestamp": "2019-06-25T17:42:00",
  59. "message": "this is another log message"
  60. }
  61. -----------------------
  62. // CONSOLE
  63. // TEST[continued]
  64. Now that we have these indices, we'll look at a few different ways of migrating
  65. these indices to ILM.
  66. [[ilm-with-existing-periodic-indices]]
  67. === Managing existing periodic indices with {ilm-init}
  68. NOTE: The examples in this section assume daily indices as set up in
  69. <<ilm-with-existing-indices,the previous section>>.
  70. The simplest way to manage existing indices while transitioning to fully
  71. {ilm-init}-managed indices is to allow all new indices to be fully managed by
  72. {ilm-init} before attaching {ilm-init} policies to existing indices. To do this,
  73. all new documents should be directed to {ilm-init}-managed indices - if you are
  74. using Beats or Logstash data shippers, upgrading all of those shippers to
  75. version 7.0.0 or higher will take care of that part for you. If you are not
  76. using Beats or Logstash, you may need to set up ILM for new indices yourself as
  77. demonstrated in the <<getting-started-index-lifecycle-management,getting started
  78. guide>>.
  79. NOTE: If you are using Beats through Logstash, you may need to change your
  80. Logstash output configuration and invoke the Beats setup to use ILM for new
  81. data.
  82. Once all new documents are being written to fully {ilm-init}-managed indices, it
  83. is easy to add an {ilm-init} policy to existing indices. However, there are two
  84. things to keep in mind when doing this, and a trick that makes those two things
  85. much easier to handle.
  86. The two biggest things to keep in mind are:
  87. 1. Existing periodic indices shouldn't use policies with rollover, because
  88. rollover is used to manage where new data goes. Since existing indices should no
  89. longer be receiving new documents, there is no point to using rollover for them.
  90. 2. {ilm-init} policies attached to existing indices will compare the `min_age`
  91. for each phase to the original creation date of the index, and so might proceed
  92. through multiple phases immediately.
  93. The first one is the most important, because it makes it difficult to use the
  94. same policy for new and existing periodic indices. But that's easy to solve
  95. with one simple trick: Create a second policy for existing indices, in addition
  96. to the one for new indices. {ilm-init} policies are cheap to create, so don't be
  97. afraid to have more than one. Modifying a policy designed for new indices to be
  98. used on existing indices is generally very simple: just remove the `rollover`
  99. action.
  100. For example, if you created a policy for your new indices with each phase
  101. like so:
  102. [source,js]
  103. -----------------------
  104. PUT _ilm/policy/mylogs_policy
  105. {
  106. "policy": {
  107. "phases": {
  108. "hot": {
  109. "actions": {
  110. "rollover": {
  111. "max_size": "25GB"
  112. }
  113. }
  114. },
  115. "warm": {
  116. "min_age": "1d",
  117. "actions": {
  118. "forcemerge": {
  119. "max_num_segments": 1
  120. }
  121. }
  122. },
  123. "cold": {
  124. "min_age": "7d",
  125. "actions": {
  126. "freeze": {}
  127. }
  128. },
  129. "delete": {
  130. "min_age": "30d",
  131. "actions": {
  132. "delete": {}
  133. }
  134. }
  135. }
  136. }
  137. }
  138. -----------------------
  139. // CONSOLE
  140. // TEST[continued]
  141. You can create a policy for pre-existing indices by removing the `rollover`
  142. action, and in this case, the `hot` phase is now empty so we can remove that
  143. too:
  144. [source,js]
  145. -----------------------
  146. PUT _ilm/policy/mylogs_policy_existing
  147. {
  148. "policy": {
  149. "phases": {
  150. "warm": {
  151. "min_age": "1d",
  152. "actions": {
  153. "forcemerge": {
  154. "max_num_segments": 1
  155. }
  156. }
  157. },
  158. "cold": {
  159. "min_age": "7d",
  160. "actions": {
  161. "freeze": {}
  162. }
  163. },
  164. "delete": {
  165. "min_age": "30d",
  166. "actions": {
  167. "delete": {}
  168. }
  169. }
  170. }
  171. }
  172. }
  173. -----------------------
  174. // CONSOLE
  175. // TEST[continued]
  176. Creating a separate policy for existing indices will also allow using different
  177. `min_age` values. You may want to use higher values to prevent many indices from
  178. running through the policy at once, which may be important if your policy
  179. includes potentially resource-intensive operations like force merge.
  180. You can configure the lifecycle for many indices at once by using wildcards in
  181. the index name when calling the <<indices-update-settings,Update Settings API>>
  182. to set the policy name, but be careful that you don't include any indices that
  183. you don't want to change the policy for:
  184. [source,js]
  185. -----------------------
  186. PUT mylogs-pre-ilm*/_settings <1>
  187. {
  188. "index": {
  189. "lifecycle": {
  190. "name": "mylogs_policy_existing"
  191. }
  192. }
  193. }
  194. -----------------------
  195. // CONSOLE
  196. // TEST[continued]
  197. <1> This pattern will match all indices with names that start with
  198. `mylogs-pre-ilm`
  199. Once all pre-{ilm-init} indices have aged out and been deleted, the policy for
  200. older periodic indices can be deleted.
  201. [[ilm-reindexing-into-rollover]]
  202. === Reindexing via {ilm-init}
  203. NOTE: The examples in this section assume daily indices as set up in
  204. <<ilm-with-existing-indices,the previous section>>.
  205. In some cases, it may be useful to reindex data into {ilm-init}-managed indices.
  206. This is more complex than simply attaching policies to existing indices as
  207. described in <<ilm-with-existing-periodic-indices,the previous section>>, and
  208. requires pausing indexing during the reindexing process. However, this technique
  209. may be useful in cases where periodic indices were created with very small
  210. amounts of data leading to excessive shard counts, or for indices which grow
  211. steadily over time, but have not been broken up into time-series indices leading
  212. to shards which are much too large, situations that cause significant
  213. performance problems.
  214. Before getting started with reindexing data, the new index structure should be
  215. set up. For this section, we'll be using the same setup described in
  216. <<ilm-with-existing-indices,{ilm-imit} with existing indices>>.
  217. First, we'll set up a policy with rollover, and can include any additional
  218. phases required. For simplicity, we'll just use rollover:
  219. [source,js]
  220. -----------------------
  221. PUT _ilm/policy/sample_policy
  222. {
  223. "policy": {
  224. "phases": {
  225. "hot": {
  226. "actions": {
  227. "rollover": {
  228. "max_age": "7d",
  229. "max_size": "50G"
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. -----------------------
  237. // CONSOLE
  238. // TEST[continued]
  239. And now we'll update the index template for our indices to include the relevant
  240. {ilm-init} settings:
  241. [source,js]
  242. -----------------------
  243. PUT _template/mylogs_template
  244. {
  245. "index_patterns": [
  246. "ilm-mylogs-*" <1>
  247. ],
  248. "settings": {
  249. "number_of_shards": 1,
  250. "number_of_replicas": 1,
  251. "index": {
  252. "lifecycle": {
  253. "name": "mylogs_condensed_policy", <2>
  254. "rollover_alias": "mylogs" <3>
  255. }
  256. }
  257. },
  258. "mappings": {
  259. "properties": {
  260. "message": {
  261. "type": "text"
  262. },
  263. "@timestamp": {
  264. "type": "date"
  265. }
  266. }
  267. }
  268. }
  269. -----------------------
  270. // CONSOLE
  271. // TEST[continued]
  272. <1> The new index pattern has a prefix compared to the old one, this will
  273. make it easier to reindex later
  274. <2> The name of the policy we defined above
  275. <3> The name of the alias we'll use to write to and query
  276. And create the first index with the alias specified in the `rollover_alias`
  277. setting in the index template:
  278. [source,js]
  279. -----------------------
  280. PUT ilm-mylogs-000001
  281. {
  282. "aliases": {
  283. "mylogs": {
  284. "is_write_index": true
  285. }
  286. }
  287. }
  288. -----------------------
  289. // CONSOLE
  290. // TEST[continued]
  291. All new documents should be indexed via the `mylogs` alias at this point. Adding
  292. new data to the old indices during the reindexing process can cause data to be
  293. added to the old indices, but not be reindexed into the new indices.
  294. NOTE: If you do not want to mix new data and old data in the new ILM-managed
  295. indices, indexing of new data should be paused entirely while the reindex
  296. completes. Mixing old and new data within one index is safe, but keep in mind
  297. that the indices with mixed data should be retained in their entirety until you
  298. are ready to delete both the old and new data.
  299. By default, {ilm-init} only checks rollover conditions every 10 minutes. Under
  300. normal indexing load, this usually works well, but during reindexing, indices
  301. can grow very, very quickly. We'll need to set the poll interval to something
  302. shorter to ensure that the new indices don't grow too large while waiting for
  303. the rollover check:
  304. [source,js]
  305. -----------------------
  306. PUT _cluster/settings
  307. {
  308. "transient": {
  309. "indices.lifecycle.poll_interval": "1m" <1>
  310. }
  311. }
  312. -----------------------
  313. // CONSOLE
  314. // TEST[skip:don't want to overwrite this setting for other tests]
  315. <1> This tells ILM to check for rollover conditions every minute
  316. We're now ready to reindex our data using the <<docs-reindex,reindex API>>. If
  317. you have a timestamp or date field in your documents, as in this example, it may
  318. be useful to specify that the documents should be sorted by that field - this
  319. will mean that all documents in `ilm-mylogs-000001` come before all documents in
  320. `ilm-mylogs-000002`, and so on. However, if this is not a requirement, omitting
  321. the sort will allow the data to be reindexed more quickly.
  322. IMPORTANT: If your data uses document IDs generated by means other than
  323. Elasticsearch's automatic ID generation, you may need to do additional
  324. processing to ensure that the document IDs don't conflict during the reindex, as
  325. documents will retain their original IDs. One way to do this is to use a
  326. <<reindex-scripts,script>> in the reindex call to append the original index name
  327. to the document ID.
  328. [source,js]
  329. -----------------------
  330. POST _reindex
  331. {
  332. "source": {
  333. "index": "mylogs-*", <1>
  334. "sort": { "@timestamp": "desc" }
  335. },
  336. "dest": {
  337. "index": "mylogs", <2>
  338. "op_type": "create" <3>
  339. }
  340. }
  341. -----------------------
  342. // CONSOLE
  343. // TEST[continued]
  344. <1> This index pattern matches our existing indices. Using the prefix for
  345. the new indices makes using this index pattern much easier.
  346. <2> The alias set up above
  347. <3> This option will cause the reindex to abort if it encounters multiple
  348. documents with the same ID. This is optional, but recommended to prevent
  349. accidentally overwriting documents if two documents from different indices
  350. have the same ID.
  351. Once this completes, indexing new data can be resumed, as long as all new
  352. documents are indexed into the alias used above. All data, existing and new, can
  353. be queried using that alias as well. We should also be sure to set the
  354. {ilm-init} poll interval back to its default value, because keeping it set too
  355. low can cause unnecessary load on the current master node:
  356. [source,js]
  357. -----------------------
  358. PUT _cluster/settings
  359. {
  360. "transient": {
  361. "indices.lifecycle.poll_interval": null
  362. }
  363. }
  364. -----------------------
  365. // CONSOLE
  366. // TEST[skip:don't want to overwrite this setting for other tests]
  367. All of the reindexed data should now be accessible via the alias set up above,
  368. in this case `mylogs`. Once you have verified that all the data has been
  369. reindexed and is available in the new indices, the existing indices can be
  370. safely removed.