elasticsearch 分片及集群节点下线

分片(shard):

因为ES是个分布式的搜索引擎, 所以索引通常都会分解成不同部分, 而这些分布在不同节点的数据就是分片. ES自动管理和组织分片, 并在必要的时候对分片数据进行再平衡分配, 所以用户基本上不用担心分片的处理细节,一个分片默认最大文档数量是20亿.
ES默认为一个索引创建5个主分片,数据将尽可能平均分配到每一个分片。

副本(replica)

每一个主分片默认创建一个副本,副本数量可动态修改。
副本为主分片备份

对于分布式搜索引擎来说, 分片及副本的分配将是高可用及快速搜索响应的设计核心.主分片与副本都能处理查询请求, 它们的唯一区别在于只有主分片才能处理索引请求.
单节点(单机)不可创建副本,副本应存在于其它节点,防止主节点挂掉后数据丢失。

集群查看

GET _cluster/health?level=indices

goods为索引
goods
集群分片

GET _cluster/health?level=shards

在这里插入图片描述
查看索引分片

GET _cluster/health/goods?level=shards
{
    
    
  "cluster_name": "elasticsearch",
  "status": "green",
  "timed_out": false,
  "number_of_nodes": 3,
  "number_of_data_nodes": 3,
  "active_primary_shards": 5,
  "active_shards": 10,
  "relocating_shards": 0,
  "initializing_shards": 0,
  "unassigned_shards": 0,
  "delayed_unassigned_shards": 0,
  "number_of_pending_tasks": 0,
  "number_of_in_flight_fetch": 0,
  "task_max_waiting_in_queue_millis": 0,
  "active_shards_percent_as_number": 100,
  "indices": {
    
    
    "goods": {
    
    
      "status": "green",
      "number_of_shards": 5,
      "number_of_replicas": 1,
      "active_primary_shards": 5,
      "active_shards": 10,
      "relocating_shards": 0,
      "initializing_shards": 0,
      "unassigned_shards": 0,
      "shards": {
    
    
        "0": {
    
    
          "status": "green",
          "primary_active": true,
          "active_shards": 2,
          "relocating_shards": 0,
          "initializing_shards": 0,
          "unassigned_shards": 0
        },
        "1": {
    
    
          "status": "green",
          "primary_active": true,
          "active_shards": 2,
          "relocating_shards": 0,
          "initializing_shards": 0,
          "unassigned_shards": 0
        },
        "2": {
    
    
          "status": "green",
          "primary_active": true,
          "active_shards": 2,
          "relocating_shards": 0,
          "initializing_shards": 0,
          "unassigned_shards": 0
        },
        "3": {
    
    
          "status": "green",
          "primary_active": true,
          "active_shards": 2,
          "relocating_shards": 0,
          "initializing_shards": 0,
          "unassigned_shards": 0
        },
        "4": {
    
    
          "status": "green",
          "primary_active": true,
          "active_shards": 2,
          "relocating_shards": 0,
          "initializing_shards": 0,
          "unassigned_shards": 0
        }
      }
    }
  }
}

分析

GET _cluster/allocation/explain

索引分析

GET _cluster/allocation/explain
{
    
    
  "index": "goods",
  "shard": 2,
  "primary": true
}

参数按自己情况填写,shard为分片数

节点下线

节点下线需要将此节点上分片数据移动至其它节点
如果直接关闭节点,那在进行文档更新的时候,文档所在主分片位于关闭节点,则会报错,找不到对应primary shard。
1、ES提供了让某个节点上所有数据都移走的功能如下:

ElasticSearch集群就会自动把这个节点上的所有分片,都自动转移到其他节点上,等到转移完成,这个空节点就可以毫无影响的下线。

PUT http://hostname:9200/_cluster/settings?pretty{
    
    
  "transient": {
    
    
    "cluster.routing.allocation.exclude._name": "{node.name}"
  }
}

查看此节点分片信息

GET _nodes/{
    
    node.name}/stats/indices?pretty

等待所有数据迁移完成了才可以将节点关闭。
此时集群里面将此节点分片迁移至其他节点,所以当有数据来时不会再进入此节点。当下线后如果需要重新上线,需要将节点重新分配分片。

PUT http://hostname:9200/_cluster/settings?pretty{
    
    
  "transient": {
    
    
    "cluster.routing.allocation.exclude._name": null
  }
}

此时集群路由恢复,当此节点再次上线后会再次分配分片。
具体参考
官方文档

猜你喜欢

转载自blog.csdn.net/qq_27275851/article/details/112188005
今日推荐