elasticsearch分片移动操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36666651/article/details/83865606

分片移动触发条件

  • 创建/删除一个Index;
  • 加入/离开一个Node;
  • 手动执行了Reroute命令;
  • 修改了Replica设置;

Allocate策略,明确指定是否允许分片分配到指定Node上,分为index级别和cluster级别
https://www.elastic.co/guide/en/elasticsearch/reference/master/allocation-filtering.html
https://www.elastic.co/guide/en/elasticsearch/reference/2.4/shard-allocation-filtering.html

  • index.routing.allocation.require.{attribute}
  • index.routing.allocation.include{attribute}
  • index.routing.allocation.exclude.{attribute}
  • cluster.routing.allocation.require.{attribute}
  • cluster.routing.allocation.include.{attribute}
  • cluster.routing.allocation.exclude.{attribute}

require表示必须分配到指定node,include表示可以分配到指定node,exclude表示不允许分配到指定Node,cluster的配置会覆盖index级别的配置,比如index include某个node,cluster exclude某个node,最后的结果是exclude某个node,上面{attribute}表示node的匹配方式有:
_name 匹配node名称,多个node名称用逗号隔开
_ip 匹配node ip,多个ip用逗号隔开
_host 匹配node的host name 多个host name用逗号隔开

elasticsearch下线一个节点时,操作执行后,集群会把此node上的分片迁移到其他节点,然后就可以对node进行一些操作,例如关机重启换硬盘等:

# 让_name匹配到对应的node即可 也可以使用_ip _host
# persistent表示永久生效,transient表示暂时生效,重启后无效

PUT _cluster/settings
{
  "transient" : {
    "cluster.routing.allocation.exclude._name" : "node_name"
  }
}

# 当节点重新加入以后,执行下面命令,就可以重新分配分片了
# !!! 这几个规则是叠加生效的,开始以为exclude和include是相反的命令,最后试了半天发现一直无法重新分配分片
# 所以分配规则时不要相互冲突了,不然会出问题,我用2.x实验执行冲突的操作请求很久没有返回
# 主要是require和exclude规则冲突,手动移动分片时,如果与规则产生了冲突也会失败
PUT _cluster/settings
{
  "transient" : {
    "cluster.routing.allocation.exclude._name" : ""
  }
}

分片分配原因解释,会说明为啥分片未被分配或者移动未成功(2.x无效)
https://www.elastic.co/guide/en/elasticsearch/reference/master/cluster-allocation-explain.html

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

# 指定某个分片分析
GET /_cluster/allocation/explain
{
  "index": "myindex",
  "shard": 0,
  "primary": false,
  "current_node": "nodeA"                         
}

# 未分配的
GET /_cluster/allocation/explain

elasticsearch移动分片,move用来分片间移动,allocate用来将未分配分片指定到某个node

# 6.4版本

POST /_cluster/reroute
{
    "commands" : [
        {
            "move" : {
                "index" : "test", "shard" : 0,
                "from_node" : "node1", "to_node" : "node2"
            }
        },
        {
          "allocate_replica" : {
                "index" : "test", "shard" : 1,
                "node" : "node3"
          }
        }
    ]
}

# 2.4版本
curl -XPOST 'localhost:9200/_cluster/reroute' -d '{
    "commands" : [ {
        "move" :
            {
              "index" : "test", "shard" : 0,
              "from_node" : "node1", "to_node" : "node2"
            }
        },
        {
          "allocate" : {
              "index" : "test", "shard" : 1, "node" : "node3"
          }
        }
    ]
}'

将索引从一个集群迁移到另一个集群:

# 6.4版本
# 2.x版本需要先创建索引再迁移 https://www.elastic.co/guide/en/elasticsearch/reference/2.4/docs-reindex.html#reindex-from-remote 而且问题较多,不建议生产上使用
POST _reindex
{
  "source": {
    "remote": {
      "host": "http://oldhost:9200",
      "username": "user",
      "password": "pass"
    },
    "index": "source",
    "query": {
      "match": {
        "test": "data"
      }
    }
  },
  "dest": {
    "index": "dest"
  }
}

猜你喜欢

转载自blog.csdn.net/qq_36666651/article/details/83865606
今日推荐