前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch shard 分配感知

Elasticsearch shard 分配感知

作者头像
HLee
修改2021-09-16 10:04:45
1.9K0
修改2021-09-16 10:04:45
举报
文章被收录于专栏:房东的猫

简介

官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.2/allocation-awareness.html

您可以将自定义节点属性用作感知属性,以使 Elasticsearch 在分配分片时考虑物理硬件配置。 如果 Elasticsearch 知道哪些节点在同一台物理服务器上,在同一机架中或在同一区域中,则它可以分发主分片及其副本分片,以最大程度地减少发生故障时丢失所有分片副本的风险。

You can use custom node attributes asawareness attributesto enable Elasticsearch to take your physical hardware configuration into account when allocating shards. If Elasticsearch knows which nodes are on the same physical server, in the same rack, or in the same zone, it can distribute the primary shard and its replica shards to minimise the risk of losing all shard copies in the event of a failure.

通过 cluster.routing.allocation.awareness.attributes 设置启用分片分配感知后,分片仅分配给已为指定感知属性设置值的节点。 如果您使用多个感知属性,那么 Elasticsearch 在分配分片时会分别考虑每个属性。

可以在 elasticsearch.yml 中配置分配感知设置,并使用 cluster-update-settings API 动态更新。默认情况下,Elasticsearch 使用 adaptive replica selection 来路由搜索或GET请求。 但是,由于存在分配感知属性,Elasticsearch将更喜欢在相同位置(具有相同的感知属性值)使用分片来处理这些请求。

When shard allocation awareness is enabled with thecluster.routing.allocation.awareness.attributessetting, shards are only allocated to nodes that have values set for the specified awareness attributes. If you use multiple awareness attributes, Elasticsearch considers each attribute separately when allocating shards.

The allocation awareness settings can be configured in elasticsearch.yml and updated dynamically with the cluster-update-settings API.

Elasticsearch prefers using shards in the same location (with the same awareness attribute values) to process search or GET requests. Using local shards is usually faster than crossing rack or zone boundaries.

不均衡的 shard 分布

假设您的硬件分布于两个不同的物理机架中:

相同节点在同一台机器
相同节点在同一台机器

在上面我们可以看到:我们的 my_index 的 shard 分布于两个不同的物理机架中 rack1 及 rack2。在上面我们可以看出来 P0 及 R0 分布于 rack1 中,而 P1 和 R1 分布于 rack2 中。假如有一种情况我们的 rack1 或者 rack2 由于某种事故从而导致它们其中的一个不可用,那么我们的 my_index 将导致不可用。这是因为分片0或分片1将不存在。

相同节点且主副分片在同一台机器
相同节点且主副分片在同一台机器

为了避免这种情况我们可以让我们的 Elasticsearch 知道我们的硬件的物理分配。这个在 Elasticsearch 中称之为 shard allocation awareness。这种解决方案非常实用于当我们的 Elasticsearch 的多个 node 分享同样的资源:disk,host mache,netowork switch,rack 等。

我们可以通过下面的两个步骤来进行配置:

  1. 对我们的 node 打上标签
  2. 更新我们的 cluster 配置

Step1:对 node 打上标签

我们可以使用 node.attr 来对我们的 node 进行打上标签。

1. Specify the location of each node with a custom node attribute. For example, if you want Elasticsearch to distribute shards across different racks, you might set an awareness attribute calledrack_idin each node’selasticsearch.ymlconfig file.

代码语言:javascript
复制
node.attr.rack_id: rack_one

2. You can also set custom attributes when you start a node:

代码语言:javascript
复制
`./bin/elasticsearch -Enode.attr.rack_id=rack_one`

Step2: 对 cluster 进行配置

我们必须告诉 Elasticsearch 我们使用哪个属性或哪些属性用于我们的 Shard allocation awareness。我们可以通过使用 cluster.routing.allocation.awareness 这个 cluster 级的配置来告诉我们的 Elasticsearch:

Tell Elasticsearch to take one or more awareness attributes into account when allocating shards by settingcluster.routing.allocation.awareness.attributesineverymaster-eligible node’selasticsearch.ymlconfig file.

代码语言:javascript
复制
PUT _cluster/settings
{
  "persistent": {
    "cluster.routing.allocation.awareness.attributes": "my_rack_id"
  }
}

You can also use the cluster-update-settings API to set or update a cluster’s awareness attributes.

均衡的 Shard 分布

经过上面的 step1 及 step2 的配置后,现在,你可以确保所有分片至少有一个副本将存在于每个索引的每个机架中。

如果在 rack1 或 rack2 其中的一个 rack 在损坏的情况下,我们可以确保我们的数据访问是不间断的。当然如果两个机架同时都被损坏,那么我们也无能为力了。

强迫感知 (forced awareness)

默认情况下,如果一个位置失败,Elasticsearch 会将所有丢失的副本分片分配给其余位置。 尽管你可能在所有位置上都有足够的资源来承载您的主和副本分片,但单个位置可能无法承载所有分片。

By default, if one location fails, Elasticsearch assigns all of the missing replica shards to the remaining locations. While you might have sufficient resources across all locations to host your primary and replica shards, a single location might be unable to hostALLof the shards.

为了防止发生故障时单个位置过载,可以设置 cluster.routing.allocation.awareness.force,以便在其他位置的节点可用之前,不分配任何副本。

To prevent a single location from being overloaded in the event of a failure, you can setcluster.routing.allocation.awareness.forceso no replicas are allocated until nodes are available in another location.

例如,如果你有一个名为 my_rack_id 的感知属性并在 rack1 和 rack2 中配置了节点,则可以使用强制感知来防止 Elasticsearch 在只有一个区域可用的情况下分配副本:

For example, if you have an awareness attribute calledzoneand configure nodes inzone1andzone2, you can use forced awareness to prevent Elasticsearch from allocating replicas if only one zone is available:

代码语言:javascript
复制
cluster.routing.allocation.awareness.attributes: "my_rack_id"
cluster.routing.allocation.awareness.force.zone.values: "rack1,rack2"

使用此示例配置,如果你启动两个节点并将 node.attr.my_rack_id 设置为 rack1 并创建具有5个分片和1个副本的索引,Elasticsearch 将创建索引并分配5个主分片,但不分配副本。 仅当将 node.attr.my_rack_id 设置为 rack2 的节点可用时,才分配副本。

With this example configuration, if you start two nodes withnode.attr.zoneset tozone1and create an index with 5 shards and 1 replica, Elasticsearch creates the index and allocates the 5 primary shards but no replicas. Replicas are only allocated once nodes withnode.attr.zoneset tozone2are available.

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 不均衡的 shard 分布
    • Step1:对 node 打上标签
      • Step2: 对 cluster 进行配置
      • 均衡的 Shard 分布
      • 强迫感知 (forced awareness)
      相关产品与服务
      Elasticsearch Service
      腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档