网站怎么做视频,三丰云做游戏网站,建站系统推荐,wordpress建设QQ登录文章目录 冷热集群架构的应用场景冷热集群架构的优势冷热集群架构实战搭建集群 索引生命周期管理认识索引生命周期索引生命周期管理的历史演变索引生命周期管理的基础知识Rollover#xff1a;滚动索引 冷热集群架构的应用场景
某客户的线上业务场景如下#xff1a;系统每天增… 文章目录 冷热集群架构的应用场景冷热集群架构的优势冷热集群架构实战搭建集群 索引生命周期管理认识索引生命周期索引生命周期管理的历史演变索引生命周期管理的基础知识Rollover滚动索引 冷热集群架构的应用场景
某客户的线上业务场景如下系统每天增加6TB日志数据高峰时段的写入和查询频率都很高导致Elasticsearch集群压力大经常出现查询缓慢的问题。
目前采用冷热集群架构其中热节点使用SSD并具有良好的索引和搜索性能将数据保存4天后推送到温节点中并使用HDD进行存储。改善架构后集群性能有极大提升。
总之在成本有限的情况下让客户专注于实时数据和历史数据之间的硬件隔离可以最大化解决客户反映的检索响应慢的问题。
冷热集群架构的优势
Elasticsearch集群采用冷热集群架构会具有以下优势。
1)有效降低存储成本将不常用的数据存储在冷节点上可以减小热节点上索引大小并降低存储成本。这可有效降低硬件成本并提高索引查询效率。
2)更好地管理数据将不同类型的数据分配到不同类型的节点上可以更好地管理数据。这为索引生命周期管理提供了保障。
3)提高查询性能将热节点用于处理最常访问的数据而将冷节点用于存储不常用的数据可以缩短查询响应时间并提高查询性能。
4)优化集群性能将不同类型的任务分配给不同类型的节点可以避免资源争夺现象从而提高整个集群系统性能。
5)更好的可扩展性使用冷热集群架构在需要适应数据量增长或其他需求变化时运维人员可以更容易地扩展集群、添加或删除节点。综上所述Elasticsearch的冷热集群架构可以提高查询性能、降低存储成本、优化集群性能、更好地管理数据和提高可扩展性。这些优势使其成为处理PB级时序大数据的理想选择。
冷热集群架构实战
搭建集群
首先搭建一个具有3个节点的集群划分冷、温、热节点角色。在节点层面设置节点类型分别如下所示。
version: 2.2
services:cerebro:image: lmenezes/cerebro:0.8.3container_name: hwc_cerebroports:- 9000:9000command:- -Dhosts.0.hosthttp://elasticsearch:9200networks:- hwc_es7netkibana:image: docker.elastic.co/kibana/kibana:7.1.0container_name: hwc_kibana7environment:- XPACK_GRAPH_ENABLEDtrue- TIMELION_ENABLEDtrue- XPACK_MONITORING_COLLECTION_ENABLEDtrueports:- 5601:5601networks:- hwc_es7netelasticsearch:image: docker.elastic.co/elasticsearch/elasticsearch:7.1.0container_name: es7_hotenvironment:- cluster.namegeektime-hwc- node.namees7_hot- node.attr.box_typehot- bootstrap.memory_locktrue- ES_JAVA_OPTS-Xms512m -Xmx512m- discovery.seed_hostses7_hot,es7_warm,es7_cold- cluster.initial_master_nodeses7_hot,es7_warm,es7_coldulimits:memlock:soft: -1hard: -1volumes:- hwc_es7data_hot:/usr/share/elasticsearch/dataports:- 9200:9200networks:- hwc_es7netelasticsearch2:image: docker.elastic.co/elasticsearch/elasticsearch:7.1.0container_name: es7_warmenvironment:- cluster.namegeektime-hwc- node.namees7_warm- node.attr.box_typewarm- bootstrap.memory_locktrue- ES_JAVA_OPTS-Xms512m -Xmx512m- discovery.seed_hostses7_hot,es7_warm,es7_cold- cluster.initial_master_nodeses7_hot,es7_warm,es7_coldulimits:memlock:soft: -1hard: -1volumes:- hwc_es7data_warm:/usr/share/elasticsearch/datanetworks:- hwc_es7netelasticsearch3:image: docker.elastic.co/elasticsearch/elasticsearch:7.1.0container_name: es7_coldenvironment:- cluster.namegeektime-hwc- node.namees7_cold- node.attr.box_typecold- bootstrap.memory_locktrue- ES_JAVA_OPTS-Xms512m -Xmx512m- discovery.seed_hostses7_hot,es7_warm,es7_cold- cluster.initial_master_nodeses7_hot,es7_warm,es7_coldulimits:memlock:soft: -1hard: -1volumes:- hwc_es7data_cold:/usr/share/elasticsearch/datanetworks:- hwc_es7netvolumes:hwc_es7data_hot:driver: localhwc_es7data_warm:driver: localhwc_es7data_cold:driver: localnetworks:hwc_es7net:driver: bridge三个节点冷热集群架构启动后使用如下命令查看节点。
GET /_cat/nodes?v查询节点属性使用如下命令
GET _cat/nodeattrses7_warm 172.21.0.6 172.21.0.6 ml.machine_memory 9698672640
es7_warm 172.21.0.6 172.21.0.6 ml.max_open_jobs 20
es7_warm 172.21.0.6 172.21.0.6 box_type warm
es7_warm 172.21.0.6 172.21.0.6 xpack.installed true
es7_cold 172.21.0.2 172.21.0.2 ml.machine_memory 9698672640
es7_cold 172.21.0.2 172.21.0.2 ml.max_open_jobs 20
es7_cold 172.21.0.2 172.21.0.2 box_type cold
es7_cold 172.21.0.2 172.21.0.2 xpack.installed true
es7_hot 172.21.0.4 172.21.0.4 ml.machine_memory 9698672640
es7_hot 172.21.0.4 172.21.0.4 box_type hot
es7_hot 172.21.0.4 172.21.0.4 xpack.installed true
es7_hot 172.21.0.4 172.21.0.4 ml.max_open_jobs 20
方案一在索引层面指定节点角色写入数据。
PUT mytest
{settings:{number_of_shards:1,number_of_replicas:0,index.routing.allocation.require.box_type:cold}
}GET /_cat/shards/mytest?hi,shard,p,nodemytest 0 p es7_cold方案二通过模板指定节点角色写入数据。
POST _template/my-template
{index_patterns: test-*,settings: {index.number_of_replicas: 0,index.routing.allocation.require.box_type:cold}
}GET _template/my-templatePUT /test-01GET /_cat/shards/test-01?hi,shard,p,node索引生命周期管理
认识索引生命周期
在大规模系统中特别是以日志、指标和实时时间序列为基础的系统中集群索引的发展和变化遵循其固有规律。理论上来说一旦创建了索引它就可能永久存在。然而在创建后如果让索引无限制地扩张下去则会演变成一个数据量庞大且过度膨胀的实体。这种情况可能导致一系列问题例如随着时间推移时序数据和业务数据量逐步增加。
实际上索引并非无限制存在的举例如下。
❑集群单个分片的最大文档数上限约为20亿条即232-1。 ❑根据官方的最佳实践建议应将索引分片大小控制在30GB50GB之间。 ❑如果索引数据量过大则可能出现健康问题并导致整个集群核心业务停摆。 ❑大型索引恢复所需时间远远超过小型索引。 ❑大型索引检索单位速度较慢并影响写入和更新操作。 ❑在某些业务场景中用户更关注最近3天或7天的业务数据而大型索引会将所有历史数据汇聚在一起不利于查询特定需求的数据。
索引生命周期管理的历史演变
ILM索引生命周期管理是Elasticsearch 6.6公测版首次引入的功能并于6.7版本正式推出。它是Elasticsearch的一部分主要用于帮助用户管理索引。在没有ILM之前索引生命周期的管理基于Rollover和Curator工具实现。Kibana 7.X版本提供了一个配置界面来进行索引生命周期管理如图所示。
索引生命周期管理的基础知识
在3个节点的集群中节点角色的设置分别如下。 ❑节点node-1主节点数据节点热节点。 ❑节点node-2主节点数据节点温节点。 ❑节点node-3主节点数据节点冷节点。
为了演示如何应用ILM首先需要配置冷热架构在上文中已经讲解了配置细节。如果磁盘数量不足则待删除的冷数据在处理时具有最高优先级如果硬件资源相对受限则应将SSD作为热节点的首选配置。检索优先级最高的是热节点数据因此检索热节点数据比检索全量数据的响应速度更快。
Rollover滚动索引
自从Elasticsearch 5.X版本推出Rollover API以来该API解决了使用日期作为索引名称的索引所具有的大小不均衡的问题。对于日志类数据而言Rollover非常有用。通常情况下我们按天对索引进行分割如果数据量更大则可以进一步拆分。在没有Rollover之前我们需要在程序中设置一个自动生成索引的模板。
以下讲解如何实践Rollover操作。
1)创建符合正则表达式规范即中间是“-”字符并且后面是数字字符的索引并批量导入数据。否则会出现以下报错。 创建索引和导入数据操作如下。
###创建基于日期的索引
PUT my-index-20250709-0000001
{aliases: {my-alias: {is_write_index: true}}
}
####批量导入数据
PUT my-alias/_bulk
{index:{_id:1}}
{title:testing 01}
{index:{_id:2}}
{title:testing 02}
{index:{_id:3}}
{title:testing 03}
{index:{_id:4}}
{title:testing 04}
{index:{_id:5}}
{title:testing 05}2)基于滚动的3个条件实现索引的滚动。
❑滚动条件1数据写入时间超过7天。 ❑滚动条件2最大文档数超过5条。 ❑滚动条件3最大的主分片数大于50gb这里的gb代表Gigabytes可理解为GB。
注意
当3个条件是“或”的关系时只要满足其中一个索引就会滚动。在Elasticsearch 7.X版本中可以设置的滚动条件如表12-1所示。 查看索引中所有主分片(pri.store.size)的总存储空间命令如下。
GET _cat/indices?vspri.store.size:desc查看分片大小且按照分片大小由大到小进行排序命令行如下。其中p代表主分片r代表副本分片。
GET /_cat/shards?vtruesstore:desc滚动索引操作如下。
# rollover滚动索引
POST my-alias/_rollover
{conditions: {max age: 7d,max docs: 5,max_primary_shard_size: 50gb}
}GET my-alias/ _count
#在满足滚动条件的前提下滚动索引
PUT my-alias/_bulk
{index:{_id:6}}
{title:testing 06}
#检索数据,验证滚动是否生效
GET my-alias/_search执行结果如下所示。我们可以清晰看到插入第6条数据会触发max_docs:5的条件原来的索引my-index-20250709-000001会继续保留而新写入的第6条数据滚动到了新索引my-index-20250709-000002中。滚动索引操作的效果如图所示。 es 7.17.1
DELETE _template/test_templateGET _template/test_templatePUT _ilm/policy/my_policy
{policy: {phases: {hot: {actions: {rollover: {max_docs: 3 }}},delete: {min_age: 30d,actions: {delete: {}}}}}
}PUT _template/test_template
{order: 0,index_patterns: [test_*],settings: {index: {number_of_shards: 6,refresh_interval: 10s},lifecycle: {name: my_policy,rollover_alias : test_rollover}},mappings: {properties: {createTime: {type: date,format: yyyy-MM-dd HH:mm:ss},requestId: {type: keyword},title: {type: text,analyzer: standard,fields: {keyword: {type: keyword}}}}},aliases: {}
}PUT %3Ctest_%7Bnow%2Fd%7D-000001%3E
{aliases: {test_rollover: {is_write_index: true}}
}GET %3Ctest_%7Bnow%2Fd%7D-000001%3EPOST /test_rollover/_rollover
{conditions: {max_docs: 3 }
}POST /test_rollover/_doc
{businessInfo:078
}GET /test_rollover/_doc/_search## indices.lifecycle.poll_interval 索引生命周期策略默认是10分钟检查一次符合策略的索引GET _cluster/settings?include_defaultsflat_settingsGET _cat/nodeattrs?vhhost,attr,value
参考
https://juejin.cn/post/7217399337989488695#heading-3
https://help.aliyun.com/document_detail/178307.html
## 运行三个节点分片 将box_type设置成 hotwarm和coldPUT _cluster/settings
{persistent: {indices.lifecycle.poll_interval:1s}
}PUT /_ilm/policy/log_ilm_policy
{policy: {phases: {hot: {actions: {rollover: {max_docs: 5}}},warm: {min_age: 10s,actions: {allocate: {include: {box_type: warm}}}},cold: {min_age: 15s,actions: {allocate: {include: {box_type: cold}}}},delete: {min_age: 20s,actions: {delete: {}}}}}
}PUT /_template/log_ilm_template
{index_patterns : [app_log-*],settings : {index : {lifecycle : {name : log_ilm_policy,rollover_alias : app_log},routing : {allocation : {include : {box_type : hot}}},number_of_shards : 1,number_of_replicas : 0}},mappings : { },aliases : {}
}GET /_template/DELETE /_template/log_ilm_templatePUT %3Capp_log-%7Bnow%2Fs%7Byyyy.MM.dd-HHmm%7D%7D-000001%3E
{aliases:{app_log:{is_write_index:true}}
}POST app_log/_doc
{dfd:qweqwe
}DELETE app_log-2024.01.06-0749-000001GET app_log-2024.01.06-0800-000001/_settings
安装 https://github.com/onebirdrocks/geektime-ELK