扩大ES索引分片数操作(一)

ES的索引扩增操作指南

导言

众所周知,在Elasticsearch中,一旦索引的分片数(shard count)在创建索引时设定,就不能直接修改。这是因为ES的设计原则之一是不可变性:一旦数据被写入,就不能修改其结构。

但需求总是真切存在的,要想增加更多的存储空间或提高性能,有以下三种通融方案:扩容、重索引、拆分片。今天介绍最为快捷的方案三:索引分片扩增。

分片扩增手把手教学

需要注意的是这三种方案只有集群扩容是原索引和原业务无感知的操作,其余的方案本质都是生成新的索引而已。

索引分片拆分的完整操作步骤,已多次在实际环境中测验通过:

  1. 打开ES的索引分配和自动均衡(执行过一次之后,后续不需要执行)

    http 复制代码
    PUT _cluster/settings
    {
      "persistent": {
        "cluster": {
          "routing": {
            "allocation.enable": "all",
            "allocation.cluster_concurrent_rebalance": 4,
            "allocation.node_concurrent_recoveries": 4,
            "allocation.allow_rebalance": "indices_primaries_active",
            "rebalance.enable": "all"
          }
        },
        "indices": {
          "recovery": {
            "max_bytes_per_sec": "500m"
          }
        }
      }
    }

    直接在Kibana中执行上述命令,无需修改。这里500m是一个速度控制。

  2. 停止业务写入后,使用命令关闭指定索引的写入开关,如这里是关闭test01。

    http 复制代码
    PUT test01/_settings
    {
      "settings": {
        "index.blocks.write": true
      }
    }

    test01需要换成要扩增的索引的名字,不是新索引的名。

  3. 进行索引的分片扩增操作,如原来是10个可以改为80或者160,如原来是1个则一般可以改为20或者40。

    http 复制代码
    POST test01/_split/test99
    {
      "settings":{
        "index.number_of_shards":80
      }
    }

    test99是新索引的名称,需要自己指定。

  4. 观察新索引是否创建

    http 复制代码
    GET /_cat/indices/test99?v
  5. 打开新索引的写入开关

    http 复制代码
    PUT test99/_settings
    {
      "settings": {
        "index.blocks.write": null
      }
    }

    警告!这一步一定要执行,否则新索引后续无法写入。

  6. 观察新索引是否恢复完毕

    http 复制代码
    GET /_cat/indices/test99?v

    health变为green就是恢复完成了。

  7. 观察集群是否恢复green

    http 复制代码
    GET /_cluster/health

    恢复为green即可。

以上步骤中,只有2、3、5是必须执行的,第一步执行一次后无需再执行,其他操作均为观察情况。

完成之后,老的索引就可以删除了(空间会稍后释放):

http 复制代码
DELETE test01

注意事项

待扩增生成的ES新索引的分片数并不能任意指定:

示例的分片扩增(Split)遵循以下两个条件约束(首先要大于原分片数):

  • 第一,是旧索引分片的整数倍
  • 第二,是640的因数

否则分别报错如下:

例如,10改22,会报错说the number of source shards 10 must be a factor of 22

又比如,10改30看着挺好已取整,但又报错说the number of routing shards 640 must be a multiple of the target shards 30

分片扩增成功的示例:也就是10改40或者80是没有问题的。

另外,如果索引没有设置为只读,会报错:

index test99 must be read-only to resize index. use "index.blocks.write=true"

只需按照步骤2设置一下就可以了,务必先通知业务侧停止写入数据!

More

喜欢我们的文章?那就关注公众号**1024点线面**吧!更多精彩,不容错过。

相关推荐
Elasticsearch12 小时前
Kubernetes attributes processor v1:它对 EDOT Collector 意味着什么
elasticsearch
Elasticsearch13 小时前
Elasticsearch Serverless 如何通过 hollow shards 将索引节点关闭次数降低 30%
elasticsearch
SelectDB技术团队14 小时前
Agent Trace 数据底座建设:宽表建模、全文检索与成本聚合的配置与验证步骤
大数据·python·clickhouse·elk·elasticsearch·全文检索·复杂查询
Zhu75814 小时前
快速测试-ZLMediaKit
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客14 小时前
使用 NVIDIA cuVS 在 Elasticsearch 中实现 GPU 加速的向量索引:在 10 分钟内处理 1.38 亿个向量
大数据·数据库·elasticsearch·搜索引擎·全文检索·安全威胁分析
liuhl09101 天前
【无标题】
大数据·elasticsearch·搜索引擎
xbgRS1 天前
Elasticsearch的查询
elasticsearch
Elasticsearch1 天前
使用 Lucene 搜索你的 Bean —— Highlighting
elasticsearch
Elasticsearch1 天前
使用 Lucene 搜索你的 Bean —— Elasticsearch
elasticsearch
Elasticsearch2 天前
最好的 LLM 只有 59% 的时间能写出正确的 Elasticsearch ES|QL。以下是另外 41% 出错的原因
elasticsearch