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

介绍

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

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

前提条件

在遵循本教程之前,您将需要:

Elasticsearch 可能具有较高的要求,因为它默认地分配大约 1 GB 的 RAM,所以请记住,您可能需要在内存限制环境中启用 SWAP

步骤 1 – 安装和配置 Elasticsearch

在安装Elasticsearch之前,您需要确保安装可用文本编辑器。与Rocky Linux 9一起提供的默认文本编辑器是vivi是一个非常强大的文本编辑器,但对于缺乏经验的用户来说,它可能有点杂。

1sudo dnf install nano -y

现在您可以继续安装 Elasticsearch. Elasticsearch 组件无法在 Rocky 的默认包存储库中使用,而可以从 Elasticsearch 项目维护的存储库中使用。

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

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

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

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

1sudo nano /etc/yum.repos.d/elasticsearch.repo
1[label /etc/yum.repos.d/elasticsearch.repo]
2[elasticsearch]
3name=Elasticsearch repository for 8.x packages
4baseurl=https://artifacts.elastic.co/packages/8.x/yum
5gpgcheck=1
6gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
7enabled=0
8autorefresh=1
9type=rpm-md

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

如果你使用nano,你可以使用Ctrl+X保存和停止,然后在提示时,Y,然后输入。

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

1sudo dnf install --enablerepo=elasticsearch elasticsearch

当被要求确认安装时,按y

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

1[secondary_label Output]
2--------------------------- Security autoconfiguration information ------------------------------
3
4Authentication and authorization are enabled.
5TLS for the transport and HTTP layers is enabled and configured.
6
7The generated password for the elastic built-in superuser is : CH77_qG8ji8QCxwUCr3w
8

记住这个密码,因为你将在本教程中稍后使用它,你将需要它来创建其他Elasticsearch用户。

步骤 2 – 配置 Elasticsearch

要配置 Elasticsearch,您将编辑其主要配置文件 elasticsearch.yml,其中存储了其大多数配置选项。

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

1sudo nano /etc/elasticsearch/elasticsearch.yml

<$>[注] 注: Elasticsearch 的配置文件是 YAML 格式,这意味着您需要维护插入语法。

elasticsearch.yml文件为您的集群、节点、路径、内存、网络、发现和网关提供了配置选项. 这些选项大多都已预先配置在文件中,但您可以根据需要进行更改。

