# 客製化並套用Index lifecycle policy 提供3種方式修改,各有優缺點。 ## 方法1 - 修改 component template (@custom) - 優點:之後新的index template都會套用component template,現有的也會,所屬的index可以建立新的並套用 - 缺點:要先一個一個找出來要改哪個 1. 先到Kibana -> Management -> Data -> Index Management -> Component Templates 設定Index設定值的範本,需要找結尾是`@custom`的 ![](https://i.imgur.com/mYmlJKa.png) 2. 編輯 ![](https://i.imgur.com/ivQ5oJm.png) 3. 在這裡加入自訂的lifecycle policy ```json { "index": { "lifecycle": { "name": "your-lifecycle-name" } } } ``` ![](https://i.imgur.com/6gWyViT.png) 4. 直接點 `Review`,預覽修改的內容,然後點`Save component template` ![](https://i.imgur.com/qufnqvE.png) 5. 後面會跳出是否要套用和rollover現有的資料(建新的index,而且會套用新的policy),我選擇是,因為新的index就是新的policy,之後就可以直接刪了舊資料,省得一個一個再改 ![](https://i.imgur.com/YRm1rFm.png) 6. 修改結果,可以把第一筆`00001`的刪掉了 ![](https://i.imgur.com/77qsKKZ.png) ![](https://i.imgur.com/JRPn7bH.png) ## 方法2 - 偷懶,修改現有index template的lifecycle policy - 優點:可以快速改,有介面可看 - 缺點:index不會套用新的policy,還是要自己一個一個改。想用custom的component template的話依舊是舊版的lifecycle policy 1. logs為預設的policy,logs-mymac是我自己的 ![](https://i.imgur.com/Whaqz38.png) 2. 點一下目標policy最右邊的`+`,輸入要搬移的index template,然後`Add policy` ![](https://i.imgur.com/YP7okpC.png) 3. 修改結果:只有index template有改到而已,相對應的index還是舊的,之後還要自己改現有index的policy ![](https://i.imgur.com/3YIiV1X.png) 4. 修改index的policy,然後滑上面一點按`save` ``` "index.lifecycle.name": "your-lifecycle-name" ``` ![](https://i.imgur.com/zCT7DOd.png) ## 方法3 - API 簡單暴力,建議先理解各項的取名規則,直接全部修改。需要注意可能會改到你不想要改的資料。 可以到`Dev Tools`裡面直接執行。 ### 命名規則 - component template: 名稱@custom - index template: component tempalte的名稱-* - index: .ds-index template的完整名稱-* 備註:`*`通常是space的名稱 ### Component Template (@custom) ``` # update component template's index lifecycle PUT _component_template/${indexName}@custom { "template": { "settings": { "index": { "lifecycle": { "name": "${lifecycleName}" } } } } } ``` ### Index Template 這個不需要執行,修改了component template之後,index template會自動套用。 ``` # update index template's index lifecycle PUT _index_template/${indexName} { "index_patterns": [ "${indexName}-*" ] "template": { "settings": { "index": { "lifecycle": { "name": "${lifecycleName}" } } } } } ``` ### Index ``` # update index's lifecycle PUT .ds-${indexName}-${spaceName}*/_settings { "index": { "lifecycle": { "name": "${lifecycleName}" } } } ``` ![](https://i.imgur.com/JK3QPl8.png) ## 要修改的資料 ### 目前套用的policy - logs - metrics - traces-apm ### Data Streams使用到的index template和component template #### Policy: logs - logs-elastic_agent - logs-elastic_agent@custom - logs-elastic_agent.apm_server - logs-elastic_agent.apm_server@custom - logs-elastic_agent.filebeat - logs-elastic_agent.filebeat@custom - logs-elastic_agent.fleet_server - logs-elastic_agent.fleet_server@custom - logs-elastic_agent.metricbeat - logs-elastic_agent.metricbeat@custom #### Policy: metrics - metrics-elastic_agent.apm_server - metrics-elastic_agent.apm_server@custom - metrics-elastic_agent.elastic_agent - metrics-elastic_agent.elastic_agent@custom - metrics-elastic_agent.filebeat - metrics-elastic_agent.filebeat@custom - metrics-elastic_agent.fleet_server - metrics-elastic_agent.fleet_server@custom - metrics-elastic_agent.metricbeat - metrics-elastic_agent.metricbeat@custom - metrics-system.cpu - metrics-system.cpu@custom - metrics-system.diskio - metrics-system.diskio@custom - metrics-system.fsstat - metrics-system.fsstat@custom - metrics-system.load - metrics-system.load@custom - metrics-system.memory - metrics-system.memory@custom - metrics-system.network - metrics-system.network@custom - metrics-system.process - metrics-system.process@custom - metrics-system.process.summary - system.process.summary@custom - metrics-system.socket_summary - system.socket_summary@custom - metrics-system.uptime - metrics-system.uptime@custom #### Policy: traces-apm - traces-apm - traces-apm@custom ## 參考 - https://www.elastic.co/guide/en/elasticsearch/reference/current/set-up-lifecycle-policy.html#apply-policy-multiple - https://www.elastic.co/guide/en/apm/guide/8.6/ilm-how-to.html ###### tags: `Elasticsearch`