es的索引生命周期管理
一、常見(jiàn)概念及命令
1.1、概念
ILM定義了四個(gè)生命周期階段:
Hot
:正在積極地更新和查詢索引。Warm
:不再更新索引,但仍在查詢。cold
:不再更新索引,很少查詢。信息仍然需要可搜索,但是如果這些查詢速度較慢也可以。Delete
:不再需要該索引,可以安全地將其刪除
rollover
: rollover可以基于大小,文檔數(shù)或使用期限創(chuàng)建新的索引
去保存數(shù)據(jù)
1.2、DSL命令
索引的生命周期常用命令:
GET _ilm/status # 查看狀態(tài)
POST _ilm/start # 啟動(dòng)
POST _ilm/stop # 停止POST <index>/_ilm/remove
# 從index中移除策略
查看索引的生命周期狀態(tài):(
重點(diǎn)
)GET <index-name>/_ilm/explain
例如:GET /my_cas_history_logs/_ilm/explain
GET _ilm/policy
#查看全局策略
查看索引的策略應(yīng)用情況:(重點(diǎn)
)GET _iml/policy/<policy-name>
例如:GET _iml/policy/cas_login_policy
刪除策略DELETE _ilm/policy/<policy_id>
# 刪除指定策略
例如:DELETE _ilm/policy/pcsp_log_record_policy
# 刪除指定策略
模板
查看模板詳情:(重點(diǎn)
)GET _template/<template-name>
例如:GET _template/pcsp_log_record_template
刪除模板DELETE _template/<template-name>
l例如:DELETE _template/pcsp_log_record_template
在實(shí)驗(yàn)中可以修改設(shè)置,來(lái)縮短ILM檢測(cè)時(shí)間間隔。ILM定期運(yùn)行(indices.lifecycle.poll_interval),默認(rèn)是10分鐘,檢查索引是否符合策略標(biāo)準(zhǔn),并執(zhí)行所需的任何步驟。實(shí)際開(kāi)發(fā)中不修改此項(xiàng)。
PUT /_cluster/settings
{
"transient": {
"indices.lifecycle.poll_interval": "1m"
}
}
二、生命周期的管理步驟
第一步:創(chuàng)建生周期 policy。
#為索引創(chuàng)建ilm規(guī)則(可以根據(jù)不同場(chǎng)景設(shè)置不同規(guī)則)
#設(shè)置ilm規(guī)則,主分片沒(méi)50GB或者超過(guò)30天或數(shù)據(jù)條數(shù)超過(guò)500000000切換一次索引(三者可任意保留無(wú)需都留下),超過(guò)360天的索引自動(dòng)刪除
#max_size:主分片數(shù)*50GB
#common_policy 是自己取的策略名字,自己根據(jù)自己合適的取,這里只設(shè)置了hot和delete。根據(jù)自己業(yè)務(wù)需求決定是否設(shè)置warm、cold
PUT _ilm/policy/common_policy {
"policy":{
"phases":{
"hot":{
"min_age":"0ms",
"actions":{
"rollover":{ #滾動(dòng)創(chuàng)建新索引的觸發(fā)條件
"max_size":"50gb", # 當(dāng)容量超過(guò)50gb(根據(jù)自己的需求設(shè)置)
"max_docs": 500000000, # 當(dāng)總條數(shù)超過(guò)500000000(根據(jù)自己的需求設(shè)置)
"max_age":"30d" # 當(dāng)時(shí)間超過(guò)30d(根據(jù)自己的需求設(shè)置)
},
"set_priority":{ #優(yōu)先級(jí),任一滿足條件就執(zhí)行
"priority":100
}
}
},
"delete":{#刪除策略
"min_age":"360d", #超過(guò)360天的數(shù)據(jù)就自動(dòng)刪除
"actions":{
"delete":{}
}
}
}
}
}
實(shí)戰(zhàn)演練:
PUT _ilm/policy/pcsp_log_record_policy
{
"policy":{
"phases":{
"hot":{
"min_age":"0ms",
"actions":{
"rollover":{
"max_size":"50gb",
"max_age":"2m",
"max_docs":5
},
"set_priority":{
"priority":100
}
}
},
"delete":{
"min_age":"5m",
"actions":{
"delete":{}
}
}
}
}
}
第二步:創(chuàng)建索引模板,模板中關(guān)聯(lián) policy 和別名。
PUT _template/<template_name>
{
"order":0,
"index_patterns":["<index_name>-*"], #index_name是自己的索引別名
"settings":{
"index.lifecycle.name":"common_policy", #這里的common_policy就是上面設(shè)置的策略
"index.lifecycle.rollover_alias":"<index_name>", #指定索引的rollover別名
"index.number_of_replicas":"1", # 設(shè)置副本1
"index.number_of_shards":"6", # 設(shè)置主分片數(shù)為6(建議分片數(shù)設(shè)置為數(shù)據(jù)節(jié)點(diǎn)的倍數(shù)個(gè))
"index.refresh_interval":"30s",
"index.translog.durability":"async",
"index.translog.sync_interval":"10s",
"index.unassigned.node_left.delayed_timeout":"30m"
},
"mappings":{},
"aliases":{}
}
實(shí)戰(zhàn)演練:
PUT _template/pcsp_log_record_template
{
"order":0,
"index_patterns":["pcsp_log_record-*"],
"settings":{
"index.lifecycle.name":"pcsp_log_record_policy",
"index.lifecycle.rollover_alias":"pcsp_log_record",
"index.number_of_replicas":"1",
"index.number_of_shards":"1",
"index.refresh_interval":"30s",
"index.translog.durability":"async",
"index.translog.sync_interval":"10s",
"index.unassigned.node_left.delayed_timeout":"30m"
},
"mappings": {
"properties": {
"address": {
"type": "keyword"
},
"age": {
"type": "long"
},
"name": {
"type": "text",
"analyzer": "ik_max_word"
}
}
},
"aliases":{}
}
第三步:創(chuàng)建符合模板的起始索引,設(shè)置別名(即我們統(tǒng)一對(duì)外提供服務(wù)的索引名)。
#創(chuàng)建<index_name>索引,支持rollover的時(shí)候index名稱附加年月日時(shí)分秒(將url及json里面的<index_name>替換為對(duì)應(yīng)索引名)
PUT /%3C<index_name>-%7Bnow%2Fm%7Byyyy.MM.dd.HH.mm%7CAsia%2FShanghai%7D%7D-000001%3E
{
"aliases":{
"<index_name>":{
"is_write_index":true
}
}
}
例如:(我測(cè)試的例子)
PUT /%3Cpcsp_log_record-%7Bnow%2Fm%7Byyyy.MM.dd.HH.mm%7CAsia%2FShanghai%7D%7D-000001%3E
{
"aliases":{
"pcsp_log_record":{
"is_write_index":true
}
}
}
按以上設(shè)置完成后就可以查看
測(cè)試插入數(shù)據(jù):
POST /pcsp_log_record/_doc
{
"name": "王者榮耀1",
"age": 19,
"address": "長(zhǎng)沙岳麓山"
}
GET /my_cas_history_logs/_search
以上就完成了一個(gè)簡(jiǎn)單的es 的生命周期的管理
三、生命周期的管理的測(cè)試
在實(shí)際開(kāi)發(fā)中,測(cè)試?yán)蠋焼?wèn)如何測(cè)試es的生命周期生效了呢?總不能等一年才看到刪除效果吧?
cas_policy_logs 是我的索引名字(別名),以此為例:
#查看索引的生命周期,可以看到當(dāng)前的索引的策略應(yīng)用情況GET /cas_policy_logs/_ilm/explain
刪除策略
刪除索引
DELETE pcsp_history-3.09.11.17.24-0004
刪除策略
DELET _ilm/policy/pcsp_history_policy
刪除模板
DELET _template/pcsp_history
注意:如果用這個(gè)方法沒(méi)有刪掉,可以改用kibana圖形化工具的操作刪除的(要先刪除所有索引才能刪除策略
)
設(shè)置es的ILM檢測(cè)時(shí)間間隔,測(cè)試完記得改回去,默認(rèn)10分鐘。這僅僅是為了增加es監(jiān)測(cè)頻率。
PUT /_cluster/settings
{
"transient": {
"indices.lifecycle.poll_interval": "30s"
}
}
重點(diǎn):
這里delete指定的是當(dāng)老的索引index超過(guò)rollver的時(shí)間,后的delete指定時(shí)間才被刪掉。而不是索引的創(chuàng)建時(shí)間。
如果是索引按指定時(shí)間rollver,那刪除時(shí)間就是rollver中的max_age+delete指定時(shí)間,才被刪除。而索引按容量或者文檔數(shù)量(max_size、max_docs)那么就小于
max_age+delete指定時(shí)間 就被刪除。
官網(wǎng)對(duì)于刪除的定義是:Delete the index 30d days after rollover.
(30d是這里設(shè)置的刪除時(shí)間)
總結(jié)來(lái)說(shuō)就是:當(dāng)這個(gè)索引不再寫入數(shù)據(jù)(即創(chuàng)建新索引)開(kāi)始算在delete設(shè)定時(shí)間后刪除
查看生命周期的重要命令:
查看索引的生命周期狀態(tài):(
重點(diǎn)
)GET <index-name>/_ilm/explain
例如:
GET /my_cas_history_logs/_ilm/explain
查看索引的策略應(yīng)用情況:(重點(diǎn)
)GET _iml/policy/<policy-name>
例如:GET _iml/policy/cas_login_policy
查看模板詳情:(重點(diǎn)
)GET _template/<template-name>
例如:GET /cas_log/_ilm/explain文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-816206.html
刪完之后重新建策略即可,設(shè)置調(diào)試,這樣就完成了測(cè)試。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-816206.html
到了這里,關(guān)于elasticsearch 7.9.3知識(shí)歸納整理(五)之 es的索引生命周期管理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!