当前位置: 代码网 > it编程>游戏开发>ar > 使用ElasticSearch集群搭建步骤

使用ElasticSearch集群搭建步骤

2024年10月15日 ar 我要评论
一、前言elasticsearch 是一个开源的分布式搜索和分析引擎,用于全文搜索、结构化搜索、分析和可视化大规模数据。它被设计成一个可扩展的实时搜索引擎,能够处理大规模数据,并提供快速的搜索和分析功

一、前言

elasticsearch 是一个开源的分布式搜索和分析引擎,用于全文搜索、结构化搜索、分析和可视化大规模数据。

它被设计成一个可扩展的实时搜索引擎,能够处理大规模数据,并提供快速的搜索和分析功能。

以下是一些 elasticsearch 的特性和用途:

  • 全文搜索:elasticsearch可以存储大量的数据,并提供基于全文搜索的查询功能,支持各种复杂的查询操作。
  • 实时性:elasticsearch能够实时索引数据,并且查询的响应时间非常快,适用于需要快速查询的场景。
  • 分布式架构:elasticsearch 是一个分布式系统,数据被分散存储在多个节点上,可以水平扩展以处理大规模数据。
  • 多种数据类型支持:elasticsearch 支持多种数据类型的索引和查询,包括文本、数字、日期等。
  • 强大的查询语言:elasticsearch 提供了丰富而灵活的查询语言,可以满足各种复杂的查询需求。
  • 实时数据分析:除了搜索功能,elasticsearch 还提供了强大的数据分析功能,可以对数据进行聚合、统计和可视化分析。
  • 易于部署和管理:elasticsearch 提供了简单易用的api和管理工具,可以方便地部署、监控和管理集群。
  • 与其他工具集成:elasticsearch 与许多工具和技术(如logstash、kibana、beats等)集成,构建了强大的日志管理和数据分析解决方案。

总的来说,elasticsearch是一个功能强大且灵活的搜索和分析引擎,适用于各种场景,包括日志分析、实时监控、全文搜索等。

本例将在3台机器上搭建 elasticsearch 集群:

192.168.72.151  node-1
192.168.72.152  node-2
192.168.72.153  node-3

二、使用 rpm 安装 elasticsearch

导入 elasticsearch gpg 密钥

下载并安装公共签名密钥

rpm --import https://artifacts.elastic.co/gpg-key-elasticsearch

从 rpm 存储库安装

在 /etc/yum.repos.d/ 创建 elasticsearch.repo

[elasticsearch]
name=elasticsearch repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/gpg-key-elasticsearch
enabled=0
autorefresh=1
type=rpm-md

执行 yum install 命令

yum install --enablerepo=elasticsearch elasticsearch -y

默认生成的目录

  • 安装目录:/usr/share/elasticsearch
  • 配置文件目录:/etc/elasticsearch

启动 elasticsearch 命令:

systemctl enable elasticsearch.service
systemctl start elasticsearch.service

三、设置基本安全性

首次启动 elasticsearch 时,会为用户生成密码,并自动为用户配置 tls ,可以随时调整 tls 配置,更新节点证书

生成证书

1、在任意节点上进入 elasticsearch 的安装目录,使用 elasticsearch-certutil 为集群生成 ca 。

bin/elasticsearch-certutil ca
  • 设置 ca 文件名(默认即可)
  • 为 ca 设置密码

2、利用上一步的 elastic-stack-ca.p12 为集群生成证书和私钥。

bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
  • 输入ca的密码
  • 为证书创建密码,并设置文件名(默认即可)

3、将证书拷贝到其他节点上。

使用tls加密节点间通信

1、进入 elasticsearch 配置目录,编辑 elasticsearch.conf 文件:

cluster.name: my-cluster
node.name: node-1
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate 
xpack.security.transport.ssl.client_authentication: required
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12

2、执行以下命令将证书密码保存到 elasticsearch 的 keystore

bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password

3、为集群中的每个节点完成前面的步骤。

4、在集群的每个节点上,重新启动 elasticsearch 。

四、为 elasticsearch 加密 http 客户端通信

1、 在任意单个节点上,从安装 elasticsearch 的目录中,运行elasticsearch http 证书工具生成证书签名请求(csr)。

bin/elasticsearch-certutil http
  • 是否生成 csr ,输入 n 。
  • 是否使用现有 ca ,输入 y 。
  • 输入 ca 的路径。这是 elastic-stack-ca.p12 的绝对路径。
  • 输入 ca 的密码。
  • 输入证书的有效期。
  • 是否为每个节点生成一个证书,输入 y 。
  • 输入每个节点的名称(node.name)。
  • 输入所有节点的主机名、ip地址。
  • 输入私钥密码。

该命令生成一个 .zip 文件,其中包含 elasticsearch 和 kibana 使用的证书和密钥。每个文件夹都包含一个 readme.txt ,说明如何使用这些文件。

2、解压缩生成的 elasticsearch-ssl-http.zip 文件。这个压缩文件包含用于elasticsearch和kibana的目录。

/elasticsearch
|_ readme.txt
|_ http.p12
|_ sample-elasticsearch.yml
/kibana
|_ readme.txt
|_ elasticsearch-ca.pem
|_ sample-kibana.yml

3、在集群中的每个节点上,完成以下步骤:

复制上面 elasticsearch 文件夹中的 http.p12 到 elasticsearch 的配置目录下。

编辑 elasticsearch.yml ,启用 https 安全性,并指定 http.12 文件的位置。

xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: http.p12

将私钥密码添加到 elasticsearch 的安全设置中。

bin/elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password

4、重新启动 elasticsearch

五、配置集群

编辑 elasticsearch.yml

3个节点的配置文件除了ip、节点名之外,都一样

cluster.name: my-clusters
node.name: node-1

path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch

indices.memory.index_buffer_size: 20%
indices.memory.min_index_buffer_size: 96mb
thread_pool:
  search:
    size: 32
  analyze:
    size: 30
    queue_size: 1000
indices.requests.cache.size: 2%
indices.queries.cache.size: 10%
indices.fielddata.cache.size: 20%
indices.breaker.fielddata.limit: 40%

network.host: 0.0.0.0
http.port: 9200
transport.port: 9300
transport.compress: true
http.max_content_length: 100mb

discovery.seed_hosts: ["192.168.72.151", "192.168.72.152","192.168.72.153"]

cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
cluster.fault_detection.leader_check.interval: 5s
discovery.cluster_formation_warning_timeout: 10s
cluster.publish.timeout: 30s
cluster.routing.allocation.node_initial_primaries_recoveries: 16
cluster.routing.allocation.node_concurrent_recoveries: 8
indices.recovery.max_bytes_per_sec: 125mb

集群默认是通过9300端口连接的,记得防火墙还要开通9300

启动集群

依次启动各个节点

service elasticsearch start

设置密码

bin/elasticsearch-setup-passwords interactive

检查集群状态

curl  -xget --user elastic:password http://192.168.72.151:9200/_cluster/health?pretty

也可以在浏览器直接访问

请注意,上述步骤中的ip地址、端口和目录路径应根据你的实际环境进行调整。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com