[Elasticsearch]4.可伸缩性解密:集群、节点和分片

2024-04-04 00:38

本文主要是介绍[Elasticsearch]4.可伸缩性解密:集群、节点和分片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

可伸缩性解密:集群、节点和分片

更新连载中…请关注

Scalability and resilience: clusters,nodes, and shard

Elasticsearch支持根据需要进行扩缩容.这得益于Elasticsearch是原生支持分布式的.可以通过往机器中添加服务器(节点)的方式扩大集群容量从而存储更多数据.Elasticsearch会自动的均一些数据和计算任务给新加入的数据.甚至不需要应用程序参与,Elasticsearch完全知道该怎么把数据均衡到多个节点并且提供良好的可伸缩性和高可用性.集群的节点越多这种操作越顺滑越无感. 就是这么丝滑,堪比丝袜!

Elasticsearch is built to be always available and to scale with your needs. It does this by being distributed by nature. You can add servers (nodes) to a cluster to increase capacity and Elasticsearch automatically distributes your data and query load across all of the available nodes. No need to overhaul your application, Elasticsearch knows how to balance multi-node clusters to provide scale and high availability. The more nodes, the merrier.

为什么能这么丝滑呢?用的什么配方?爆开揉碎一探究竟,在Elasticsearch的内部索引其实是包含一个或多个物理存储分片的逻辑组。每个分片都有它自己的索引数据.Elasticsearch就是通过把索引中的文档划分到多个分片(shard)中,再把划分的分片分到多个节点来实现索引的分布式存储的.当机器发生扩容或缩容时,Elasticsearch自动迁移重新均衡分片,这样就能保证在磁盘损坏或通过新加服务器(节点)扩容时依然可以正常提供服务.

How does this work? Under the covers, an Elasticsearch index is really just a logical grouping of one or more physical shards, where each shard is actually a self-contained index. By distributing the documents in an index across multiple shards, and distributing those shards across multiple nodes, Elasticsearch can ensure redundancy, which both protects against hardware failures and increases query capacity as nodes are added to a cluster. As the cluster grows (or shrinks), Elasticsearch automatically migrates shards to rebalance the cluster.

等等!好像哪里不对,扩容好说,磁盘损坏了上面的分片不都坏了,还能提供正常服务?按读书少,你可不要骗俺啊?

这就要说下分片的类型了,其实有俩种类型的分片: 主分片和副分片(备用分片).在索引中的每个文档隶属于一个主分片.副分片就是主分片的备份.

副分片:

其实我就是传说中的备胎,咿呀咿呀哟!

主分片和副分片通常是不在一个磁盘上的,当发生磁盘损坏时,磁盘上的主分片对应的副分片也就转正了,这样就解释了为什么当磁盘损坏时Elasticsearch仍能提供服务了.另外备胎,不!是副分片也能提供读服务,这样就提高了集群的读取文档的吞吐量.(因为同时可以有多台机器提供文档读取服务)

There are two types of shards: primaries and replicas. Each document in an index belongs to one primary shard. A replica shard is a copy of a primary shard. Replicas provide redundant copies of your data to protect against hardware failure and increase capacity to serve read requests like searching or retrieving a document.

索引的主分片数量需要在创建索引的时候指定,创建后就不能修改了。但副分片的数量在索引创建后还是可以修改地.而且修改副分片数量不会影响正在执行的索引和查询操作. 这就是备胎的份量啊!

The number of primary shards in an index is fixed at the time that an index is created, but the number of replica shards can be changed at any time, without interrupting indexing or query operations.

分片如此重要,怎么设置分片大小和数量呢?

It depands:

设置索引的分片大小和主分片数量时,有些我们需要权衡取舍地方:

分片越多维护这些分片的开销就越大,分片越大再进行数据均衡时需要移动的数据也越多耗费的时间也越多.

鱼和熊掌不可兼得

There are a number of performance considerations and trade offs with respect to shard size and the number of primary shards configured for an index. The more shards, the more overhead there is simply in maintaining those indices. The larger the shard size, the longer it takes to move shards around when Elasticsearch needs to rebalance a cluster.

查询的分片越小查询每个分片用的时间越少,但也意味需要更多的查询,查询越多消耗也越大,所以有时候分区大点分区数量少点查询可能反而更快.总之一句话,你自己看着办吧.

Querying lots of small shards makes the processing per shard faster, but more queries means more overhead, so querying a smaller number of larger shards might be faster. In short…it depends.

