gznz12345 发表于 2019-1-29 09:11:22

Elasticsearch分片&副本分配

  集群索引中可能由多个分片构成,并且每个分片可以拥有多个副本,将一个单独的索引分为多个分片,可以处理不能在单一服务器上运行的
  大型索引.
  由于每个分片有多个副本,通过副本分配到多个服务器,可以提高查询的负载能力.
  为了进行分片和副本操作,需要确定将这些分片和副本放到集群节点的哪个位置,需要确定把每个分片和副本分配到哪台服务器/节点上.
  1.索引创建&指定节点参数:
  $curl -XPOST 'http://localhost:9200/filebeate-ali-hk-fd-tss1'
  $curl -XPUT 'http://localhost:9200/filebeat-ali-hk-fd-tss1/_settings' -d '{
  "index.routing.allocation.include.zone":"ali-hk-ops-elk1"
  }'
  将索引指定存放在elk1的节点上
  $curl -XPUT 'http://localhost:9200/filebeat-ali-hk-fd-tss1/settings' -d '{
  "index.routing.allocation.include._ip":"ip_addr1,ip_addr2"
  }'
  根据ip地址指定索引的分配节点
  2.排除索引分配的节点:
  $curl -XPOST 'http://localhost:9200/filebeat-ali-hk-fd-tss2'
  $curl -XPUT 'http://localhost:9200/filebeat-ali-hk-fd-tss2/_setting' -d '{
  "index.routing.allocation.exclude.zone":"ali-hk-ops-elk2"
  }'
  $curl -XPUT 'http://localhost:9200/filebeat-ali-hk-fd-tss2/_setting' -d '{
  "index.routing.allocation.exclude._ip":"ip_addr1,ip_addr2"
  }'
  根据ip地址排除索引分配的节点
  3.每个节点上分片和副本数量的控制:
  对一个索引指定每个节点上的最大分片数量:
  $curl -XPUT 'http://localhost:9200/filebeat-ali-hk-fd-tss1/_settings' -d '{
  "index.routing.allocation.total_shards_per_node":1
  }'
  如果配置不当,导致主分片无法分配的话,集群就会处于red状态.
  4.手动移动分片和副本:
  移动分片:
  $curl -XPOST 'http://localhost:9200/_cluster/reroute' -d '{
  "commands":[{
  "move":{
  "index":"filebeat-ali-hk-fd-tss1",
  "shard":1,
  "from_node":"ali-hk-ops-elk1",
  "to_node":"ali-hk-ops-elk2"
  }
  }]
  }'
  取消分片:
  $curl -XPOST 'http://localhost:9200/_cluster/reroute' -d '{
  "commands":[{
  "cancel":{
  "index":"filebeat-ali-hk-fd-tss1",
  "shard":1,
  "node":"ali-hk-ops-elk1"
  }
  }]
  }'
  分配分片:
  $curl -XPOST 'http://localhost:9200/_cluster/reroute' -d '{
  "commands":[{
  "allocate":{
  "index":"filebeat-ali-hk-fd-tss1",
  "shard":1,
  "node":"ali-hk-ops-elk1"
  }
  }]
  }'
  将某个未分配的索引手动分配到某个节点上.
  $curl -XPOST 'http://localhost:9200/_cluster/reroute' -d '{
  "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
https://images2015.cnblogs.com/blog/1016907/201703/1016907-20170310142439076-923252001.png
  集群状态: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://localhost:9200/_cat/shards' | grep UNASSIGNED |awk '{print $1}'|sort |uniq):do
  for shards in $(curl -XGET 'http://localhost:9200/_cat/shards' | grep UNASSIGNED | grep $index | awk '{print $2}'|sort|uniq):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
  本文出自https://www.cnblogs.com/kasumi/p/6530535.html
elasticsearch-索引分片和副本设置
索引设置
  你可以通过修改配置来自定义索引行为,详细配置参照 {ref}/index-modules.html[索引模块]
  Tip
Elasticsearch 提供了优化好的默认配置。 除非你理解这些配置的作用并且知道为什么要去修改,否则不要随意修改。  下面是两个 最重要的设置:

[*]  number_of_shards
[*]  每个索引的主分片数,默认值是 5 。这个配置在索引创建后不能修改。
[*]  number_of_replicas
[*]  每个主分片的副本数,默认值是 1 。对于活动的索引库,这个配置可以随时修改。
  例如,我们可以创建只有 一个主分片,没有副本的小索引:
PUT /my_temp_index  
{    "settings": {      "number_of_shards" :   1,      "number_of_replicas" : 0
  
    }
  
}
  然后,我们可以用 update-index-settings API 动态修改副本数:
PUT /my_temp_index/_settings  
{    "number_of_replicas": 1}
  本文出自https://blog.csdn.net/chuan442616909/article/details/55212251


页: [1]
查看完整版本: Elasticsearch分片&副本分配