ES的集群shard平衡

作者: 明翼 | 来源:发表于2018-08-04 11:22 被阅读481次

    1、前言

    最近集群老是挂,数据量多是一个原因,其次我们对shard的路由好像不怎么理解,特此我整理一篇shard的路由分配情况。

    1.1 shard allocation

    集群碎片分配是指将索引碎片分配到其他节点的过程,如下情况:

    • 集群故障恢复
    • 副本分配
    • 索引动态均衡(节点增加或者减少后自动进行)
      都会执行索引碎片的重新分配。
      ES有两个主要的相关配置:
      1)控制平时分配的。
      2)控制动态平衡时候分配规则。

    2、shard分配

    2.1 cluster.routing.allocation.enable

    以下动态设置可用于控制分片和恢复:
    cluster.routing.allocation.enable
    启用或禁用特定种类的分片的分配:

    • all - (默认值)允许为所有类型的分片分配分片。
    • primaries - 仅允许分配主分片的分片。
    • new_primaries - 仅允许为新索引的主分片分配分片。
    • none - 任何索引都不允许任何类型的分片。

    重新启动节点时,此设置不会影响本地主分片的恢复。 如果重新启动的节点具有未分配的主分片的副本,会立即恢复该主分片。
    永久生效说明:
    正式使用去掉注释,注释明确说明含义用的

    PUT _cluster/settings
    { 
      "persistent" :
      { 
         "cluster.routing.rebalance.enable": "none",
           ##允许在一个节点上发生多少并发传入分片恢复。 默认为2。
           ##多数为副本
          "cluster.routing.allocation.node_concurrent_incoming_recoveries":2,
          ##允许在一个节点上发生多少并发传出分片恢复,默认为2.
           ## 多数为主分片
          "cluster.routing.allocation.node_concurrent_outgoing_recoveries":2,
           ##为上面两个的统一简写
          "cluster.routing.allocation.node_concurrent_recoveries":2,
          ##在通过网络恢复副本时,节点重新启动后未分配的主节点的恢复使用来自本地  磁盘的数据。 
         ##这些应该很快,因此更多初始主要恢复可以在同一节点上并行发生。 默认为4。
          "cluster.routing.allocation.node_initial_primaries_recoveries":4,
    ##允许执行检查以防止基于主机名和主机地址在单个主机上分配同一分片的多个实例。 
    ##默认为false,表示默认情况下不执行检查。 此设置仅适用于在同一台计算机上启动多个节点的情况。这个我的理解是如果设置为false,
    ##则同一个节点上多个实例可以存储同一个shard的多个副本没有容灾作用了
       "cluster.routing.allocation.same_shard.host":true
        }
        
    }
    

    2.2 重新平衡分片分配

    cluster.routing.rebalance.enable
    为特定类型的分片启用或禁用重新平衡:

    • all - (默认值)允许各种分片的分片平衡。
    • primaries - 仅允许主分片的分片平衡。
    • replicas - 仅允许对副本分片进行分片平衡。
    • none - 任何索引都不允许任何类型的分片平衡。

    cluster.routing.allocation.allow_rebalance

    • 始终 - 始终允许重新平衡。
    • indices_primaries_active - 仅在所有主分片激活时。
    • indices_all_active - (默认)仅当所有分片都激活时。

    cluster.routing.allocation.cluster_concurrent_rebalance
    允许控制群集范围内允许的并发分片重新平衡数。 默认为2。
    请注意,此设置仅控制由于群集中的不平衡而导致的并发分片重定位数。 此设置不会因分配过滤或强制感知而限制分片重定位。

    2.3 重新平衡系数设置

    curl -XPUT 'http://127.0.0.1:9200/_cluster/settings?pretty=true' -d '{
    "transient": {
    ##默认为 0.45f
    "cluster.routing.allocation.balance.shard" : 0.33,
    ## 0.55f提高会使集群中所有节点上的每个索引的碎片数量趋于相等。
    "cluster.routing.allocation.balance.index": 0.33,
    ##1.0f 提高将导致集群在优化碎片平衡方面不那么积极。
    "cluster.routing.allocation.balance.threshold” : 1
    }
    }’
    

    elasticsearch内部计算公式是:

    weightindex(node, index) = indexBalance * (node.numShards(index) – avgShardsPerNode(index))
    weightnode(node, index) = shardBalance * (node.numShards() – avgShardsPerNode)
    weightprimary(node, index) = primaryBalance * (node.numPrimaries() – avgPrimariesPerNode)
    weight(node, index) = weightindex(node, index) + weightnode(node, index) + weightprimary(node, index)
    

    如果计算最后的weight(node, index)大于threshold, 就会发生shard迁移。

    参考https://blog.csdn.net/an74520/article/details/42871023
    集群节点临时重启
    当修改配置时可能需要重启集群才生效,或者集群发生严重错误无法恢复时都可能需要重启集群
    一个集群节点重启前要先临时禁用自动分配,设置cluster.routing.allocation.enable为none,否则节点停止后,当前节点的分片会自动分配到其他节点上,本节点启动后需要等其他节点RECOVERING后才会RELOCATING,也就是分片在其他节点恢复后又转移回来,浪费大量时间
    首先禁用自动分配
    curl -XPUT http://127.0.0.1:9200/_cluster/settings -d '{
    "transient" : {
    "cluster.routing.allocation.enable" : "none"
    }
    }'
    然后再重启集群
    集群启动后再改回配置
    curl -XPUT http://127.0.0.1:9200/_cluster/settings -d '{
    "transient" : {
    "cluster.routing.allocation.enable" : "all"
    }
    }'

    相关文章

      网友评论

        本文标题:ES的集群shard平衡

        本文链接:https://www.haomeiwen.com/subject/ygwpvftx.html