Carry の Blog Carry の Blog
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Carry の Blog

好记性不如烂键盘
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • MySQL

  • Redis

  • Keydb

  • TiDB

  • MongoDB

  • Elasticsearch

    • 安装配置
    • 给Elasticsearch集群添加用户密码
    • Elastichsearch的分片和副本
    • 单节点分片达到默认上限解决办法
      • Elasticsearch集群节点磁盘使用分配不均解决办法
      • Elastichsearch的模板template和映射mapping
      • Elastichsearch查询-分页查询
      • Elasticsearch字符串搜索方式
      • Elastichsearch使用wildcard字段模糊匹配
      • ES数据迁移工具esm
      • Nginx Mirror 模块实现三套ES写入网关
      • ES单机多节点集群docker-compose一键安装
      • tcpdump抓包Elasticsearch语句
      • ElasticSearch 动态模板 使用方法
      • ES打开slowlog记录慢语句
      • ES加速恢复
      • Elasticsearch 常用 DSL 语句
      • Logstash迁移ES数据
    • Kafka

    • victoriametrics

    • BigData

    • Sqlserver

    • 数据库
    • Elasticsearch
    Carry の Blog
    2022-03-17
    目录

    单节点分片达到默认上限解决办法

    Elasticsearch 默认的单节点分片上限为 1000,当达到这个上限时,集群状态会变为 RED,无法继续创建索引。以下是解决方法:


    # 方法 1:增加单节点分片限制

    修改 Elasticsearch 的单节点分片限制参数 cluster.max_shards_per_node,适合临时或紧急情况。
    注意:过多的分片可能导致内存和性能问题。

    1. 使用 REST API 修改:

      curl -XPUT -H "Content-Type: application/json" http://<ES_HOST>:<ES_PORT>/_cluster/settings -d '{
        "persistent": {
          "cluster.max_shards_per_node": <新上限>
        }
      }'
      
      1
      2
      3
      4
      5

      例如,将限制提高到 2000:

      curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_cluster/settings -d '{
        "persistent": {
          "cluster.max_shards_per_node": 2000
        }
      }'
      
      1
      2
      3
      4
      5
    2. 验证设置是否生效:

      curl -XGET http://<ES_HOST>:<ES_PORT>/_cluster/settings
      
      1

    # 方法 2:减少分片数量

    分片数量过多是因为创建索引时默认的分片数量较高,可以通过以下方式减少分片数量:

    1. 为新索引设置更少的分片: 创建索引时指定分片数量,例如设置为 1:

      curl -XPUT -H "Content-Type: application/json" http://<ES_HOST>:<ES_PORT>/<索引名> -d '{
        "settings": {
          "index": {
            "number_of_shards": 1,
            "number_of_replicas": 1
          }
        }
      }'
      
      1
      2
      3
      4
      5
      6
      7
      8
    2. 修改模板的分片配置: 如果使用模板自动创建索引,可以更新模板配置:

      curl -XPUT -H "Content-Type: application/json" http://<ES_HOST>:<ES_PORT>/_template/<模板名> -d '{
        "index_patterns": ["*"],
        "settings": {
          "number_of_shards": 1,
          "number_of_replicas": 1
        }
      }'
      
      1
      2
      3
      4
      5
      6
      7
    3. 合并索引: 使用 _shrink API 合并多个分片为一个分片:

      POST /<索引名>/_shrink/<新索引名>
      
      1

    # 方法 3:增加节点

    当分片数量增长到单节点难以承受时,可以通过增加节点来分散分片负载:

    1. 新增节点:

      • 部署新节点,并确保与现有集群连接。
      • 在 elasticsearch.yml 中配置集群名称相同的 cluster.name,并设置 discovery.seed_hosts。
    2. 验证集群节点数:

      curl -XGET http://<ES_HOST>:<ES_PORT>/_cat/nodes
      
      1
    3. 均衡分片: 新节点加入后,Elasticsearch 会自动将分片重新分配到新节点上。如果未自动分配,可以手动触发:

      POST /_cluster/reroute
      
      1

    # 方法 4:删除不必要的索引

    如果某些旧索引不再需要,可以通过以下方式删除:

    1. 列出所有索引:

      curl -XGET http://<ES_HOST>:<ES_PORT>/_cat/indices?v
      
      1
    2. 删除指定索引:

      curl -XDELETE http://<ES_HOST>:<ES_PORT>/<索引名>
      
      1
    3. 设置索引生命周期管理 (ILM): 为自动删除旧数据设置生命周期规则:

      PUT _ilm/policy/<策略名>
      {
        "policy": {
          "phases": {
            "delete": {
              "min_age": "30d",
              "actions": {
                "delete": {}
              }
            }
          }
        }
      }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13

    # 建议

    • 如果分片总数较多但每个分片数据量较小,应尽量减少分片数量(单个分片推荐数据量为 20-50GB)。
    • 如果长期需要支持大量分片,应考虑增加节点,避免单节点资源耗尽。
    上次更新: 4/24/2025

    ← Elastichsearch的分片和副本 Elasticsearch集群节点磁盘使用分配不均解决办法→

    最近更新
    01
    tidb fast ddl
    04-04
    02
    TiDB配置文件调优 原创
    04-03
    03
    如何移除TiDB中的表分区 原创
    04-03
    更多文章>
    Theme by Vdoing
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式