ElasticSearch實戰系列十: ElasticSearch冷熱分離架構


前言

本文主要介紹ElasticSearch冷熱分離架構以及實現。

冷熱分離架構介紹

冷熱分離是目前ES非常火的一個架構,它充分的利用的集群機器的優劣來實現資源的調度分配。ES集群的索引寫入及查詢速度主要依賴於磁盤的IO速度,冷熱數據分離的關鍵點為使用固態磁盤存儲數據。若全部使用固態,成本過高,且存放冷數據較為浪費,因而使用普通機械磁盤與固態磁盤混搭,可做到資源充分利用,性能大幅提升的目標。因此我們可以將實時數據(5天內)存儲到熱節點中,歷史數據(5天前)的存儲到冷節點中,並且可以利用ES自身的特性,根據時間將熱節點的數據遷移到冷節點中,這里因為我們是按天建立索引庫,因此數據遷移會更加的方便。

架構圖:
在這里插入圖片描述

一個例子

使用冷熱分離的時候,我們需要將索引庫建立在熱節點中,等到一定的時間時間在將該索引庫遷移冷節點中。因此這里我們需要更加熱節點的量來進行設置分片數。
比如,我們擁有6個熱節點,9個冷節點,索引庫的主分片的數據量500G左右,那么該索引庫建立18個分片並且都在在熱節點中,此時該索引庫的分片的分布是,熱節點:18,冷節點0;等到該數據不是熱數據之后,將該索引庫的分片全部遷移到冷節點中,索引庫的分片的分布是, 熱節點:0,冷節點18。

單個索引庫熱冷節點分片分布示例:

時間 索引庫名稱 熱節點分片數量 冷節點分片數量
20190707 TEST_20190703 18 0
20190708 TEST_20190703 0 18

最終實現效果圖,這里我用cerebro界面截圖來表示
cerebro示例圖:
寫入ES索引庫中,分片分布在熱節點中
在這里插入圖片描述
過了一段時間之后進行了遷移,分片數據遷移到了冷節點:
在這里插入圖片描述

更多ElasticSearch的相關介紹可以查看我的這篇博文:https://www.cnblogs.com/xuwujing/p/12093933.html

ElasticSearch冷熱分離架構實現

ElasticSearch冷熱分離架構是一種思想,其實現原理是使用ElasticSearch的路由完成,在data節點設置對應的路由,然后在創建索引庫時指定分布到那些服務器,過一段時間之后,根據業務要求在將這些索引庫的數據進行遷移到其他data節點中。

ElasticSearch節點配置

這里需要改變的節點為data節點,其他的節點配置無需更改。這里我就用以前寫的ElasticSearch實戰系列一: ElasticSearch集群+Kibana安裝教程里面的配置進行更改。

data節點的elasticsearch.yml原配置:

cluster.name: pancm
node.name: data1
path.data: /home/elk/datanode/data
path.logs: /home/elk/datanode/logs
network.host: 0.0.0.0
network.publish_host: 192.169.0.23
transport.tcp.port: 9300
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.169.0.23:9301","192.169.0.24:9301","192.169.0.25:9301"]
node.master: false
node.data: true
node.ingest: false 
index.number_of_shards: 5
index.number_of_replicas: 1
discovery.zen.minimum_master_nodes: 1
bootstrap.memory_lock: true
http.max_content_length: 1024mb

相比普通的data節點, 主要是增加了這兩個配置:

node.attr.rack: r1
node.attr.box_type: hot

熱節點配置示例:

cluster.name: pancm
node.name: data1
path.data: /home/elk/datanode/data
path.logs: /home/elk/datanode/logs
network.host: 0.0.0.0
network.publish_host: 192.169.0.23
transport.tcp.port: 9300
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.169.0.23:9301","192.169.0.24:9301","192.169.0.25:9301"]
node.master: false
node.data: true
node.ingest: false 
index.number_of_shards: 5
index.number_of_replicas: 1
discovery.zen.minimum_master_nodes: 1
bootstrap.memory_lock: true
http.max_content_length: 1024mb
node.attr.rack: r1
node.attr.box_type: hot

冷節點配置大體相同,就是后面的值進行更改

node.attr.rack: r9
node.attr.box_type: cool

冷節點配置示例:

cluster.name: pancm
node.name: data1
path.data: /home/elk/datanode/data
path.logs: /home/elk/datanode/logs
network.host: 0.0.0.0
network.publish_host: 192.169.0.23
transport.tcp.port: 9300
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.169.0.23:9301","192.169.0.24:9301","192.169.0.25:9301"]
node.master: false
node.data: true
node.ingest: false 
index.number_of_shards: 5
index.number_of_replicas: 1
discovery.zen.minimum_master_nodes: 1
bootstrap.memory_lock: true
http.max_content_length: 1024mb
node.attr.rack: r9
node.attr.box_type: cool

ElasticSearch索引庫設置

在創建索引庫的時候需要指定默認索引庫的分片歸屬,如果沒有指定,就會根據ElasticSearch默認進行均勻分布。這里我們將索引庫默認創建到hot節點中,滿足業務條件之后在使用命令或代碼將該索引庫設置到冷節點中。

索引示例:

PUT TEST_20190717
{
  "index":"TEST_20190717",
  "settings": {
    "number_of_shards" :18,
    "number_of_replicas" : 1,
    "refresh_interval" : "10s",
    "index.routing.allocation.require.box_type":"hot"
  },
"mappings": {
    "mt_task_hh": {
      "properties": {
        "accttype": {
          "type": "byte"
        },
....

}
}

索引庫冷節點設置

根據業務要求,我們可以對索引庫的數據進行遷移,使用dsl語句在kibana上執行或者使用java代碼實現都可以。

dsl語句:

PUT TEST_20190717/_settings
{
  
    "index.routing.allocation.require.box_type":"cool"
  
}

java代碼實現:

  public static void setCool(String index) throws IOException {
        RestClient restClient = null;
        try {
            Objects.requireNonNull(index, "index is not null");
            restClient = client.getLowLevelClient();
            String source = "{\"index.routing.allocation.require.box_type\": \"%s\"}";
            source = String.format(source, "cool");
            HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
            restClient.performRequest("PUT", "/" + index + "/_settings", Collections.<String, String>emptyMap(), entity);
        } catch (IOException e) {
            throw e;
        } finally {
            if (restClient != null) {
                restClient.close();
            }
        }
    }

完整代碼地址: https://github.com/xuwujing/java-study/tree/master/src/main/java/com/pancm/elasticsearch

其它

其實這篇文章本應來說在2019年就完成了初稿,但是因為其他的事情一直耽擱,好在查看草稿是發現了,於是便補了。因為時隔太久,細節上相比之前的文章有一定的差距。不過好在是寫出來了,以后的話寫文章的話還是盡早,不然后面就忘了。目前ElasticSearch實戰系列已經寫了10篇了,雖然中間的間隔有點久,后面我會慢慢的更新這個系列,盡量把自己所學所感悟的寫出來,如有寫的不好,希望能夠指出討論。

ElasticSearch實戰系列:

音樂推薦

原創不易,如果感覺不錯,希望給個推薦!您的支持是我寫作的最大動力!
版權聲明:
作者:虛無境
博客園出處:http://www.cnblogs.com/xuwujing
CSDN出處:http://blog.csdn.net/qazwsxpcm
掘金出處:https://juejin.im/user/5ae45d5bf265da0b8a6761e4    
個人博客出處:http://www.panchengming.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM