如何在 CentOS 8 上安装和配置 Elasticsearch

作者选择了 COVID-19 救援基金作为 Write for Donations计划的一部分接受捐款。

介绍

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

Elasticsearch 支持 RESTful 操作,这意味着您可以使用 HTTP 方法(‘GET’、‘POST’、‘PUT’、‘DELETE’ 等)与 HTTP URI(/collection/entry)相结合来操纵您的数据。

Elasticsearch是一个免费的和开源的软件,其背后有一个坚实的公司 - Elastic. 这种组合使其适合许多用例,从个人测试到企业整合。

本文将向您介绍Elasticsearch,并向您展示如何安装,配置和开始使用它。

前提条件

要遵循本教程,您将需要以下内容:

步骤 1 —在CentOS 8上安装Java

Elasticsearch 是用 Java 编程语言编写的,你的第一个任务就是在你的服务器上安装一个 Java Runtime Environment (JRE)。

安装最新版本的 OpenJDK 8:

1sudo dnf install java-1.8.0-openjdk.x86_64 -y

现在检查您的安装:

1java -version

命令将创建这样的输出:

1[secondary_label Output]
2openjdk version "1.8.0_262"
3OpenJDK Runtime Environment (build 1.8.0_262-b10)
4OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)

当您在使用 Elasticsearch 时取得进展并开始寻找更好的 Java 性能和兼容性时,您可以选择安装 Oracle 的专有 Java (Oracle JDK 8)。

步骤 2 — 在 CentOS 8 上下载和安装 Elasticsearch

您可以直接从 elastic.co下载 Elasticsearch 在zip,tar.gz,debrpm包中。

在这篇文章写作时,最新的Elasticsearch版本是7.9.2。

从工作目录下载程序:

1sudo rpm -ivh https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-x86_64.rpm

Elasticsearch 将安装在 /usr/share/elasticsearch/,其配置文件将放置在 /etc/elasticsearch,其 init 脚本将添加到 /etc/init.d/elasticsearch

要确保 Elasticsearch 自动启动和停止服务器,请将其 init 脚本添加到默认的运行级别:

1sudo systemctl daemon-reload && sudo systemctl enable elasticsearch.service

有了 Elasticsearch 安装,您现在将配置一些重要的设置。

步骤 3 —在CentOS 8上配置Elasticsearch

现在你已经安装了Elasticsearch及其Java依赖,是时候配置Elasticsearch了。

Elasticsearch 配置文件位于 /etc/elasticsearch 目录中. 我们将审查和编辑的文件是:

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

在任何Elasticsearch服务器上定制的第一个变量是node.namecluster.nameelasticsearch.yml

正如其名称所示,node.name指定了服务器(节点)的名称和与其相关的集群. 如果您不定制这些变量,将自动分配一个node.name与服务器的主机名称有关。

cluster.name 值由 Elasticsearch 自动检测功能用于自动检测并将 Elasticsearch 节点与集群相关联,因此,如果您不更改默认值,则在集群中可能存在相同网络上发现的不需要的节点。

让我们开始编辑主要的elasticsearch.yml配置文件。

使用nano或您喜爱的文本编辑器打开它:

1sudo nano /etc/elasticsearch/elasticsearch.yml

删除#字符为node.namecluster.name,以免评论,然后更改它们的值。

1[label /etc/elasticsearch/elasticsearch.yml]
2...
3node.name: "My First Node"
4cluster.name: mycluster1
5...

elasticsearch.yml中也可以找到网络设置。默认情况下,Elasticsearch 会听 localhost 在端口 9200 上,所以只有来自同一服务器的客户端可以连接。

另一个重要的设置是node.roles属性,您可以将此设置为master-eligible(在配置中仅仅是master),dataingest

主资格角色负责集群的健康和稳定性。在大量集群节点的大型部署中,建议只具有角色的多个专用节点。通常,专用节点不会存储数据或创建索引。

数据角色定义了将数据存储的节点,即使数据节点过载,群集健康也不应该受到严重影响,只要有其他节点来承担额外的负载。

最后,注入角色允许节点接受和处理数据流.在较大的设置中,应该有专用的注入节点,以避免可能的数据节点的过载。

