Elasticsearch分片管理 #

一、分片概述 #

1.1 分片类型 #

text
分片类型
├── 主分片(Primary Shard)
│   ├── 存储原始数据
│   ├── 处理写入请求
│   └── 创建时确定数量
└── 副本分片(Replica Shard)
    ├── 主分片的副本
    ├── 处理读请求
    └── 可动态调整数量

1.2 分片作用 #

text
分片作用
├── 水平扩展
│   └── 数据分布到多个节点
├── 并行处理
│   └── 查询并行执行
├── 高可用
│   └── 副本提供冗余
└── 负载均衡
    └── 读写分散

二、分片策略 #

2.1 分片数量规划 #

text
分片数量规划
├── 数据量
│   └── 单分片10-50GB
├── 节点数量
│   └── 分片数≥节点数
├── 查询并发
│   └── 分片数匹配并发需求
└── 增长预期
    └── 预留扩展空间

2.2 分片数量计算 #

text
计算公式
├── 主分片数 = 数据总量 / 单分片大小
├── 副本数 = 节点数 - 1 (最大)
└── 总分片数 = 主分片数 × (1 + 副本数)

2.3 创建索引时设置分片 #

bash
PUT /products
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  }
}

2.4 动态调整副本 #

bash
PUT /products/_settings
{
  "number_of_replicas": 2
}

三、分片路由 #

3.1 路由公式 #

text
路由公式
shard_num = hash(_routing) % num_primary_shards

默认routing = _id

3.2 自定义路由 #

bash
PUT /products/_doc/1?routing=apple
{
  "name": "iPhone 15",
  "brand": "Apple"
}

3.3 路由配置 #

bash
PUT /products
{
  "mappings": {
    "_routing": {
      "required": true
    }
  }
}

3.4 路由查询 #

bash
GET /products/_doc/1?routing=apple

GET /products/_search?routing=apple
{
  "query": {
    "match_all": {}
  }
}

3.5 路由优势 #

text
路由优势
├── 数据局部性
│   └── 相关数据在同一分片
├── 查询效率
│   └── 只查询相关分片
└── 资源隔离
    └── 不同租户数据分离

四、分片分配 #

4.1 分片分配设置 #

bash
PUT /_cluster/settings
{
  "persistent": {
    "cluster.routing.allocation.enable": "all",
    "cluster.routing.allocation.cluster_concurrent_rebalance": 2,
    "cluster.routing.allocation.node_concurrent_recoveries": 2,
    "cluster.routing.allocation.node_initial_primaries_recoveries": 4
  }
}
参数 说明
cluster.routing.allocation.enable 分配策略
cluster_concurrent_rebalance 并发重平衡数
node_concurrent_recoveries 节点并发恢复数

4.2 分配过滤 #

bash
PUT /_cluster/settings
{
  "persistent": {
    "cluster.routing.allocation.include._tier_preference": "data_hot,data_warm,data_cold"
  }
}

4.3 节点属性分配 #

bash
PUT /_cluster/settings
{
  "persistent": {
    "cluster.routing.allocation.require.box_type": "hot"
  }
}

4.4 索引级别分配 #

bash
PUT /products/_settings
{
  "index.routing.allocation.include._tier_preference": "data_hot"
}

五、分片均衡 #

5.1 均衡设置 #

bash
PUT /_cluster/settings
{
  "persistent": {
    "cluster.routing.rebalance.enable": "all",
    "cluster.routing.balance.shard": 0.45,
    "cluster.routing.balance.index": 0.55,
    "cluster.routing.balance.threshold": 1.0
  }
}
参数 说明 默认值
shard 分片权重 0.45
index 索引权重 0.55
threshold 阈值 1.0

5.2 禁用均衡 #

bash
PUT /_cluster/settings
{
  "persistent": {
    "cluster.routing.rebalance.enable": "none"
  }
}

5.3 索引级别均衡 #

bash
PUT /products/_settings
{
  "index.routing.rebalance.enable": "none"
}

六、分片恢复 #

6.1 恢复设置 #

bash
PUT /_cluster/settings
{
  "persistent": {
    "indices.recovery.max_bytes_per_sec": "100mb",
    "indices.recovery.max_concurrent_file_chunks": 2,
    "indices.recovery.max_concurrent_operations": 1
  }
}

6.2 查看恢复状态 #

bash
GET /_cat/recovery?v

GET /products/_recovery

6.3 分片状态 #

状态 说明
INITIALIZING 初始化中
STARTED 已启动
RELOCATING 迁移中
UNASSIGNED 未分配

七、未分配分片 #

7.1 查看未分配分片 #

bash
GET /_cat/shards?v&state=UNASSIGNED

GET /_cluster/allocation/explain
{
  "index": "products",
  "shard": 0,
  "primary": true
}

7.2 分配未分配分片 #

bash
POST /_cluster/reroute
{
  "commands": [
    {
      "allocate_stale_primary": {
        "index": "products",
        "shard": 0,
        "node": "node-1",
        "accept_data_loss": true
      }
    }
  ]
}

7.3 常见原因 #

text
未分配原因
├── 节点离线
├── 磁盘空间不足
├── 副本数过多
├── 分配规则限制
└── 分片损坏

八、分片监控 #

8.1 分片状态 #

bash
GET /_cat/shards?v

GET /_cat/shards/products?v

8.2 分片统计 #

bash
GET /_stats

GET /products/_stats

8.3 段信息 #

bash
GET /_cat/segments?v

GET /products/_segments

九、最佳实践 #

9.1 分片规划 #

text
分片规划建议
├── 单分片大小
│   └── 10-50GB
├── 分片数量
│   └── 每节点< 20个/GB堆
├── 副本数量
│   └── 生产至少1个
└── 预留扩展
    └── 考虑数据增长

9.2 路由建议 #

text
路由建议
├── 使用业务字段路由
│   └── 如用户ID、租户ID
├── 必须路由
│   └── 设置required: true
├── 查询时指定路由
│   └── 提高查询效率
└── 避免热点
    └── 合理选择路由字段

十、总结 #

本章介绍了Elasticsearch分片管理:

  1. 主分片和副本分片各有作用
  2. 合理规划分片数量
  3. 路由控制数据分布
  4. 分配和均衡自动管理
  5. 监控和处理未分配分片
  6. 根据场景优化分片策略

下一步,我们将学习集群健康。

最后更新:2026-03-27