Elasticsearch 在端口9200上听取来自任何地方的流量。这在 Elasticsearch 8.x 中并不像在以前的版本中那样存在问题,因为 Elasticsearch 现在需要默认身份验证。然而,您可能需要限制对您的 Elasticsearch 实例的外部访问,以防止外部人阅读您的数据或关闭您的 Elasticsearch 集群通过其 [REST API] (https://en.wikipedia.org/wiki/Representational_state_transfer)。

1[label /etc/elasticsearch/elasticsearch.yml]
2. . .
3# ---------------------------------- Network -----------------------------------
4#
5# Set the bind address to a specific IP (IPv4 or IPv6):
6#
7network.host: localhost
8. . .

指定localhost允许Elasticsearch在所有界面和绑定IP上聆听。如果您希望它只在特定界面上聆听,您可以指定其IP代替localhost。保存并关闭elasticsearch.yml

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

使用systemctl启动 Elasticsearch 服务. 给 Elasticsearch 几分钟启动,否则可能会出现无法连接的错误。

1sudo systemctl start elasticsearch

接下来,运行以下命令,以允许 Elasticsearch 每次启动服务器时启动:

1sudo systemctl enable elasticsearch

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

步骤 3 – 确保 Elasticsearch

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

如果您需要允许远程访问 HTTP API,您可以通过firewalld来限制网络曝光。如果您遵循前提 Initial Server Setup with Rocky Linux 9 教程中的步骤,该防火墙应该已经启用。

如果您想投资额外的保护,Elasticsearch 可购买商用 Shield 插件

步骤 4 – 测试 Elasticsearch

到目前为止,Elasticsearch应该在端口9200上运行。您可以通过将标准的HTTP GET请求设置为localhost:9200并使用curl来测试它。从Elasticsearch 8.x开始,Elasticsearch API默认情况下需要HTTPS身份验证,因此您可以使用--cacert参数将其提供的证书包含在请求中。

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

您将被要求输入您在安装时收到的管理员密码. 验证后,您应该收到以下响应:

 1[secondary_label Output]
 2{
 3  "name" : "elasticrocky",
 4  "cluster_name" : "elasticsearch",
 5  "cluster_uuid" : "_hb4dLuuR-ipiloXHT_AMw",
 6  "version" : {
 7    "number" : "8.5.3",
 8    "build_flavor" : "default",
 9    "build_type" : "rpm",
10    "build_hash" : "4ed5ee9afac63de92ec98f404ccbed7d3ba9584e",
11    "build_date" : "2022-12-05T18:22:22.226119656Z",
12    "build_snapshot" : false,
13    "lucene_version" : "9.4.2",
14    "minimum_wire_compatibility_version" : "7.17.0",
15    "minimum_index_compatibility_version" : "7.0.0"
16  },
17  "tagline" : "You Know, for Search"
18}

如果您收到类似上述的响应,则 Elasticsearch 正在正常工作. 如果没有,请确保您遵循正确的安装说明,并允许 Elasticsearch 完全启动一段时间。

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

1curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200/_nodes?pretty
 1[secondary label Output]
 2{
 3  "_nodes" : {
 4    "total" : 1,
 5    "successful" : 1,
 6    "failed" : 0
 7  },
 8  "cluster_name" : "elasticsearch",
 9  "nodes" : {
10    "7TgeSgV2Tma0quqd6Mw6hQ" : {
11

通过这种方式,您可以验证节点、集群、应用路径、模块等等的所有当前设置。

第5步:使用Elasticsearch

要开始使用 Elasticsearch,让我们先添加一些数据。Elasticsearch 使用一个 RESTful API,该 API 响应常见的 CRUD 命令: create, read, update,和 delete. 要将数据发送到 API,您将再次使用curl,但这一次您将通过指定-X PUT并使用-d在命令行中包含一些 JSON 格式化数据来创建PUT而不是GET请求。

你可以这样添加你的第一个入口:

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

你应该得到以下答案:

 1[secondary_label Output]
 2{
 3  "_index" : "test",
 4  "_id" : "1",
 5  "_version" : 1,
 6  "result" : "created",
 7  "_shards" : {
 8    "total" : 2,
 9    "successful" : 1,
10    "failed" : 0
11  },
12  "_seq_no" : 0,
13  "_primary_term" : 1
14}

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

  • test 是 Elasticsearch 中的数据索引
  • _doc 是类型
  • 1 是我们在上面的索引和类型下输入的 ID

您可以通过 HTTP GET 请求获取此第一个条目。

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

这应该是产生的结果:

 1[secondary_label Output]
 2{
 3  "_index" : "test",
 4  "_id" : "1",
 5  "_version" : 1,
 6  "_seq_no" : 0,
 7  "_primary_term" : 1,
 8  "found" : true,
 9  "_source" : {
10    "counter" : 1,
11    "tags" : [
12      "red"
13    ]
14  }
15}

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

1curl --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 应该承认这样的成功修改:

 1[secondary_label Output]
 2{
 3  "_index" : "test",
 4  "_id" : "1",
 5  "_version" : 2,
 6  "result" : "updated",
 7  "_shards" : {
 8    "total" : 2,
 9    "successful" : 1,
10    "failed" : 0
11  },
12  "_seq_no" : 1,
13  "_primary_term" : 1
14}

在上面的示例中,我们将第一个条目的消息更改为你好,人们!这样,版本号被自动增加到2

在上述请求中,您可能已经注意到额外的参数pretty。它添加了格式格式,以便您可以将每个数据字段写入一个新的行上。

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

结论

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

Published At
Categories with 技术
comments powered by Disqus