如何在 Rocky Linux 9 上安装和配置 Elasticsearch如何在 Rocky Linux 9 上安装和配置 Elasticsearch如何在 Rocky Linux 9 上安装和配置 Elasticsearch如何在 Rocky Linux 9 上安装和配置 Elasticsearch
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 Rocky Linux 9 上安装和配置 Elasticsearch

介绍

Elasticsearch 是一个实时分布式搜索和分析数据的平台。由于其可用性、强大的功能和可扩展性,它是一个受欢迎的选择。

本文将指导您安装 Elasticsearch 8.x,为您的用例配置它,保护您的安装,并开始使用您的 Elasticsearch 服务器。

先决条件

在学习本教程之前,您需要:

  • 具有 2GB RAM 和 2 个 CPU 的 Rocky Linux 9 服务器,设置为非根 sudo 用户。您可以通过使用 Rocky Linux 9 进行初始服务器设置来实现这一点

Elasticsearch 可能有相对较高的要求,因为它默认为自己分配大约 1GB 的 RAM,因此请记住,您可能需要在内存受限的环境中启用交换。您的 Elasticsearch 服务器需要的 CPU、RAM 和存储量取决于您生成的记录数。

第 1 步 — 安装和配置 Elasticsearch

在安装 Elasticsearch 之前,您需要确保安装了可用的文本编辑器。 Rocky Linux 9 自带的默认文本编辑器是 vi。 vi 是一个非常强大的文本编辑器,但对于缺乏使用经验的用户来说可能有点迟钝。你可能想要安装一个对用户更友好的编辑器,例如 nano 以方便在你的 Rocky Linux 9 服务器上编辑配置文件:

  1. sudo dnf install nano -y

现在您可以继续安装 Elasticsearch。 Elasticsearch 组件在 Rocky 的默认包存储库中不可用。他们可以从 Elasticsearch 项目维护的存储库中参与。

所有包都使用 Elasticsearch 签名密钥进行签名,以保护您的系统免受包欺骗。使用密钥验证的包将被您的包管理器视为信任。在此步骤中,您将导入 Elasticsearch 公共 GPG 密钥并添加 Elastic 包源列表以安装 Elasticsearch。

首先,使用 rpm 打包工具从 elastic.co 导入密钥:

  1. rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

接下来,使用 nano 或您最喜欢的文本编辑器,在 /etc/yum.repos.d/ 目录中创建一个名为 elasticsearch.repo 的文件,因此您的包管理器可以连接到 Elasticsearch 存储库:

  1. sudo nano /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

该文件的 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch 部分指示您的包管理器使用您下载的密钥来验证 Elasticsearch 包的存储库和文件信息。

保存并关闭文件。如果您使用的是 nano,您可以使用 Ctrl+X 保存并退出,然后在出现提示时,按 Y 然后回车。

最后,使用 dnf 包管理器安装 Elasticsearch:

  1. sudo dnf install --enablerepo=elasticsearch elasticsearch

在提示确认安装时按 y。

Elasticsearch 安装输出的一部分应该包括安全自动配置信息,最重要的是,自动生成的 Elasticsearch 管理员密码:

Output
--------------------------- Security autoconfiguration information ------------------------------ Authentication and authorization are enabled. TLS for the transport and HTTP layers is enabled and configured. The generated password for the elastic built-in superuser is : CH77_qG8ji8QCxwUCr3w …

记下此密码,因为您将在本教程后面使用它,并且您将需要它来创建其他 Elasticsearch 用户。 Elasticsearch 现已安装并准备好进行配置。

第 2 步 — 配置 Elasticsearch

要配置 Elasticsearch,您需要编辑其主要配置文件 elasticsearch.yml,其中存储了大部分配置选项。此文件位于 /etc/elasticsearch 目录中。

使用 nano 或您最喜欢的文本编辑器打开 Elasticsearch 的配置文件:

  1. sudo nano /etc/elasticsearch/elasticsearch.yml

注意:Elasticsearch 的配置文件是 YAML 格式,这意味着你需要保持缩进语法。确保在编辑此文件时没有添加额外的空格。

elasticsearch.yml 文件为您的集群、节点、路径、内存、网络、发现和网关提供配置选项。大多数这些选项已在文件中预先配置,但您可以根据需要更改它们。出于此单服务器配置的目的,您只需调整网络主机的设置。

