ilm-with-existing-indices.asciidoc 13 KB

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