Rocky Linux 8上Elasticsearch安装与配置终极指南

Elasticsearch简介

Elasticsearch 是一个用于分布式实时搜索和数据分析的平台。它以其易用性、强大的功能和可扩展性而备受青睐。

本文将指导您在Rocky Linux 8上安装Elasticsearch 8.x,并根据您的使用场景进行配置,保护您的安装,并开始使用Elasticsearch服务器。

先决条件

在跟随本教程之前,您将需要:

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

步骤1 — 安装和配置Elasticsearch

在安装Elasticsearch之前,您需要确保已安装一个可用的文本编辑器。Rocky Linux 8自带的默认文本编辑器是vi。vi是一个功能极其强大的文本编辑器,但对于缺乏经验的用户来说可能有些晦涩。您可能需要安装一个更用户友好的编辑器,例如nano,以便在Rocky Linux 8服务器上编辑配置文件:

sudo dnf install nano -y

现在您可以继续安装Elasticsearch。Elasticsearch组件不在Rocky的默认软件包仓库中。它们可以从Elasticsearch项目维护的仓库中获取。

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

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

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

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

sudo nano /etc/yum.repos.d/elasticsearch.repo
/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,最后按Enter来保存并退出。

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

sudo dnf install --enablerepo=elasticsearch elasticsearch

当提示确认安装时,按y

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

输出
--------------------------- 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

这是文章《如何在Rocky Linux 8上安装和配置Elasticsearch》的第2部分(共4部分)。

要配置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关闭您的Elasticsearch集群。要限制访问,请找到指定network.host的行,通过删除行首的#来取消注释,并将其值替换为localhost,使其如下所示:

/etc/elasticsearch/elasticsearch.yml

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

指定localhost允许Elasticsearch监听所有接口和绑定的IP。如果您希望它只监听特定接口,可以在localhost的位置指定其IP。保存并关闭elasticsearch.yml。如果您使用的是nano,可以通过按Ctrl+X,然后在提示时按Y,最后按Enter来保存并退出。

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

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

  1. sudo systemctl start elasticsearch

 

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

  1. sudo systemctl enable elasticsearch

 

在Elasticsearch开机自启后,让我们进入下一步,讨论安全性。

第三步 — 保护Elasticsearch

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

如果您需要允许远程访问HTTP API,可以使用firewalld限制网络暴露。如果您遵循了前提条件中的《Rocky Linux 8初始服务器设置》教程,此防火墙应该已经启用。Elasticsearch在端口9200上运行,因此如果您需要选择性地允许外部访问,可以创建一个防火墙配置文件来打开或限制端口9200。

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

第四步 — 测试Elasticsearch

这是文章《如何在Rocky Linux 8上安装和配置Elasticsearch》的第3部分(共4部分)。

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

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

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

输出
{
  "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以获取人类可读的文本格式:

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命令:创建(create)、读取(read)、更新(update)和删除(delete)。要将数据发送到API,您将再次使用curl,但这次您将通过指定-X PUT并在命令行上使用-d包含一些JSON格式的数据来发出PUT请求而不是GET请求。

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

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"]}'

您应该会收到以下响应:

输出

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

您已使用 cURL 向 Elasticsearch 服务器发送了一个 HTTP PUT 请求。请求的 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’

 

这应该是预期的输出:

输出

{ “_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 应该会像这样确认成功修改:

输出

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

在上面的示例中,我们已将第一个条目的消息修改为“Hello, People!”。随之,版本号自动增加到 2。

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

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

结论

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

bannerAds