在早期的版本中,索引中的数据增长到一定量之后,如何实现无感切换,答案是通过定时任务和_rollover api,注意_rollover api只能应用于别名,并且应用切换条件。
实验如下:
###创建索引和别名,主分片为5
PUT my_log-000001
{
"settings": {
"number_of_shards": 5
},
"aliases": {
"my_log": {}
}
}
###连续插入4条件数据
POST my_log/_doc
{
"msg":"hello4"
}
###查看下数据和别名指向的索引
GET my_log/_search
GET _alias/my_log
数据会一直存在my_log-000001中
###通过调用_rollover api进行数据切换
POST my_log/_rollover
{
"conditions": {
"max_docs": 3,
"max_age": "7d",
"max_size": "5gb"
}
}
GET _alias/my_log
输出结果
{
"my_log-000002": {
"aliases": {
"my_log": {}
}
}
}
至此可以通过写一个定时器,定时的去调用my_log/_rollover api实现日志索引的管理,满足条件手动切换。
接着上面的话题,别名my_log已经指向新的索引了my_log-000002,那旧的索引my_log-000001,该如何处理呢,可以把多个主分片合并以节省资源,因为他已经成为历史索引了,合并索引前,有几个需要注意的点:
进行以上设置,最好按顺序来,或一起设置
PUT my_log-000001/_settings
{
"number_of_replicas": 0,
"index.routing.allocation.require._ip":"10.189.138.22",
"index.blocks.write":"true"
}
进行索引shrink
POST my_log-000001/_shrink/my_log_00001_shrink
#原先是5个主分片,现在变成了1个主分片,通过以下方式查看
GET my_log_00001_shrink
GET my_log_00001_shrink/_search
现在主分片已经合并了,还可以进一步优化:合并segment
##如何实现索引的force_merge
POST my_log_00001_shrink/_forcemerge?max_num_segments=1
max_num_segments只能作为api参数传递,不能做为body使用
经过以上手动一步步的处理,我们可以写成定时任务,通过程序脚本去调用,实现当前索引,单个索引不会太大,历史索引通过主分片合并,segment合并,副本清理,一步一步减少历史索引对集群资源的使用情况,有没有更好的办法呢。
以上一系列手动的操作,我们可以通过定义一个ilm来解决,ES自动帮我们处理。
实验一下:
DELETE my_log-000001
DELETE my_log-000002
PUT _ilm/policy/my_log_policy
{
"policy": {
"phases": {
"hot": {
"min_age": "0",
"actions": {
"set_priority": {
"priority": 100
},
"rollover": {
"max_age": "1m",
"max_docs": 3,
"max_size": "5gb"
}
}
},
"warm": {
"min_age": "1m",
"actions": {
"set_priority": {
"priority": 50
},
"allocate": {
"number_of_replicas": 0
},
"forcemerge": {
"max_num_segments": 1
},
"shrink": {
"number_of_shards": 1
}
}
}
}
}
}
PUT my_log-000001
{
"settings": {
"index.lifecycle.name":"my_log_policy",
"index.lifecycle.rollover_alias":"my_log"
},
"aliases": {
"my_log": {}
}
}
查看索引是否已经关联到ilm
GET my_log-000001/_ilm/explain
POST my_log/_doc
{
"msg":"hello4"
}
连续4次插入数据,当插入第4条数据的时候,理论上索引会发生自动切换。
但是通过GET /_alias/my_log查看的时候,并没有指向my_log-000002索引。
原因是ilm在检查索引是否符合切换条件时,是10分钟检测一次,我们调整一下成5秒。
PUT _cluster/settings
{
"transient": {
"indices.lifecycle.poll_interval":"5s"
}
}
过一分就会发现已经切换。
继续往里面插入数据5条
POST my_log/_doc
{
"msg":"hello5"
}
等多久都不会再次切换,原因是什么呢?
通过查看新索引的ilm策略,根本没应用上,第一次我们创建索引时,是手动指定的,新索引没有关联到ilm,
所以不会发生切换,要如何解决呢?
我们可以通过索引模板,每次生成新的索引时,自动关联相关字段,别名,ilm策略等,在新的8.X以上的版本,
同时引入了组件模板,使索引模板使用更灵活。
删除以上索引重来一遍
DELETE my_log-000001
DELETE my_log-000002
创建组件模板
PUT _component_template/componet_log_setting
{
"template": {
"settings": {
"number_of_shards": 5
}
}
}
PUT _component_template/componet_log_mapping
{
"template": {
"mappings": {
"properties": {
"@timestamp":{
"type": "date"
},
"msg":{
"type": "text"
}
}
}
}
}
创建索引模板
PUT _index_template/log_template
{
"index_patterns": [
"my_log*"
],
"template": {
"settings": {
"index.lifecycle.name": "my_log_policy",
"index.lifecycle.rollover_alias": "my_log"
}
},
"composed_of": [
"componet_log_setting",
"componet_log_mapping"
]
}
PUT my_log-000001
{
"settings": {
"index.lifecycle.name":"my_log_policy",
"index.lifecycle.rollover_alias":"my_log"
},
"aliases": {
"my_log": {}
}
}
POST my_log/_doc
{
"msg":"hello3"
}
连续插入10条记录,发现已经切换到3了
GET /_alias/my_log
返回结果
{
"my_log-000003": {
"aliases": {
"my_log": {}
}
}
}
GET my_log-000001
GET my_log-000002
两个索引已经合并了。
在现实的实际生产环境中,会存在的情况是,已经有索引很大了,对历史数据要清理,对新产生的数据应用索引策略,具体如何操作呢?
GET /_cat/nodeattrs
PUT my_index_test/_doc/1
{
"message":"zhouyinhui1"
}
PUT my_index_test/_doc/2
{
"message":"zhouyinhui2"
}
PUT my_index_test/_doc/3
{
"message":"zhouyinhui3"
}
PUT my_index_test/_doc/4
{
"message":"zhouyinhui4"
}
PUT /_ilm/policy/my_policy_test
{
"policy": {
"phases": {
"warm": {
"min_age": "1m",
"actions": {
"set_priority": {
"priority": 50
},
"allocate": {
"number_of_replicas": 0,
"require": {
"_ip": "10.189.138.33"
}
},
"forcemerge": {
"max_num_segments": 1
}
}
}
}
}
}
GET /_cat/shards/my_index_test
GET /_cat/segments/my_index_test?v
PUT my_index_test/_settings
{
"index.lifecycle.name":"my_policy_test"
}
PUT /_cluster/settings
{
"transient": {
"cluster.routing.allocation.node_concurrent_recoveries": "2",
"cluster.routing.allocation.cluster_concurrent_rebalance": "2",
"indices.recovery.max_bytes_per_sec": "80mb"
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有