在 CentOS 7/Fedora 37/36/35/34/33 上安装 ELK Stack
如何在 CentOS 7/Fedora 37/36/35/34/33 上安装 ELK Stack? “ELK”是Elasticsearch、Logstash 和 Kibana
的缩写。下一个部分将介绍这些工具的简短描述。
- Elasticsearch:这是一个开源、分布式、RESTful、基于 JSON 的搜索引擎。它具有可扩展性、易于使用且灵活
- Logstash:这是一个服务器端数据处理管道,可同时从多个源获取数据,进行转换,然后将其发送到 Elasticsearch 等“存储”。
- Kibana 让用户可以在 Elasticsearch 中使用图表和图形可视化数据。
对于 RHEL 8,请参阅:
- 如何在 RHEL/CentOS 8 上安装 ELK Stack
请按照以下步骤在 CentOS 7/Fedora Linux 上安装和配置 ELK 堆栈工具。
第1步:安装Java/OpenJDK
由于 Elasticsearch 依赖于 Java,因此您需要在 CentOS 7/Fedora 系统上安装 Java。
sudo yum -y install java-openjdk-devel java-openjdk
通过检查版本确认安装:
$ java -version
openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment (Red_Hat-17.0.5.0.8-1.fc37) (build 17.0.5+8)
OpenJDK 64-Bit Server VM (Red_Hat-17.0.5.0.8-1.fc37) (build 17.0.5+8, mixed mode, sharing)
第二步:添加ELK仓库
安装 Java 后,添加提供 ELK 堆栈包的 ELK 堆栈存储库。
对于 Elasticsearch 8.x
cat <<EOF | sudo tee /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-8.x]
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=1
autorefresh=1
type=rpm-md
EOF
对于 Elasticsearch 7.x
cat <<EOF | sudo tee /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
对于 Elasticsearch 6.x
cat <<EOF | sudo tee /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-6.x]
name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
添加存储库后,导入 GPG 密钥:
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
清除并更新您的 YUM 包索引。
sudo yum clean all
sudo yum makecache
第 3 步:安装并配置 Elasticsearch
Elasticsearch 存储库已可供使用。您可以使用以下命令安装 Elasticsearch:
sudo yum -y install vim elasticsearch
确认包安装。
$ rpm -qi elasticsearch
Name : elasticsearch
Epoch : 0
Version : 8.2.0
Release : 1
Architecture: x86_64
Install Date: Thu May 19 20:56:11 2022
Group : Application/Internet
Size : 1115332284
License : Elastic License
Signature : RSA/SHA512, Wed Apr 20 12:55:44 2022, Key ID d27d666cd88e42b4
Source RPM : elasticsearch-8.2.0-1-src.rpm
Build Date : Wed Apr 20 10:42:41 2022
Build Host : packer-virtualbox-iso-1646848364
Relocations : /usr
Packager : Elasticsearch
Vendor : Elasticsearch
URL : https://www.elastic.co/
....
Elasticsearch 8
记下生成的安全信息:
--------------------------- 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 : tzV1Ju5fqnEy3B5+zc5G
If this node should join an existing cluster, you can reconfigure this with
'/usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token <token-here>'
after creating an enrollment token on your existing cluster.
You can complete the following actions at any time:
Reset the password of the elastic built-in superuser with
'/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic'.
Generate an enrollment token for Kibana instances with
'/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana'.
Generate an enrollment token for Elasticsearch nodes with
'/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node'.
-------------------------------------------------------------------------------------------------
要为elastic用户设置您自己的密码,请使用以下命令:
sudo /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic -i
您可以通过编辑以下文件来设置 JVM 选项,例如内存限制:/etc/elasticsearch/jvm.options
sudo vi /etc/elasticsearch/jvm.options
下面的示例设置总堆空间的初始/最大大小
-Xms1g
-Xmx1g
如果您的系统内存较少,您可以将其配置为使用小兆字节的内存。
-Xms256m
-Xmx512m
在启动时启动并启用elasticsearch服务:
$ sudo systemctl enable --now elasticsearch.service
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
测试以验证其是否正常工作:
Elasticsearch 8:
$ sudo curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200
Enter host password for user 'elastic':
{
"name" : "cent7.novalocal",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "5GFmuAkwQ4Sxrrrg4G-b6A",
"version" : {
"number" : "8.2.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "b174af62e8dd9f4ac4d25875e9381ffe2b9282c5",
"build_date" : "2022-04-20T10:35:10.180408517Z",
"build_snapshot" : false,
"lucene_version" : "9.1.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
Elasticsearch 7 及更低版本
$ curl http://127.0.0.1:9200
{
"name" : "bBzN5Kg",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "LKyqXXSvRvCpX9QAwKlP2Q",
"version" : {
"number" : "6.5.4",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "d2ef93d",
"build_date" : "2018-12-17T21:17:40.758843Z",
"build_snapshot" : false,
"lucene_version" : "7.5.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
创建测试索引:
$ curl -X PUT "http://127.0.0.1:9200/mytest_index"
{"acknowledged":true,"shards_acknowledged":true,"index":"mytest_index"}
第 4 步:安装并配置 Kibana
从添加的 Elasticsearch 存储库下载并安装 Kibana。
sudo yum -y install kibana
安装成功后,配置Kibana:
$ sudo vim /etc/kibana/kibana.yml
server.host: "0.0.0.0"
server.name: "kibana.example.com"
还设置elasticsearch主机
elasticsearch.hosts: ["http://localhost:9200"]
根据需要更改其他设置,然后启动 kibana 服务:
sudo systemctl enable --now kibana
访问 http://ip-address:5601 打开 Kibana 仪表板:
如果您有活动的防火墙服务,请允许 TCP 端口 5601
sudo firewall-cmd --add-port=5601/tcp --permanent
sudo firewall-cmd --reload
Kibana 8 配置
首先生成注册令牌
sudo /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
在 http://serverip:5601 上打开 Kibana Web 控制台并粘贴生成的令牌,然后点击“配置弹性”
生成验证码。运行以下命令来检索代码。
sudo /usr/share/kibana/bin/kibana-verification-code
输入命令输出中的代码
等待 Kibana 配置完成后再使用。
使用elastic作为之前配置的用户名和密码进行身份验证。
第5步:安装和配置Logstash
最后一个安装是针对 Logstash 的。它将充当客户端系统的集中式日志服务器,运行像 filebeat 这样的代理。
sudo yum -y install logstash
Logstash 自定义配置可以放置在 /etc/logstash/conf.d/
目录下。
有关更多详细信息,请参阅 Logstash 配置手册。
第 6 步:安装其他 ELK 工具 – 额外奖励
其他可以安装的 ELK 工具包括:
- Filebeat:轻量级日志传送器。它通过提供一种轻量级的方式来转发和集中日志和文件,帮助您让简单的事情变得简单
- Metricbeat:从您的系统和服务收集指标。从 CPU 到内存,从 Redis 到 NGINX 等等,Metricbeat 是一种发送系统和服务统计数据的轻量级方法。
- Packetbeat:网络数据的轻量级传送器
- Heartbeat:用于正常运行时间监控的轻量级托运器。它可以帮助您通过主动探测来监控服务的可用性
- Auditbeat:轻量级托运程序,可帮助您审核系统上用户和进程的活动
这些工具可以使用yum
包管理器使用各自的名称进行安装。下面的示例将安装所有 ELK 插件工具。
sudo yum install filebeat auditbeat metricbeat packetbeat heartbeat-elastic
请参阅官方 ELK 堆栈文档以及资源和培训,了解每种工具的配置和进一步阅读。
更多指南:
- 在 Ubuntu 上安装 Graylog 和 Elasticsearch
- 在 CentOS/RHEL 8 上安装 Graylog
- 在 CentOS 7 上安装 Elasticsearch 7