es 建索引指定分片 es索引重新分片_reroute

1.概述

elasticsearch可以通过reroute api来手动进行索引分片的分配。

不过要想完全手动,必须先把cluster.routing.allocation.disable_allocation参数设置为true禁止es进行自动索引分片分配,否则你从一节点把分片移到另外一个节点,那么另外一个节点的一个分片又会移到那个节点。数据量很少的时候,可能影响不大,但是如果数据量很大,这个参数必须要设置,因为如果不设置,即便你不手动迁移分片,在我们重启机群的时候,也会产生分片的迁移,导致大量资源被占用,重启变慢.

一共有三种操作,分别为:移动(move),取消(cancel)和分配(allocate)。下面分别介绍这三种情况:

1.1 移动(move)

把分片从一节点移动到另一个节点。可以指定索引名和分片号。

1.2 取消(cancel)

取消分配一个分片。可以指定索引名和分片号。node参数可以指定在那个节点取消正在分配的分片。allow_primary参数支持取消分配主分片。

1.3 分配(allocate)

分配一个未分配的分片到指定节点。可以指定索引名和分片号。node参数指定分配到那个节点。allow_primary参数可以强制分配主分片,不过这样可能导致数据丢失。我一般用来清空某个未分配分片的数据的时候才设置这个参数

集群索引中可能由多个分片构成,并且每个分片可以拥有多个副本,将一个单独的索引分为多个分片,可以处理不能在单一服务器上运行的大型索引.

由于每个分片有多个副本,通过副本分配到多个服务器,可以提高查询的负载能力.

为了进行分片和副本操作,需要确定将这些分片和副本放到集群节点的哪个位置,需要确定把每个分片和副本分配到哪台服务器/节点上.

1.索引创建&指定节点参数:

POST /filebeate-ali-hk-fd-tss1

PUT /filebeat-ali-hk-fd-tss1/_settings
{
"index.routing.allocation.include.zone":"ali-hk-ops-elk1"
}

将索引指定存放在elk1的节点上

PUT /filebeat-ali-hk-fd-tss1/settings
{
"index.routing.allocation.include._ip":"ip_addr1,ip_addr2"
}

根据ip地址指定索引的分配节点

2.排除索引分配的节点

POST /filebeat-ali-hk-fd-tss2

PUT /filebeat-ali-hk-fd-tss2/_setting
{
"index.routing.allocation.exclude.zone":"ali-hk-ops-elk2"
}

PUT /filebeat-ali-hk-fd-tss2/_setting
{
"index.routing.allocation.exclude._ip":"ip_addr1,ip_addr2"
}

根据ip地址排除索引分配的节点

3.每个节点上分片和副本数量的控制:

对一个索引指定每个节点上的最大分片数量:

PUT /filebeat-ali-hk-fd-tss1/_settings
{
"index.routing.allocation.total_shards_per_node":1
}

如果配置不当,导致主分片无法分配的话,集群就会处于red状态.

4.手动移动分片和副本:

移动分片:

POST /_cluster/reroute
{
  "commands": [
    {
      "move": {
        "index": "filebeat-ali-hk-fd-tss1",
        "shard": 1,
        "from_node": "ali-hk-ops-elk1",
        "to_node": "ali-hk-ops-elk2"
      }
    }
  ]
}

取消分片:

POST /_cluster/reroute
{
  "commands": [
    {
      "cancel": {
        "index": "filebeat-ali-hk-fd-tss1",
        "shard": 1,
        "node": "ali-hk-ops-elk1"
      }
    }
  ]
}

分配分片(用来分配未分配状态的分片,会导致数据丢失):

POST /_cluster/reroute
{
  "commands": [
    {
      "allocate": {
        "index": "filebeat-ali-hk-fd-tss1",
        "shard": 1,
        "node": "ali-hk-ops-elk1",
        "allow_primary": true (允许该分片做主分片)
      }
    }
  ]
}

将某个未分配的索引手动分配到某个节点上.

POST /_cluster/reroute
{
  "commands": [
    {
      "move": {
        "index": "filebeat-ali-hk-fd-tss1",
        "shard": 1,
        "from_node": "ali-hk-ops-elk1",
        "to_node": "ali-hk-ops-elk2"
      }
    },
    {
      "cancel": {
        "index": "filebeat-ali-hk-fd-tss1",
        "shard": 1,
        "node": "ali-hk-ops-elk1"
      }
    },
    {
      "allocate": {
        "index": "filebeat-ali-hk-fd-tss1",
        "shard": 1,
        "node": "ali-hk-ops-elk1"
      }
    }
  ]
}

5.关于unassigned shards的问题解决:

  1. 出现大量的unassigned shards
  2. 集群的状态为:red

集群状态:red-->存在不可用的主分片

A:fix unassigned shards:
查看所有分片的状态:

$curl -XGET 'http://localhost:9200/_cat/shards'

查询所有unassigned的分片:

$curl -XGET 'http://localhost:9200/_cat/shards' | grep UNASSIGNED

B:查询得到master节点的唯一标识:

$curl -XGET 'http://localhost:9200/_nodes/process?pretty=true'

C:执行route对unassigned的索引进行手动分片:

for index in (curl -XGET 'http://192.168.12.133:9200/_cat/shards' | grep UNASSIGNED|awk '{print $1}') 

do 

      for shards in (curl -XGET 'http://localhost:9200/_cat/shards'|grep UNASSIGNED|awk '{print $2}')

      do

           curl XPOST 'http://localhost:9200/_cluster/reroute'-d '{

             "commands":[

               {

                  "allocate":{

                  "index":index,"shards":shards,

                  "node":"ali-k-ops-elk1",

                  "allow_primary":"true"

                }

              }

             ]     

          }'

      done

done

即使爬到最高的山上,一次也只能脚踏实地地迈一步。