ELK Stack 安装教程,RHEL及衍生系统。

本文最后更新于 2023年11月30日 下午

介绍

“ELK”是三个开源项目的首字母缩写,这三个项目分别是:Elasticsearch、Logstash 和 Kibana。Elasticsearch 是一个搜索和分析引擎。Logstash 是服务器端数据处理管道,能够同时从多个来源采集数据,转换数据,然后将数据发送到诸如 Elasticsearch 等“存储库”中。Kibana 则可以让用户在 Elasticsearch 中使用图形和图表对数据进行可视化。

目前最新的名字叫 Elastic Stack 是 ELK Stack 的更新换代产品。(增加了各种Beats)

示意图

注意:本文章主要用于syslog类型的日志收集,取消ES的加密访问是为了方便接入frostmourne进行日志告警。

安装环境

系统: AlmaLinux 9 (关闭SE Linux)

Java:OpenJDK 19 (从RPM 8.5版本开始使用内置19版本)

浏览器:Firefox、Chrome、Safari、Edge(使用最新版)

软件版本,使用ELK 8.5+

也适用于其他RHEL8-9版本衍生版系统:如Centos Stream、Rocky Linux等。

兼容性查询:https://www.elastic.co/cn/support/matrix

PS: 参考 https://www.skynemo.cn/archives/base-on-centos79-install-elasticsearch81-standalone-and-config#

官网文档:https://www.elastic.co/guide/en/elasticsearch/reference/master/rpm.html

官网下载地址

Download Elasticsearch | Elastic

下载RPM包

使用清华大学镜像源进行安装

1
2
3
4
5
6
7
8
9
vim /etc/yum.repos.d/elasticsearch.repo

[elasticsearch]
name=Elasticsearch repository for 8.x packages
baseurl=https://mirrors.tuna.tsinghua.edu.cn/elasticstack/8.x/yum
gpgcheck=0
enabled=1
autorefresh=1
type=rpm-md

#创建elasticsearch.repo 至 /etc/yum.repos.d/中,注意安装前检查SELinux是否关闭。

#注意:关闭了GPG认证

PS: [使用 RPM |安装 Elasticsearch弹性搜索指南 ]](https://www.elastic.co/guide/en/elasticsearch/reference/8.11/rpm.html#rpm-repo)

1
dnf install elasticsearch logstash kibana

使用dnf进行安装

1
systemctl enable --now elasticsearch.service kibana.service logstash.service

#启动并设定开机启动。

默认初始化配置

使用dnf安装的时候,需要检查输入日志,会有类似以下的输出。

默认开启HTTPS,并生成默认管理员账号elastic账号的密码。可以使用下面命令进行重置密码。

1
/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic

可以使用下面的命令生成 Kibana 访问Elasticsearch的令牌。

1
/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
--------------------------- 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 : wm7khlFyj3VrHKOtb5S9

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'.

访问测试

https://192.168.198.6:9200 使用HTTPS访问测试,用管理员账号密码。

Elasticsearch 配置

切换客户API为不加密,HTTP访问。

1
vim /etc/elasticsearch/elasticsearch.yml 

打开文件

1
2
3
4
# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
enabled: false
keystore.path: certs/http.p12

修改上述配置为 false.

1
systemctl restart elasticsearch.service  

重启服务,以生效配置。

访问 http://192.168.198.6:9200 ,使用http测试。

日志告警

关闭https是为了兼容frostmourne做日志告警。

教程:https://songxwn.com/frostmourne_install/

Kibana 配置

Kibana 数据都存储在ES数据库,且为data角色的。

默认配置

默认为 5601 端口,默认绑定为127.0.0.1 地址。使用HTTP协议。

可以修改kibana.yml 文件修改绑定地址,或者使用Nginx反向代理。

修改语言

1
vim /etc/kibana/kibana.yml  

vim修改配置文件

1
2
3
i18n.locale: "zh-CN"
server.host: "0.0.0.0"

修改此行为中文配置,重启服务生效。第二行为配置监听IP.

基本认证配置

1
2
3
4
5
6
7
8
9
10
# =================== System: Elasticsearch ===================
# The URLs of the Elasticsearch instances to use for all your queries.
elasticsearch.hosts: ["http://127.0.0.1:9200"]

# If your Elasticsearch is protected with basic authentication, these settings provide
# the username and password that the Kibana server uses to perform maintenance on the Kibana
# index at startup. Your Kibana users still need to authenticate with Elasticsearch, which
# is proxied through the Kibana server.
elasticsearch.username: "kibana_system"
elasticsearch.password: "he5l=fIIbD1WhD_=aNg_"

修改文件,配置ES访问地址,配置系统默认账号kibana_system的密码,填入配置文件。(必须为此账号)

1
2
3
4
5

/usr/share/elasticsearch/bin/elasticsearch-reset-password -u kibana_system

# 重置密码

PS: 参考 https://www.elastic.co/guide/en/elasticsearch/reference/current/security-minimal-setup.html#add-built-in-users

Logstash 配置

开启syslog,将数据转发给ES.

vim /etc/logstash/conf.d/syslog.conf

创建修改syslog配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

input {
syslog {
type => "system-syslog"
port => 514
}
}

output {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "system-syslog-%{+YYYY.MM}"
user => "elastic"
password => "2H*Y9rL5D=xFbvLAz_Nr"
}
}

配置为syslog输入,端口为默认的514。

将数据输出到ES,配置索引前缀为 system-syslog,按月分割索引。

注意 中括号。

修改systemd服务文件。

1
2
3
4
5
6
vim /lib/systemd/system/logstash.service

[Service]
Type=simple
User=root
Group=root
1
2
3
4
5

systemctl daemon-reload

# 重载

需要修改启动用户为ROOT才能使用小于1024的端口号。(linux对于非root权限用户不能使用1024以下的端口,)

Kibana WEB配置

创建数据视图

在主菜单-Stack Management-Kibana-数据视图里面创建

参考文档: https://www.elastic.co/guide/en/kibana/current/data-views.html

使用类似 syslog* 匹配索引和日期。

索引管理

在主菜单-Stack Management-数据-索引管理里面查看所有索引。

数据查看

路径:http://127.0.0.1:5601/app/discover (选择数据视图查看)

PS: 主机名字段问题

https://songxwn.com/ELK-logstash-hostname/


ELK Stack 安装教程,RHEL及衍生系统。
https://songxwn.com/elk/
作者
Song
发布于
2022年8月29日
更新于
2023年11月30日
许可协议