这里给出点建议,仅供参考:

  • 控制分片大小在GB到数十GB.对于时序数据通常可以控制20GB到40GB.

  • Aim to keep the average shard size between a few GB and a few tens of GB. For use cases with time-based data, it is common to see shards in the 20GB to 40GB range.

  • 避免分片过多,一个节点可以容纳的分片数与可用堆空间成正比.一般来说,每GB堆空间的分片数不应大于20.

  • Avoid the gazillion shards problem. The number of shards a node can hold is proportional to the available heap space. As a general rule, the number of shards per GB of heap space should be less than 20.

最好的确定分区大小和数量的方式就是在你的应用场景下使用数据和查询测试下.

The best way to determine the optimal configuration for your use case is through testing with your own data and queries.

容灾

In case of disaster

为了保证良好的性能,集群中的节点需要放在同一个网络环境中,因为跨数据中心移动迁移分片会耗费比较长的时间.但是为了高可用,我们又不能把所有鸡蛋都放在一个篮子里.怎么做到当一个数据中心发生重大故障另一个数据中心能够及时的接管,甚至让用户都感觉不到发生了故障呢?可以使用跨集群副本功能也就是CCR.

For performance reasons, the nodes within a cluster need to be on the same network. Balancing shards in a cluster across nodes in different data centers simply takes too long. But high-availability architectures demand that you avoid putting all of your eggs in one basket. In the event of a major outage in one location, servers in another location need to be able to take over. Seamlessly. The answer? Cross-cluster replication (CCR).

跨集群副本可以自动热备份主集群的索引数据到第二集群.如果主集群挂了,第二集群继续对外提供服务.第二集群也可以提供只读的服务这样就可以优选与用户距离近的集群提供服务了.

CCR provides a way to automatically synchronize indices from your primary cluster to a secondary remote cluster that can serve as a hot backup. If the primary cluster fails, the secondary cluster can take over. You can also use CCR to create secondary clusters to serve read requests in geo-proximity to your users.

跨集群副本是主从机制.在主集群上的是主,处理所有的写请求。在第二集群上的是从,只提供读服务(只处理读请求).

Cross-cluster replication is active-passive. The index on the primary cluster is the active leader index and handles all write requests. Indices replicated to secondary clusters are read-only followers.

运维

Care And feeding

像其它企业应用系统一样,我们也需要对Elasticsearch进行安全管理和监控.可以通过Kibana提供的控制中心管理、监控Elasticsearch.比如数据回滚、管理索引生命周期啊什么的.

As with any enterprise system, you need tools to secure, manage, and monitor your Elasticsearch clusters. Security, monitoring, and administrative features that are integrated into Elasticsearch enable you to use Kibana as a control center for managing a cluster. Features like data rollups and index lifecycle management help you intelligently manage your data over time

这篇关于[Elasticsearch]4.可伸缩性解密:集群、节点和分片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/874395

相关文章

Redis分片集群的实现

《Redis分片集群的实现》Redis分片集群是一种将Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一... 目录1. Redis Cluster的核心概念哈希槽(Hash Slots)主从复制与故障转移2.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下

Java操作ElasticSearch的实例详解

《Java操作ElasticSearch的实例详解》Elasticsearch是一个分布式的搜索和分析引擎,广泛用于全文搜索、日志分析等场景,本文将介绍如何在Java应用中使用Elastics... 目录简介环境准备1. 安装 Elasticsearch2. 添加依赖连接 Elasticsearch1. 创

centos7基于keepalived+nginx部署k8s1.26.0高可用集群

《centos7基于keepalived+nginx部署k8s1.26.0高可用集群》Kubernetes是一个开源的容器编排平台,用于自动化地部署、扩展和管理容器化应用程序,在生产环境中,为了确保集... 目录一、初始化(所有节点都执行)二、安装containerd(所有节点都执行)三、安装docker-

如何在一台服务器上使用docker运行kafka集群

《如何在一台服务器上使用docker运行kafka集群》文章详细介绍了如何在一台服务器上使用Docker运行Kafka集群,包括拉取镜像、创建网络、启动Kafka容器、检查运行状态、编写启动和关闭脚本... 目录1.拉取镜像2.创建集群之间通信的网络3.将zookeeper加入到网络中4.启动kafka集群

Nacos集群数据同步方式

《Nacos集群数据同步方式》文章主要介绍了Nacos集群中服务注册信息的同步机制,涉及到负责节点和非负责节点之间的数据同步过程,以及DistroProtocol协议在同步中的应用... 目录引言负责节点(发起同步)DistroProtocolDistroSyncChangeTask获取同步数据getDis

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r