elasticsearch数据冷热分离、数据冷备
环境:
6个es节点 冷热配置
es1 master节点
# elasticsearch.yml node.name: "es1" cluster.name: "docker-cluster" network.host: 0.0.0.0 node.master: true node.data: false
es2、es3、es4 热数据节点
# 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" # 标识为热数据节点
es5、es6 冷数据节点
# 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" # 标识为热数据节点
思路:
- 创建index模板,指定"index.routing.allocation.require.box_type"为"hot"。新建立的index默认放置在热数据节点中存储。
- 修改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" 配置,效果相同。
创建测试index:
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 的数据迁移到冷数据节点上。
提示:更新索引标记的任务可以放到定时任务中去实现。
1. 有x台机器tag设置为hot
2. 有y台机器tag设置为cool
3. hot集群中只存最近两天的.
4. 有一个定时任务每天将前一天的索引标记为cool
5. es看到有新的标记就会将这个索引迁移到冷集群中, 这都是es自动完成的
参考:
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进行数据恢复,所以一定要进行数据冷备。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!