<$>[注] **注:**一个节点可能有一个或多个角色,允许可扩展性、冗余性和高可用性在Elasticsearch设置。默认情况下,所有这些角色都分配给节点。这适用于单节点的Elasticsearch,如本文中示例场景中所述。因此,您不需要更改该角色。

1[label /etc/elasticsearch/elasticsearch.yml]
2...
3node.roles: [ master ]
4...

美元

另一个考虑更改的设置是path.data。这决定了数据存储的路径,默认路径是/var/lib/elasticsearch。在生产环境中,建议您使用专用分区和安装点来存储Elasticsearch数据。在最佳情况下,这个专用分区将是一个单独的存储媒体,可以提供更好的性能和数据隔离。

1[label /etc/elasticsearch/elasticsearch.yml]
2...
3path.data: /media/different_media
4...

现在你已经完成了所有更改,保存并关闭Electronic.exe

您还必须在jvm.options中编辑配置。

请记住,Elasticsearch是由JVM运行的,也就是说,它基本上是一个Java应用程序,所以就像任何Java应用程序一样,它有JVM设置,可以配置在文件 `/etc/elasticsearch/jvm.options 中。

默认情况下,两者都设置为1GB,但这几乎从未是最佳的。不仅如此,但如果您的服务器只有1GB的RAM,您将无法在默认设置下启动Elasticsearch。

不幸的是,没有通用公式来计算内存设置。当然,你分配的内存越多,性能就越好,但请确保在服务器上剩余的流程中有足够的内存。例如,如果你的机器有1GB的RAM,你可以将XmsXmx都设置为512MB,从而允许其他流程的512MB。请注意,通常XmsXmx都设置为相同的值,以避免JVM垃圾收集的性能处罚。

如果您的服务器只有1GB的RAM,您必须编辑此设置。

打开jvm.options:

1sudo nano /etc/elasticsearch/jvm.options

现在将XmsXmx值更改为512MB:

1[label /etc/elasticsearch/jvm.options]
2...
3-Xms512m
4-Xmx512m
5...

保存和退出文件。

现在开始 Elasticsearch 首次:

1sudo systemctl start elasticsearch.service

请在尝试使用 Elasticsearch 之前至少允许 Elasticsearch 启动 10 秒,否则可能会出现连接错误。

<$>[注] **注:**您应该知道,并非所有 Elasticsearch 设置都设置并保留在配置文件中。相反,一些设置是通过其 API 设置的,例如 index.number_of_shardsindex.number_of_replicas。第一个定义了索引将分成多少块(shards)。

假设您仍然在单个节点上探索和测试 Elasticsearch,您可以使用这些设置进行播放,并通过执行以下弯曲命令来更改它们:

1curl -XPUT -H 'Content-Type: application/json' 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '{
2  "index.number_of_replicas" : "0",
3  "index.number_of_shards" : "1"
4}'

美元

随着 Elasticsearch 安装和配置,您现在将保护和测试服务器。

步骤 4 —(可选)在CentOS 8上保护Elasticsearch

Elasticsearch 没有内置的安全性,任何可以访问 HTTP API 的用户都可以控制它. 本节不是保护 Elasticsearch 的全面指南。

默认情况下,Elasticsearch 配置为仅在 localhost 网络接口上聆听,即无法进行远程连接。

  • 您已限制访问 TCP 端口 9200 仅限于使用 iptables的受信任主机。 *您已在您的受信任主机之间创建了 vpn并将 Elasticsearch 暴露在 vpn 的虚拟接口中。

只有在您完成上述操作后,您才能考虑允许 Elasticseach 在 localhost 以外的其他网络接口上聆听,例如,当您需要从其他主机连接到 Elasticsearch 时,可能会考虑进行此类更改。

若要更改网络曝光,请打开elasticsearch.yml文件:

1sudo nano /etc/elasticsearch/elasticsearch.yml

在此文件中,找到包含network.host的行,通过在行开始时删除#字符,然后将值更改为安全网络接口的IP地址。

1[label /etc/elasticsearch/elasticsearch.yml]
2...
3network.host: 10.0.0.1
4...

<$>[警告] 警告: 由于 Elasticsearch 没有内置的安全性,所以非常重要的是,您不要将此设置为任何您无法控制或信任的服务器可以访问的 IP 地址。

此外,为了额外的安全性,您可以禁用用于评估自定义表达式的脚本. 通过创建自定义恶意表达式,攻击者可能会损害您的环境。

若要禁用自定义表达式,请在 /etc/elasticsearch/elasticsearch.yml 文件的末尾添加下列行:

1...
2[label /etc/elasticsearch/elasticsearch.yml]
3script.allowed_types: none
4...

要使上述更改生效,您需要重新启动 Elasticsearch。

现在重新启动 Elasticsearch:

1sudo service elasticsearch restart

在此步骤中,您采取了一些措施来保护您的 Elasticsearch 服务器. 现在您已经准备好测试该应用程序。

步骤 5 — 在 CentOS 8 上测试 Elasticsearch

到目前为止,Elasticsearch 应该在端口 9200 上运行,您可以使用curl来测试,这是一种用于客户端 URL 转移的命令行工具。

要测试该服务,请发出这样的GET请求:

1curl -X GET 'http://localhost:9200'

你会看到以下答案:

 1[secondary_label Output]
 2{
 3  "name" : "My First Node",
 4  "cluster_name" : "mycluster1",
 5  "cluster_uuid" : "R23U2F87Q_CdkEI2zGhLGw",
 6  "version" : {
 7    "number" : "7.9.2",
 8    "build_flavor" : "default",
 9    "build_type" : "rpm",
10    "build_hash" : "d34da0ea4a966c4e49417f2da2f244e3e97b4e6e",
11    "build_date" : "2020-09-23T00:45:33.626720Z",
12    "build_snapshot" : false,
13    "lucene_version" : "8.6.2",
14    "minimum_wire_compatibility_version" : "6.8.0",
15    "minimum_index_compatibility_version" : "6.0.0-beta1"
16  },
17  "tagline" : "You Know, for Search"
18}

如果您看到类似的响应,Elasticsearch 将正常工作,否则请重新检查安装说明并允许 Elasticsearch 完全启动一段时间。

您的 Elasticsearch 服务器已启动,下一步您将从应用程序中添加和获取一些数据。

步骤6 —在CentOS 8上使用Elasticsearch

在此步骤中,您将添加一些数据到 Elasticsearch,然后执行一些手动查询。

使用curl添加你的第一个条目:

1curl -H 'Content-Type: application/json' -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'

您将看到以下结果:

1[secondary_label Output]
2{"_index":"tutorial","_type":"helloworld","_id":"1","_version":3,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":4

使用curl,您将HTTPPOST请求发送到Elasticseach服务器. 请求的URI是/tutorial/helloworld/1

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

请注意,您还必须将所有POST请求的内容类型设置为 JSON,并使用H内容类型:应用程序/json`参数。

