全栈开发之ElasticSearch8.0分布式搜索引擎集群及其高可用测试

简介: 全栈开发之ElasticSearch8.0分布式搜索引擎集群及其高可用测试

背景


如今人们的行为在网络中的各类系统中留下了清晰的足迹,各行各业的数据都在源源不断地产生着:收集的日志数据越积越多,用户的订单数据越来越多,网络爬取得到的数据不断增长,在这些可能用到搜索引擎的场景中,将搜索服务升级为集群以保证高可用是不得不面临的一个操作。好在 ElasticSearch 本身具备极为强大的横向扩展能力,所以这项工作操作起来并不太难。


学习一项新技术,实践是最好的方式,这不, ElasticSearch 8.0来了;今天就来试试 ElasticSearch 的分布式集群搭建,并验证其高可用。

image.png


系统环境


在三台虚拟主机上,搭建3个节点的 ElasticSearch 的搜索引擎集群,三台主机配置相同,主机信息如下:

[root@hadoop1 local]# uname -a
Linux hadoop1 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@hadoop1 local]# cat /proc/version
Linux version 3.10.0-1127.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Mar 31 23:36:51 UTC 2020
[root@hadoop1 local]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
内存:4G
处理器:2*2
硬盘:100G

Note:三台主机的名称分别为 hadoop1hadoop2hadoop3


集群配置


ElasticSearch 8.0的集群配置相对更方便一些,只需要将第一个实例上生成的 enrollment token 附带上,在后续的两个节点上执行启动命令即可。


其实在第一个 ElasticSearch 实例运行成功后,控制台输出了以下内容:

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✅ Elasticsearch security features have been automatically configured!
✅ Authentication is enabled and cluster connections are encrypted.
Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
  9SWTTFDuibtaS2*L0NRv
HTTP CA certificate SHA-256 fingerprint:
  75480a9fc93649e2ebd8dd0a9f0721247e8cff32fdbc78abf0b30d7ac9c8e8bd
Configure Kibana to use this cluster:
• Run Kibana and click the configuration link in the terminal when Kibana starts.
• Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):
  eyJ2ZXIiOiI4LjAuMCIsImFkciI6WyIxOTIuMTY4LjQ0LjEyNzo5MjAwIl0sImZnciI6Ijc1NDgwYTlmYzkzNjQ5ZTJlYmQ4ZGQwYTlmMDcyMTI0N2U4Y2ZmMzJmZGJjNzhhYmYwYjMwZDdhYzljOGU4YmQiLCJrZXkiOiJWYjg0RW44QnVQdHBqaU9BTUgxZjpnNFpjeUFDWVJYQ2xLRVp2eXF3U3RBIn0=
Configure other nodes to join this cluster:
• On this node:
  ⁃ Create an enrollment token with `bin/elasticsearch-create-enrollment-token -s node`.
  ⁃ Uncomment the transport.host setting at the end of config/elasticsearch.yml.
  ⁃ Restart Elasticsearch.
• On other nodes:
  ⁃ Start Elasticsearch with `bin/elasticsearch --enrollment-token <token>`, using the enrollment token that you generated.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

其中,写明了构建集群的步骤:


  • 在第一个实例上的操作:


  1. 先在第一个实例上创建enrollment token
  2. 将配置文件config/elasticsearch.yml中的transport.host取消注释;
  3. 重启当前实例;


  • 在后续节点上执行启动命令,附带enrollment token: bin/elasticsearch --enrollment-token <token>


至此,集群配置完成。

image.png

  • 完整搭建过程


在三个节点上执行以下命令完成,下载、安装、启动。

