工做隨筆——elasticsearch數據冷熱分離、數據冷備

概述:

  1. 適合日誌類型的數據存儲方案。即當日數據寫入,歷史數據只讀。
  2. 節省部分硬件成本。熱數據採用更好的硬件。

環境:

已有6個ES節點,使用docker-compose方式搭建。html

  es1:master節點node

# elasticsearch.yml
node.name: "es1"
cluster.name: "docker-cluster"
network.host: 0.0.0.0
node.master: true
node.data: false

  es二、es三、es4 熱數據節點es6

# elasticsearch.yml
node.name: "es2" # 提示:自行修改其餘節點的名稱
cluster.name: "docker-cluster"
network.host: 0.0.0.0
node.master: false
node.data: true
discovery.zen.ping.unicast.hosts: ["es1"]
node.attr.box_type: "hot"  # 標識爲熱數據節點

  es五、es6 冷數據節點docker

# elasticsearch.yml
node.name: "es5-cool" # 提示:自行修改其餘節點的名稱
cluster.name: "docker-cluster"
network.host:
0.0.0.0
node.master:
false
node.data:
true
discovery.zen.ping.unicast.hosts: [
"es1"]
node.attr.box_type:
"cool" # 標識爲熱數據節點

 

思路:

  1. 建立index模板,指定"index.routing.allocation.require.box_type"爲"hot"。新創建的index默認放置在熱數據節點中存儲。
  2. 修改index中"index.routing.allocation.require.box_type"爲"cool",讓ES自動遷移數據到冷數據節點中存儲。

建立index模板:

PUT /_template/hot_template
{
    "index_patterns" : "*", # 匹配全部的索引
    "order" : 0, # 多個模板同時匹配,以order順序倒排,order越大,優先級越高
    "settings" : {
        "number_of_shards" : 2, 
        "index.routing.allocation.require.box_type": "hot", # 指定默認爲熱數據節點
        "number_of_replicas": 1     
    }
}

提示:若是不想建立index模板,能夠在建立index時在setting中指定 "index.routing.allocation.require.box_type": "hot" 配置,效果相同。elasticsearch

 

建立測試index:ide

POST /test_index/test
{
  "test": "test"
}

查看測試index的settings信息:測試

GET test_index/_settings
回顯以下:
{
  "test_index" : {
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "require" : {
              "box_type" : "hot" # 默認index模板匹配成功,數據存放在熱數據節點
            }
          }
        },
        "number_of_shards" : "2",
        "provided_name" : "test_index",
        "creation_date" : "1553160622469",
        "number_of_replicas" : "1",
        "uuid" : "1q0SM1znRUKknJV6N8iJDQ",
        "version" : {
          "created" : "6060199"
        }
      }
    }
  }
}

數據遷移:

PUT test_index/_settings
{
   "settings": {
     "index.routing.allocation.require.box_type": "cool"  # 指定數據存放到冷數據節點
   }
}

 ES會自動將 test_index 的數據遷移到冷數據節點上。ui

提示:更新索引標記的任務能夠放到定時任務中去實現。es5

 

1. 有x臺機器tag設置爲hot
2. 有y臺機器tag設置爲cool
3. hot集羣中只存最近兩天的.
4. 有一個定時任務天天將前一天的索引標記爲cool
5. es看到有新的標記就會將這個索引遷移到冷集羣中, 這都是es自動完成的
spa

參考:

https://elasticsearch.cn/article/6127

https://elasticsearch.cn/question/283

 

數據冷備:

參考:https://www.elastic.co/guide/cn/elasticsearch/guide/current/backing-up-your-cluster.html

PUT _snapshot/my_backup  # my_backup 備份的名稱
{
    "type": "fs", 
    "settings": {
        "location": "/mount/backups/my_backup" 
    }
}

ES一旦數據被刪除沒法經過translog進行數據恢復,因此必定要進行數據冷備。

相關文章
相關標籤/搜索