05. 通过RPM安装ES
Elasticsearch的RPM软件包可以从我们的网站或RPM软件包存储库中下载。它可用于在任何基于RPM的系统上安装Elasticsearch,例如OpenSuSE、SLES、Centos、Red Hat和Oracle Enterprise。
不支持在旧版本RPM的发行版上进行RPM安装,比如SLES 11和CentOS 5。请参见在Linux或MacOS上从存档中安装Elasticsearch。
该软件包包含免费和订阅功能。开始使用30天试用以尝试所有功能。
Elasticsearch的最新稳定版本可以在Download Elasticsearch页面找到。其他版本可以在Past Releases页面找到。
Elasticsearch包含了来自JDK维护者的OpenJDK捆绑版本(GPLv2+CE)。要使用您自己的Java版本,请参阅JVM版本要求。
导入Elasticsearch GPG密钥
我们使用Elasticsearch签名密钥(PGP密钥D88E42B4,可从https://pgp.mit.edu获取)签名所有软件包,指纹为:
4609 5ACC 8548 582C 1A26 99A9 D27D 666C D88E 42B4
下载并安装公共签名密钥:
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
从RPM存储库安装
在进行RedHat基础发行版的/etc/yum.repos.d/目录,或者在OpenSuSE基础发行版的/etc/zypp/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
然后,您的存储库已准备好供使用。您现在可以使用以下命令之一安装Elasticsearch:
sudo yum install --enablerepo=elasticsearch elasticsearch
sudo dnf install --enablerepo=elasticsearch elasticsearch
sudo zypper modifyrepo --enable elasticsearch && \
sudo zypper install elasticsearch; \
sudo zypper modifyrepo --disable elasticsearch
- 在CentOS和旧版Red Hat基础的系统上,请使用yum。
- 在Fedora和其他较新版Red Hat系统上,请使用dnf。
- 在基于OpenSUSE的系统上,请使用zypper。
配置好的存储库默认处于禁用状态。这消除了在升级系统的同时意外升级elasticsearch的可能性。每个安装或升级命令必须明确地启用存储库,如上面示例命令中所示。
手动下载和安装RPM
可以从网站下载Elasticsearch v8.11.1的RPM并按如下方式安装:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.11.1-x86_64.rpm
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.11.1-x86_64.rpm.sha512
shasum -a 512 -c elasticsearch-8.11.1-x86_64.rpm.sha512
sudo rpm --install elasticsearch-8.11.1-x86_64.rpm
比较下载的RPM和发布的校验和的SHA,应该输出elasticsearch-{version}-x86_64.rpm: OK。
在基于systemd的发行版上,安装脚本将尝试设置内核参数(例如vm.max_map_count);您可以通过屏蔽systemd-sysctl.service单元来跳过这一步。
启用安全性的情况下启动Elasticsearch
在安装Elasticsearch时,默认情况下启用并配置了安全功能。安装Elasticsearch时,将自动进行以下安全配置:
- 启用认证和授权,并为内置超级用户elastic生成密码。
- 为传输和HTTP层生成TLS的证书和密钥,并使用这些密钥和证书启用和配置TLS。
- 密码、证书和密钥将输出到您的终端。
建议将elastic密码存储为您的shell中的环境变量。例如:
export ELASTIC_PASSWORD="your_password"
重新配置节点以加入现有集群
在默认情况下,安装Elasticsearch时,安装过程会配置单节点集群。如果您希望节点加入现有集群,可以在首次启动新节点之前,在现有节点上生成一个注册令牌。
在现有集群中的任何节点上,生成一个节点注册令牌:
/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node
复制注册令牌,该令牌将输出到您的终端。
在您的新Elasticsearch节点上,将注册令牌作为参数传递给elasticsearch-reconfigure-node工具:
/usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token <enrollment-token>
现在,Elasticsearch已配置为加入现有集群。
使用systemd启动新节点。
启用系统索引的自动创建
一些商业功能会在Elasticsearch中自动创建索引。默认情况下,Elasticsearch配置为允许自动创建索引,无需
额外步骤。但是,如果您在Elasticsearch中禁用了自动索引创建,则必须在elasticsearch.yml中配置action.auto_create_index,以允许商业功能创建以下索引:
action.auto_create_index: .monitoring*,.watches,.triggered_watches,.watcher-history*,.ml*
如果您使用Logstash或Beats,则可能需要在action.auto_create_index设置中添加其他索引名称,确切的值取决于您的本地配置。如果不确定环境的正确值,可以考虑将该值设置为*,这将允许自动创建所有索引。
使用systemd运行Elasticsearch
要配置Elasticsearch在系统启动时自动启动,运行以下命令:
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable elasticsearch.service
可以使用以下命令启动和停止Elasticsearch:
sudo systemctl start elasticsearch.service
sudo systemctl stop elasticsearch.service
这些命令不提供Elasticsearch是否成功启动的反馈。相反,此信息将写入/var/log/elasticsearch/目录中的日志文件。
如果您对Elasticsearch keystore进行了密码保护,您需要使用本地文件和systemd环境变量向systemd提供keystore密码。该本地文件在存在时应受到保护,并且在Elasticsearch运行起来后可以安全地删除。
echo "keystore_password" > /path/to/my_pwd_file.tmp
chmod 600 /path/to/my_pwd_file.tmp
sudo systemctl set-environment ES_KEYSTORE_PASSPHRASE_FILE=/path/to/my_pwd_file.tmp
sudo systemctl start elasticsearch.service
默认情况下,Elasticsearch服务不在systemd日志中记录信息。要启用journalctl记录,必须从elasticsearch.service文件的ExecStart命令行中删除--quiet选项。
启用systemd日志记录后,可以使用journalctl命令查看日志信息:
- 跟踪日志:
sudo journalctl -f
- 列出elasticsearch服务的日志条目:
sudo journalctl --unit elasticsearch
- 从给定时间开始列出elasticsearch服务的日志条目:
sudo journalctl --unit elasticsearch --since "2016-10-30 18:17:16"
有关更多命令行选项,请参阅man journalctl或https://www.freedesktop.org/software/systemd/man/journalctl.html。
旧版本systemd版本的启动超时
默认情况下,Elasticsearch将TimeoutStartSec参数设置为systemd的900秒。如果您运行的是至少238版本的systemd,则Elasticsearch可以自动延长启动超时,并将重复执行此操作,直到启动完成,即使超过900秒。
版本低于238的systemd不支持超时扩展机制,如果在配置的超时内启动未完成,将终止Elasticsearch进程。如果发生这种情况,Elasticsearch将在启动后的短时间内报告正常关闭:
[2022-01-31T01:22:31,077][INFO ][o.e.n.Node ] [instance-0000000123] starting ...
...
[2022-01-31T01:37:15,077][INFO ][o.e.n.Node ] [instance-0000000123] stopping ...
但是,systemd日志将报告启动超时:
Jan 31 01:22:30 debian systemd[1]: Starting Elasticsearch...
Jan 31 01:37:15 debian systemd[1]: elasticsearch.service: Start operation timed out. Terminating.
Jan 31 01:37:15 debian systemd[1]: elasticsearch.service: Main process exited, code=killed, status=15/TERM
Jan 31 01:37:15 debian systemd[1]: elasticsearch.service: Failed with result 'timeout'.
Jan 31 01:37:15 debian systemd[1]: Failed to start Elasticsearch.
为了避免这种情况,将您的systemd升级至至少238版本。您还可以通过扩展TimeoutStartSec参数来临时解决该问题。
检查Elasticsearch是否运行
您可以通过向本地主机的端口9200发送HTTPS请求来测试Elasticsearch节点是否正在运行:
curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200
确保在调用中使用https,否则请求将失败。
--cacert
HTTP层的生成的http_ca.crt证书的路径。
该调用将返回如下响应:
{
"name" : "Cp8oag6",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "AT69_T_DTp-1qgIJlatQqA",
"version" : {
"number" : "8.11.1",
"build_type" : "tar",
"build_hash" : "f27399d",
"build_flavor" : "default",
"build_date" : "2016-03-30T09:51:41.449Z",
"build_snapshot" : false,
"lucene_version" : "9.8.0",
"minimum_wire_compatibility_version" : "1.2.3",
"minimum_index_compatibility_version" : "1.2.3"
},
"tagline" : "You Know, for Search"
}
配置Elasticsearch
/etc/elasticsearch目录包含Elasticsearch的默认运行时配置。此目录及其所有包含的文件的所有权在软件包安装时设置为root:elasticsearch。
setgid标志对/etc/elasticsearch目录应用组权限,以确保Elasticsearch可以读取其中的任何文件和子目录。所有文件和子目录都继承了root:elasticsearch的所有权。从此目录或任何子目录运行命令(例如elasticsearch-keystore工具)需要root:elasticsearch权限。
Elasticsearch默认从/etc/elasticsearch/elasticsearch.yml文件加载其配置。此配置文件的格式在Configuring Elasticsearch中有解释。
RPM还有一个系统配置文件(/etc/sysconfig/elasticsearch),允许您设置以下参数:
ES_JAVA_HOME | Set a custom Java path to be used. |
---|---|
ES_PATH_CONF | Configuration file directory (which needs to include elasticsearch.yml, jvm.options, and log4j2.properties files); defaults to /etc/elasticsearch. |
ES_JAVA_OPTS | Any additional JVM system properties you may want to apply. |
RESTART_ON_UPGRADE | Configure restart on package upgrade, defaults to false. This means you will have to restart your Elasticsearch instance after installing a package manually. The reason for this is to ensure, that upgrades in a cluster do not result in a continuous shard reallocation resulting in high network traffic and reducing the response times of your cluster. |
连接客户端到Elasticsearch
当您第一次启动Elasticsearch时,TLS会自动配置为HTTP层。CA证书会被生成并存储在磁盘上:
/etc/elasticsearch/certs/http_ca.crt
该证书的十六进制编码SHA-256指纹也会输出到终端。连接到Elasticsearch的任何客户端,如Elasticsearch客户端、Beats、独立的Elastic代理和Logstash,都必须验证它们信任Elasticsearch用于HTTPS的证书。Fleet Server和Fleet管理的Elastic代理会自动配置为信任CA证书。其他客户端可以通过使用CA证书的指纹或CA证书本身来建立信任。
如果自动配置过程已经完成,您仍然可以获取安全证书的指纹。您还可以将CA证书复制到您的机器并配置您的客户端以使用它。
使用CA指纹
复制在Elasticsearch启动时输出到终端的指纹值,并配置您的客户端使用此指纹在连接到Elasticsearch时建立信任。
如果自动配置过程已经完成,您仍然可以通过运行以下命令获取安全证书的指纹。该路径是针对HTTP层的自动生成的CA证书。
openssl x509 -fingerprint -sha256 -in config/certs/http_ca.crt
该命令返回包含指纹的安全证书。颁发者应该是Elasticsearch安全自动配置HTTP CA。
issuer= /CN=Elasticsearch security auto-configuration HTTP CA
SHA256 Fingerprint=<指纹>
使用CA证书
如果您的库不支持验证指纹的方法,则在每个Elasticsearch节点上创建的自动生成的CA证书位于以下目录:
/etc/elasticsearch/certs/http_ca.crt
将http_ca.crt
文件复制到您的机器,并配置您的客户端使用此证书在连接到Elasticsearch时建立信任。
RPM的目录布局编辑
RPM将配置文件、日志和数据目录放置在适用于基于RPM的系统的相应位置:
Type | Description | Default Location | Setting |
---|---|---|---|
home | Elasticsearch home directory or $ES_HOME | /usr/share/elasticsearch | |
bin | Binary scripts including elasticsearch to start a node and elasticsearch-plugin to install plugins | /usr/share/elasticsearch/bin | |
conf | Configuration files including elasticsearch.yml | /etc/elasticsearch | ES_PATH_CONF |
conf | Environment variables including heap size, file descriptors. | /etc/sysconfig/elasticsearch | |
conf | Generated TLS keys and certificates for the transport and http layer. | /etc/elasticsearch/certs | |
data | The location of the data files of each index / shard allocated on the node. | /var/lib/elasticsearch | path.data |
jdk | The bundled Java Development Kit used to run Elasticsearch. Can be overridden by setting the ES_JAVA_HOME environment variable in /etc/sysconfig/elasticsearch. | /usr/share/elasticsearch/jdk | |
logs | Log files location. | /var/log/elasticsearch | path.logs |
plugins | Plugin files location. Each plugin will be contained in a subdirectory. | /usr/share/elasticsearch/plugins | |
repo | Shared file system repository locations. Can hold multiple locations. A file system repository can be placed in to any subdirectory of any directory specified here. | Not configured | path.repo |
安全证书和密钥
在安装Elasticsearch时,将在Elasticsearch配置目录中生成以下证书和密钥,用于将Kibana实例连接到您的安全Elasticsearch集群并加密节点间通信。以下是这些文件的列表,仅供参考。
-
http_ca.crt
用于签署此Elasticsearch集群HTTP层证书的CA证书。 -
http.p12
包含此节点HTTP层的密钥和证书的密钥库。 -
transport.p12
包含您集群中所有节点传输层的密钥和证书的密钥库。
http.p12
和 transport.p12
都是受密码保护的PKCS#12密钥库。Elasticsearch将这些密钥库的密码存储为安全设置。为了检查或更改密钥库内容,可以使用bin/elasticsearch-keystore
工具检索密码。
使用以下命令检索http.p12
的密码:
bin/elasticsearch-keystore show xpack.security.http.ssl.keystore.secure_password
使用以下命令检索transport.p12
的密码:
bin/elasticsearch-keystore show xpack.security.transport.ssl.keystore.secure_password