Elasticsearch 在端口 9200 上监听来自任何地方的流量。这在 Elasticsearch 8.x 中不像在以前的版本中那么严重,因为 Elasticsearch 现在默认需要身份验证。不过,您很可能需要限制外部访问您的 Elasticsearch 实例,以防止外部人员通过其 [REST API] (https://en.wikipedia.org/wiki/Representational_state_transfer) 读取您的数据或关闭您的 Elasticsearch 集群。要限制访问,找到指定 network.host 的行,通过删除行开头的 # 来取消注释,并替换它的值与 localhost 所以它读起来像这样:

. . .
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: localhost
. . .

指定 localhost 允许 Elasticsearch 监听所有接口和绑定的 IP。如果你想让它只监听一个特定的接口,你可以指定它的 IP 来代替 localhost。保存并关闭 elasticsearch.yml。如果您使用的是 nano,您可以使用 Ctrl+X 保存并退出,然后在出现提示时,按 Y 然后回车。

这些是您可以开始使用 Elasticsearch 的最低设置。现在您可以首次启动 Elasticsearch。

使用 systemctl 启动 Elasticsearch 服务。给 Elasticsearch 一些启动时间。否则,您可能会收到有关无法连接的错误。

  1. sudo systemctl start elasticsearch

接下来,运行以下命令使 Elasticsearch 能够在您的服务器每次启动时启动:

  1. sudo systemctl enable elasticsearch

在启动时启用 Elasticsearch 后,让我们继续下一步讨论安全性。

第 3 步 — 保护 Elasticsearch

任何可以访问 HTTP API 的人都可以控制 Elasticsearch。这不一定是安全风险,因为您已经将 Elasticsearch 配置为仅在 localhost 上侦听,并且因为 Elasticsearch 8+ 默认设置管理员密码。

如果您需要允许远程访问 HTTP API,您可以使用 firewalld 限制网络暴露。如果您按照先决条件中的步骤创建打开或限制端口 9200 的防火墙配置文件,则应该已经启用此防火墙。

如果您想投资额外的保护,Elasticsearch 提供商业 Shield 插件供您购买。

第 4 步 — 测试 Elasticsearch

到目前为止,Elasticsearch 应该在端口 9200 上运行。您可以通过使用 curl 向 localhost:9200 发出标准 HTTP GET 请求来测试它。从 Elasticsearch 8.x 开始,Elasticsearch API 默认需要 HTTPS 身份验证,因此您可以使用 --cacert 参数在请求中包含其提供的证书。最后,包含 -u elastic 参数以指定默认管理员用户名 elastic。

  1. curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200

系统将提示您输入安装时收到的管理员密码。身份验证后,您应该会收到以下响应:

Output
{ "name" : "elasticrocky", "cluster_name" : "elasticsearch", "cluster_uuid" : "_hb4dLuuR-ipiloXHT_AMw", "version" : { "number" : "8.5.3", "build_flavor" : "default", "build_type" : "rpm", "build_hash" : "4ed5ee9afac63de92ec98f404ccbed7d3ba9584e", "build_date" : "2022-12-05T18:22:22.226119656Z", "build_snapshot" : false, "lucene_version" : "9.4.2", "minimum_wire_compatibility_version" : "7.17.0", "minimum_index_compatibility_version" : "7.0.0" }, "tagline" : "You Know, for Search" }

如果您收到与上述类似的响应,则 Elasticsearch 工作正常。如果没有,请确保您已正确按照安装说明进行操作,并且已预留一些时间让 Elasticsearch 完全启动。

要对 Elasticsearch 执行更彻底的检查,请尝试查询 _nodes 端点,并将 ?pretty 添加到查询末尾以获得人类可读的文本格式:

  1. curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200/_nodes?pretty
[secondary label Output]
{
  "_nodes" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "cluster_name" : "elasticsearch",
  "nodes" : {
    "7TgeSgV2Tma0quqd6Mw6hQ" : {
…

这样,您可以验证节点、集群、应用程序路径、模块等的所有当前设置。

第 5 步 — 使用 Elasticsearch

要开始使用 Elasticsearch,让我们先添加一些数据。 Elasticsearch 使用 RESTful API,它响应通常的 CRUD 命令:创建、读取、更新和删除。要向 API 发送数据,您将再次使用 curl,但这次您将发出 PUT 而不是 GET 请求指定 -X PUT 并使用 -d 在命令行中包含一些 JSON 格式的数据。

您可以像这样添加您的第一个条目:

  1. curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic -X PUT "https://localhost:9200/test/_doc/1?pretty" -k -H 'Content-Type: application/json' -d '{"counter" : 1, "tags" : ["red"]}'

您应该收到以下响应:

Output
{ "_index" : "test", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }

使用 cURL,您已将 HTTP PUT 请求发送到 Elasticsearch 服务器。请求的 URI 是 /test/_doc/1 有几个参数:

  • test是数据在Elasticsearch中的索引。
  • _doc 是类型。
  • 1是我们在上述索引和类型下的条目的ID。

您可以使用 HTTP GET 请求检索第一个条目。

  1. curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic -X GET "https://localhost:9200/test/_doc/1?pretty" -k -H 'Content-Type: application/json'

这应该是结果输出:

Output
{ "_index" : "test", "_id" : "1", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : { "counter" : 1, "tags" : [ "red" ] } }

要修改现有条目,您可以使用 HTTP PUT 请求。

  1. curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic -X PUT "https://localhost:9200/test/_doc/1?pretty" -k -H 'Content-Type: application/json' -d '{"counter" : 1, "tags" : ["blue"]}'

Elasticsearch 应该像这样确认修改成功:

Output
{ "_index" : "test", "_id" : "1", "_version" : 2, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1 }

在上面的示例中,我们将第一个条目的 message 修改为 \Hello, People!。这样,版本号已自动增加到 2。

您可能已经注意到上述请求中的额外参数 pretty。它添加了格式化格式,以便您可以将每个数据字段写入新行。如果没有 pretty,Elasticsearch 输出返回时没有换行符或缩进。这对于 API 通信来说很好,但在命令行输出中更难阅读。

您现在已经在 Elasticsearch 中添加和查询了数据。要了解其他操作,请查看 API 文档。

结论

您现在已经安装、配置并开始使用 Elasticsearch。如需进一步探索 Elasticsearch 的功能,请参阅 Elasticsearch 官方文档。

©2015-2025 艾丽卡 support@alaica.com