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的相關介紹可以查看我的這篇博文://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: r1
node.attr.box_type: hot

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();
            }
        }
    }

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

其它

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

ElasticSearch實戰系列:

音樂推薦

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