[root@hadoop1 ~]# cd /usr/local/
[root@hadoop1 local]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.0.0-linux-x86_64.tar.gz
[root@hadoop1 local]# tar -xvf elasticsearch-8.0.0-linux-x86_64.tar.gz
[root@hadoop1 local]# mv elasticsearch-8.0.0 elasticsearch
[root@hadoop1 local]# cd elasticsearch
# 添加用户es-admin
[root@hadoop1 elasticsearch]# useradd es-admin
# 赋予对elasticsearch目录的权限
[root@hadoop1 elasticsearch]# chown -R es-admin:es-admin /usr/local/elasticsearch
# 切换至es-admin
[root@hadoop1 elasticsearch]# su es-admin
# 切换至root
[es-admin@hadoop1 elasticsearch]$ exit
exit
# 编辑vi /etc/security/limits.conf,在最后添加以下内容,注意要退出root用户重新登录后才会生效
[root@hadoop1 elasticsearch]# vi /etc/security/limits.conf
*               soft    nofile          65536
*               hard    nofile          65536
# 编辑vi /etc/sysctl.conf,在最后添加:
[es-admin@hadoop1 elasticsearch]$ vi /etc/sysctl.conf
vm.max_map_count = 262144
# 记得使用sysctl -p刷新配置文件
[root@hadoop1 elasticsearch]# sysctl -p
# 切换到es-admin用户
[root@hadoop1 elasticsearch]# su es-admin
# 尝试启动ElasticSearch
[es-admin@hadoop1 elasticsearch]$ ./bin/elasticsearch
  • 后续节点加入过程
# 在第一个实例上执行获取token
[root@hadoop1 elasticsearch]# ./bin/elasticsearch-create-enrollment-token -s node
warning: ignoring JAVA_HOME=/usr/local/jdk; using bundled JDK
eyJ2ZXIiOiI4LjAuMCIsImFkciI6WyIxOTIuMTY4LjQ0LjEyNzo5MjAwIl0sImZnciI6Ijc1NDgwYTlmYzkzNjQ5ZTJlYmQ4ZGQwYTlmMDcyMTI0N2U4Y2ZmMzJmZGJjNzhhYmYwYjMwZDdhYzljOGU4YmQiLCJrZXkiOiJYS2JXRlg4QlQyWXhuck1FTlRpdTpyeW9qSF92T1MzNnNOQ05ZQUh5d0pRIn0=
# 传输到第2台服务器hadoop2
[root@hadoop1 local]# scp elasticsearch-8.0.0-linux-x86_64.tar.gz root@hadoop2:/usr/local/
Warning: Permanently added the ECDSA host key for IP address '192.168.44.130' to the list of known hosts.
elasticsearch-8.0.0-linux-x86_64.tar.gz       100%  491MB  68.1MB/s   00:07 
# 传输到第3台服务器hadoop3
[root@hadoop1 local]# scp elasticsearch-8.0.0-linux-x86_64.tar.gz root@hadoop3:/usr/local/
# 在后续实例上,第一次启动时,附带token
[root@hadoop2 elasticsearch]# su es-admin
[es-admin@hadoop2 elasticsearch]$ ./bin/elasticsearch --enrollment-token eyJ2ZXIiOiI4LjAuMCIsImFkciI6WyIxOTIuMTY4LjQ0LjEyNzo5MjAwIl0sImZnciI6Ijc1NDgwYTlmYzkzNjQ5ZTJlYmQ4ZGQwYTlmMDcyMTI0N2U4Y2ZmMzJmZGJjNzhhYmYwYjMwZDdhYzljOGU4YmQiLCJrZXkiOiJYS2JXRlg4QlQyWXhuck1FTlRpdTpyeW9qSF92T1MzNnNOQ05ZQUh5d0pRIn0=


高可用测试


通过以下两个接口确定集群的状态(Note:以下是三个节点构成的集群的状态信息),在测试之前可以导入部分数据更直观地测试读、写可用性。

image.png

192.168.44.130 32 92  5 0.10 0.33 0.32 cdfhilmrstw - hadoop2
192.168.44.127 40 94 11 0.35 0.49 0.53 cdfhilmrstw * hadoop1
192.168.44.132 47 97  4 0.21 0.20 0.23 cdfhilmrstw - hadoop3

