介绍
Elasticsearch是一个实时分布式搜索和剖析数据的平台,具有可用性、强大的功能和可扩充性等特征,受到使用者的喜爱。
本文将教你们怎么安装、配置Elasticsearch8.x,让我们直接开始。
先决条件
在学习本教程之前,您须要打算:具有2GBRAM和2个CPU的RockyLinux8服务器。
Elasticsearch对硬件有较高的要求,自己就占有了大概1GB的RAM。
第1步:安装和配置Elasticsearch
在安装Elasticsearch之前,您须要确保安装了可用的文本编辑器,RockyLinux8自带的默认文本编辑器是vi.vi是一个十分强悍的文本编辑器,但对于缺少使用经验的用户来说可能有点迟缓。你可能想要安装一个愈发用户友好的编辑器,比如nano以便捷编辑你的RockyLinux8服务器上的配置文件:
如今您可以继续安装Elasticsearch,Elasticsearch组件在Rocky的默认包储存库中不可用,可以从Elasticsearch项目维护的储存库中参与。
所有包都使用Elasticsearch签名秘钥进行签名,以保护您的系统免受包欺瞒。使用秘钥验证的包将被您的包管理器视为信任。下边我们直接导出Elasticsearch公共GPG秘钥并添加Elastic包源列表。
首先服务器上装linux系统,使用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=文件的这一部份显示包管理器使用下载的秘钥来验证Elasticsearch包的储存库和文件信息。
保存并关掉文件。
使用包管理器安装Elasticsearchdnf:
sudo dnf install --enablerepo=elasticsearch elasticsearch
当提示确认安装时按y。
Elasticsearch安装输出的一部份应当包括Securityautoconfigurationinformation服务器上装linux系统,最重要的是,手动生成的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.ymllinux定时器,其中储存了大部份配置选项,该文件坐落/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上运行,所以在防火墙设置上可以放开9200linux服务器配置与管理,请记住,要想服务器安全,防火墙一定要开!其实了,在Elasticsearch的保护上,也可以使用其商业Shield插件,只是这是收费的。
第4步:测试Elasticsearch
到目前为止,Elasticsearch应当早已在端口9200上运行。您可以通过向localhost:9200发出标准HTTPGET恳求来测试它curl。
从Elasticsearch8.x开始,ElasticsearchAPI默认须要HTTPS身分验证,因而您可以使用参数在恳求中包含其提供的证书--cacert,最后,包含-uelastic指定默认管理员用户名的参数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使用RESTfulAPI,响应常用的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几个参数: