使用ElasticSearch集群搭建步骤

使用ElasticSearch集群搭建步骤

一、前言

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

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

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

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

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

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

192.168.72.151 node-1192.168.72.152 node-2192.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 packagesbaseurl=https://artifacts.elastic.co/packages/8.x/yumgpgcheck=1gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearchenabled=0autorefresh=1type=rpm-md

执行 yum install 命令

yum install --enablerepo=elasticsearch elasticsearch -y

默认生成的目录

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

启动 Elasticsearch 命令:

systemctl enable elasticsearch.servicesystemctl 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-clusternode.name: node-1xpack.security.enabled: truexpack.security.transport.ssl.enabled: truexpack.security.transport.ssl.verification_mode: certificate xpack.security.transport.ssl.client_authentication: requiredxpack.security.transport.ssl.keystore.path: elastic-certificates.p12xpack.security.transport.ssl.truststore.path: elastic-certificates.p12

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

bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_passwordbin/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: truexpack.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-clustersnode.name: node-1path.data: /var/lib/elasticsearchpath.logs: /var/log/elasticsearchindices.memory.index_buffer_size: 20%indices.memory.min_index_buffer_size: 96mbthread_pool: search: size: 32 analyze: size: 30 queue_size: 1000indices.requests.cache.size: 2%indices.queries.cache.size: 10%indices.fielddata.cache.size: 20%indices.breaker.fielddata.limit: 40%network.host: 0.0.0.0http.port: 9200transport.port: 9300transport.compress: truehttp.max_content_length: 100mbdiscovery.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: 5sdiscovery.cluster_formation_warning_timeout: 10scluster.publish.timeout: 30scluster.routing.allocation.node_initial_primaries_recoveries: 16cluster.routing.allocation.node_concurrent_recoveries: 8indices.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地址、端口和目录路径应根据你的实际环境进行调整。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持8b8.cn。

使用ElasticSearch集群搭建步骤