现在,使用HTTPGET请求获取您的第一个条目:

1curl -X GET 'http://localhost:9200/tutorial/helloworld/1'

结果将是这样的:

1[secondary_label Output]
2{"_index":"tutorial","_type":"helloworld","_id":"1","_version":3,"_seq_no":2,"_primary_term":4,"found":true,"_source":{ "message": "Hello World!" }}

若要更改现有条目,您可以使用这样的 HTTP PUT 请求:

1curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/tutorial/helloworld/1?pretty' -d '
2{
3  "message": "Hello People!"
4}'

Elasticsearch 会承认成功的修改如下:

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

在上面的示例中,您将第一个条目的消息更改为你好!因此,版本号增加到2

为了使你的GET操作的输出更易于人读,你也可以通过添加漂亮参数来假设你的结果:

1curl -X GET 'http://localhost:9200/tutorial/helloworld/1?pretty'

现在,答案将以更可读的格式输出:

 1[secondary_label Output]
 2{
 3  "_index" : "tutorial",
 4  "_type" : "helloworld",
 5  "_id" : "1",
 6  "_version" : 2,
 7  "_seq_no" : 1,
 8  "_primary_term" : 1,
 9  "found" : true,
10  "_source" : {
11    "message" : "Hello People!"
12  }
13}

您可以这样在 Elasticsearch 中添加和查询数据. 若要了解其他操作,您可以检查 Elasticsearch API 文档

结论

在本教程中,您安装,配置,并开始使用Elasticsearch在CentOS 8上。

Published At
Categories with 技术
comments powered by Disqus