结果中:*代表 master 节点;-代表 slave 节点。


<!-- https://hadoop1:9200/_cat/health -->
1645340882 07:08:02 elasticsearch green 3 3 26 13 0 0 0 0 - 100.0%

// https://hadoop1:9200/_cluster/health
{
    "cluster_name": "elasticsearch",
    "status": "green",
    "timed_out": false,
    "number_of_nodes": 3,
    "number_of_data_nodes": 3,
    "active_primary_shards": 13,
    "active_shards": 26,
    "relocating_shards": 0,
    "initializing_shards": 0,
    "unassigned_shards": 0,
    "delayed_unassigned_shards": 0,
    "number_of_pending_tasks": 0,
    "number_of_in_flight_fetch": 0,
    "task_max_waiting_in_queue_millis": 0,
    "active_shards_percent_as_number": 100.0
}

结果中:关注颜色状态green : 有完整的主版本和副本,一切正常;yellow : 数据完整,但是没有完整的副本,相当于警告;red : 数据不完整,部分主分片不可用,相当于故障;


停掉一个从节点


这里我们停掉第3个节点,即 hadoop3 上的 ElasticSearch 服务,查看集群状态,此时变为2个节点的集群,状态为 yellow



192.168.44.127 38 81 4 0.11 0.32 0.43 cdfhilmrstw * hadoop1
192.168.44.130  7 92 3 0.11 0.28 0.29 cdfhilmrstw - hadoop2
1645341764 07:22:44 elasticsearch yellow 2 2 18 13 0 0 8 0 - 69.2%


再停掉一个从节点


这里我们继续停掉第2个节点,即 hadoop2 上的 ElasticSearch 服务,查看集群状态,此时变为单节点的集群,状态为未连接,即不可用。


{
    "error": {
        "root_cause": [
            {
                "type": "master_not_discovered_exception",
                "reason": null
            }
        ],
        "type": "master_not_discovered_exception",
        "reason": null
    },
    "status": 503
}
{
    "error": {
        "root_cause": [
            {
                "type": "master_not_discovered_exception",
                "reason": null
            }
        ],
        "type": "master_not_discovered_exception",
        "reason": null
    },
    "status": 503
}


仅停掉主节点


我们启动停止的 hadoop2hadoop3 上的 ElasticSearch 服务(直接使用 ./bin/elasticsearch 启动服务即可,仅在第一次启动加入集群时需要附带 enrollment token ),先看下状态信息。


192.168.44.130 31 95 8 0.41 0.55 0.39 cdfhilmrstw * hadoop2
192.168.44.132  6 96 6 1.44 0.57 0.32 cdfhilmrstw - hadoop3
192.168.44.127 44 80 4 0.16 0.18 0.27 cdfhilmrstw - hadoop1
1645342672 07:37:52 elasticsearch green 3 3 26 13 0 0 0 0 - 100.0%


我们发现 master 节点发生了变化:由开始的hadoop1变为hadoop2。

那么这时,我们停掉master节点hadoop2进行测试,查看集群状态,此时变为2个节点的集群,状态为 yellow ;另外需要注意的是,群龙不可无首,国不可一日无君,那么一个集群必须要有一个master。


192.168.44.127 24 86 17 0.37 0.88 0.82 cdfhilmrstw * hadoop1
192.168.44.132 37 96 15 0.48 0.65 0.48 cdfhilmrstw - hadoop3
1645344470 08:07:50 elasticsearch yellow 2 2 18 13 0 0 8 0 - 69.2%

我们可以看到 hadoop1 又成了 master ,即当我们停掉原来的 master 后,会自动选举出新的 master


小总结


