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

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 如何在 Rocky Linux 8 上安装和配置 Elasticsearch

介绍

Elasticsearch是一个实时分布式搜索和分析数据的平台,具有可用性、强大的功能和可扩展性等特点,深受使用者的喜爱。

本文将教大家如何安装、配置Elasticsearch 8.x,让我们直接开始。

先决条件

在学习本教程之前,您需要准备:具有 2GB RAM 和 2 个 CPU 的 Rocky Linux 8 服务器。

Elasticsearch 对硬件有较高的要求,自己就占有了大约 1GB 的 RAM。

第 1 步:安装和配置 Elasticsearch

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

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

所有包都使用 Elasticsearch 签名密钥进行签名,以保护您的系统免受包欺骗。使用密钥验证的包将被您的包管理器视为信任。下面我们直接导入 Elasticsearch 公共 GPG 密钥并添加 Elastic 包源列表。

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

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

接下来,使用或您最喜欢的文本编辑器,在目录/etc/yum.repos.d/中nano创建一个名为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 包的存储库和文件信息。

保存并关闭文件。

使用包管理器安装 Elasticsearch dnf:

sudo dnf install --enablerepo=elasticsearch elasticsearch

当提示确认安装时按y

Elasticsearch 安装输出的一部分应该包括Security autoconfiguration information,最重要的是,自动生成的 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目录中。

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

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

使用 systemctl 启动 Elasticsearch 服务:

sudo systemctl start elasticsearch

设置开机自启:

sudo systemctl enable elasticsearch

在启动时启用 Elasticsearch 后,下面我们看下Elasticsearch安全性。

第 3 步:保护 Elasticsearch

我们知道 Elasticsearch 默认在端口 9200 上运行,所以在防火墙设置上可以放开9200,请记住,要想服务器安全,防火墙一定要开!当然了,在Elasticsearch的保护上,也可以使用其商业Shield 插件,只是这是收费的。

第 4 步:测试 Elasticsearch

到目前为止,Elasticsearch 应该已经在端口 9200 上运行。您可以通过向localhost:9200 发出标准 HTTP GET 请求来测试它curl。

从 Elasticsearch 8.x 开始,Elasticsearch API 默认需要 HTTPS 身份验证,因此您可以使用参数在请求中包含其提供的证书--cacert,最后,包含-u elastic指定默认管理员用户名的参数elastic。

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到查询末尾,这样的话提高可读性:

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 命令:创建、读取、更新和删除。

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
}

请求的 URI 带有/test/_doc/1几个参数:

  • test是 Elasticsearch 中数据的索引。
  • _doc是类型。
  • 1就是我们在上面的index和type下的entry的ID。

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

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 请求。

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 文档:

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html

总结

本文主要给大家介绍如何在 Rocky Linux 8 上安装和配置 Elasticsearch,内容详实,可供参考,有任何问题欢迎在下方评论区与我讨论。

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
4天前
|
NoSQL Linux 测试技术
Redis的安装(Linux版)
Redis的安装(Linux版)
41 1
|
14天前
|
缓存 Linux 测试技术
安装【银河麒麟V10】linux系统--并挂载镜像
安装【银河麒麟V10】linux系统--并挂载镜像
72 0
|
14天前
|
Linux C语言
linux yum安装ffmpeg 图文详解
linux yum安装ffmpeg 图文详解
32 0
|
1天前
|
Linux 开发工具 Android开发
Docker系列(1)安装Linux系统编译Android源码
Docker系列(1)安装Linux系统编译Android源码
3 0
|
1天前
|
Ubuntu Linux 开发工具
WSL2(3)安装Linux headers完美解决方案
WSL2(3)安装Linux headers完美解决方案
3 0
|
1天前
|
Linux 开发工具 C语言
Linux 安装 gcc 编译运行 C程序
Linux 安装 gcc 编译运行 C程序
10 0
|
1天前
|
Ubuntu 网络协议 Linux
Linux(20) Ubuntu 20.04 网络接口自动切换路由配置
Linux(20) Ubuntu 20.04 网络接口自动切换路由配置
12 0
|
1天前
|
Ubuntu Linux Python
Linux(15)Ubuntu安装ninja构建工具
Linux(15)Ubuntu安装ninja构建工具
8 0
|
1天前
|
Linux
Linux(9)Debain EC25 quectel-CM usbnet0开机自动联网配置
Linux(9)Debain EC25 quectel-CM usbnet0开机自动联网配置
6 0
|
1天前
|
Ubuntu Linux 数据安全/隐私保护
Linux(7)Ubuntu20.04 arm64安装Docker
Linux(7)Ubuntu20.04 arm64安装Docker
9 0