天天看點

ES7中,冷熱分離和索引生命周期的簡單實踐

個人學習筆記,謝絕轉載!!!

索引生命周期管理的本質--RollOver

當現有索引被認為太大或太舊時,滾動索引API将别名滾動到新索引。

該API接受一個别名和一個條件清單。别名必須隻指向一個索引。如果索引滿足指定條件,則建立一個新索引,并将别名切換到指向新索引的位置。

ES索引生命周期管理分為4個階段:hot、warm、cold、delete,其中hot主要負責對索引進行rollover操作,warm、cold、delete分别對rollover後的資料進一步處理

  • hot: 主要處理時序資料的實時寫入,該階段可以根據索引的文檔數、大小、時長決定是否調用rollover API來滾動索引
  • warm: 可以用來查詢,但是不再寫入
  • cold: 索引不再有更新操作,并且查詢也會很少
  • delete: 資料将被删除

注意:上述四個階段不是必須同時存在(除hot階段,其他為非必須)

一個簡單的需求:将實時寫入的索引寫入到高性能但容量較小的節點,将不更新的曆史索引轉移到大容量性能不高的節點,并删除一段時間之前的索引。

配置兩個節點(hot/warn)的ES叢集

在配置檔案中給ES節點打标簽,用于區分冷熱節點:

node.attr.node_type: hot    //熱節點
node.attr.node_type: warn   //冷節點
      

效果:

GET /_cat/nodeattrs?v&h=node,attr,value
node       attr              value
es-node-01 node_type         hot
es-node-02 node_type         warn
......
      

配置索引模闆

建立message-logs-*索引模闆,并設定索引落到 hot 節點

PUT/_template/template_message_logs {
"index_patterns": ["message-logs-*"],
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0,
"routing.allocation.require.node_type": "hot"
  }
}
      

配置定時任務

## 每天1點将前一天的日志轉移到warn節點
00 01 * * * curl -XPUT -H 'Content-Type: application/json' "http://127.0.0.1:9200/message-logs-$(date +\%Y\%m\%d -d'1 day ago')/_settings?pretty" -d '{"index.routing.allocation.require.node_type": "warn"}' >/dev/null 2>&1

## 每天2點将7天前的日志删除
00 02 * * * curl -XDELETE "http://127.0.0.1:9200/message-logs-$(date +\%Y\%m\%d -d'7 day ago')" >/dev/null 2>&1
      

這樣message-logs-yyyymmdd索引就實作了冷熱分離。當天的索引寫入hot節點,定時任務将前一天的是以轉移到warn節點,超過7天的自動删除。

看看效果:

ES7中,冷熱分離和索引生命周期的簡單實踐

使用ilm政策管理索引生命周期

定義httpd日志的冷熱政策

10min進行rollover,之後1h後的索引進入warm隻讀階段,3h後的索引删除

PUT /_ilm/policy/policy_httpd_logs {
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_age": "10ms"
                }
            }
        },
"warm": {
"min_age": "1h",
"actions": {
"allocate": {
"require": {
"node_type": "warm"
                    }
                },
"readonly": {}
            }
        },
"delete": {
"min_age": "3h",
"actions": {
"delete": {}
            }
        }
    }
  }
}
      

配置索引模闆,将政策應用到模闆

PUT /_template/template_httpd_logs {
"index_patterns": ["httpd-logs-*"],
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0,
"index.lifecycle.name": "policy_httpd_logs",           
"index.lifecycle.rollover_alias": "httpd-logs-alias",  
"routing.allocation.require.node_type": "hot"          
  }
}
      
  • index.lifecycle.name, 使用該模版建立的索引統一用“policy_httpd_logs”政策
  • index.lifecycle.rollover_alias, 使用該模版建立的索引統一用“httpd-logs-alias”的别名進行管理
  • index.routing.allocation.require.{attribute}, 使用該模版建立的索引統一配置設定在hot節點

手動建立第一個索引,并設定别名

索引建立的名稱應該是以 “-00001”等可自增長的字段結尾,否則政策不生效

PUT/httpd-logs-000001 {
"settings" : {
"number_of_shards":3,
"number_of_replicas":0
  },
"aliases": { 
"httpd-logs-alias" : {
"is_write_index": true
    }
  }
}
      
ES7中,冷熱分離和索引生命周期的簡單實踐

繼續閱讀