ElasticSearch 8.0的集群搭建更便捷,只需要将第一个实例上生成的 enrollment token 附带上,在后续的两个节点上执行启动命令即可。


  1. ES集群扩容极其便捷;
  2. 数据分片,可以避免单点故障;
  3. 实现负载均衡,应对高并发场景。
相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
目录
相关文章
|
存储 负载均衡 NoSQL
【赵渝强老师】Redis Cluster分布式集群
Redis Cluster是Redis的分布式存储解决方案,通过哈希槽(slot)实现数据分片,支持水平扩展,具备高可用性和负载均衡能力,适用于大规模数据场景。
804 2
|
Cloud Native 关系型数据库 分布式数据库
登顶TPC-C|云原生数据库PolarDB技术揭秘:Limitless集群和分布式扩展篇
阿里云PolarDB云原生数据库在TPC-C基准测试中以20.55亿tpmC的成绩刷新世界纪录,展现卓越性能与性价比。其轻量版满足国产化需求,兼具高性能与低成本,适用于多种场景,推动数据库技术革新与发展。
|
监控 Linux 应用服务中间件
Linux多节点多硬盘部署MinIO:分布式MinIO集群部署指南搭建高可用架构实践
通过以上步骤,已成功基于已有的 MinIO 服务,扩展为一个 MinIO 集群。该集群具有高可用性和容错性,适合生产环境使用。如果有任何问题,请检查日志或参考MinIO 官方文档。作者联系方式vx:2743642415。
4242 57
|
Cloud Native 关系型数据库 分布式数据库
登顶TPC-C|云原生数据库PolarDB技术揭秘:Limitless集群和分布式扩展篇
云原生数据库PolarDB技术揭秘:Limitless集群和分布式扩展篇
|
并行计算 PyTorch 算法框架/工具
融合AMD与NVIDIA GPU集群的MLOps:异构计算环境中的分布式训练架构实践
本文探讨了如何通过技术手段混合使用AMD与NVIDIA GPU集群以支持PyTorch分布式训练。面对CUDA与ROCm框架互操作性不足的问题,文章提出利用UCC和UCX等统一通信框架实现高效数据传输,并在异构Kubernetes集群中部署任务。通过解决轻度与强度异构环境下的挑战,如计算能力不平衡、内存容量差异及通信性能优化,文章展示了如何无需重构代码即可充分利用异构硬件资源。尽管存在RDMA验证不足、通信性能次优等局限性,但该方案为最大化GPU资源利用率、降低供应商锁定提供了可行路径。源代码已公开,供读者参考实践。
1582 3
融合AMD与NVIDIA GPU集群的MLOps:异构计算环境中的分布式训练架构实践
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
2465 64
|
JSON 安全 数据可视化
Elasticsearch(es)在Windows系统上的安装与部署(含Kibana)
Kibana 是 Elastic Stack(原 ELK Stack)中的核心数据可视化工具,主要与 Elasticsearch 配合使用,提供强大的数据探索、分析和展示功能。elasticsearch安装在windows上一般是zip文件,解压到对应目录。文件,elasticsearch8.x以上版本是自动开启安全认证的。kibana安装在windows上一般是zip文件,解压到对应目录。elasticsearch的默认端口是9200,访问。默认用户是elastic,密码需要重置。
6746 0
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
存储 安全 数据管理
如何在 Rocky Linux 8 上安装和配置 Elasticsearch
本文详细介绍了在 Rocky Linux 8 上安装和配置 Elasticsearch 的步骤,包括添加仓库、安装 Elasticsearch、配置文件修改、设置内存和文件描述符、启动和验证 Elasticsearch,以及常见问题的解决方法。通过这些步骤,你可以快速搭建起这个强大的分布式搜索和分析引擎。
741 5
|
存储 JSON Java
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
这篇文章是关于Elasticsearch的学习指南,包括了解Elasticsearch、版本对应、安装运行Elasticsearch和Kibana、安装head插件和elasticsearch-ik分词器的步骤。
1593 0
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。

热门文章

最新文章