ELK 是三个开源框架的简写,分别是:Elasticsearch、Logstash、Kibana 。
框架 | 简介 | 作用 |
---|---|---|
Elasticsearch | 开源分布式搜索引擎,提供存储、分析、搜索功能。特点:分布式、基于reasful风格、支持海量高并发的准实时搜索场景、稳定、可靠、快速、使用方便等。 | 接收搜集的海量结构化日志数据,并提供给Kibana查询分析。 |
Logstash | 开源日志搜集、分析、过滤框架,支持多种数据输入输出方式。 | 用于收集日志,对日志进行过滤形成结构化数据,并转发到Elasticsearch中。 |
Kibana | 开源日志报表系统,对Elasticsearch以及Logstash有良好的web页面支持。 | 对Elasticsearch提供的数据进行分析展示。 |
官网下载地址:/cn/downloads/elasticsearch
国内镜像源(华为):mirrors.huaweicloud/elasticsearch/
推荐使用国内的下载地址,官网下载太慢了。
选择合适的版本下载(推荐下载自带 JDK 的版本,否者自己配置的 JDK 可能不符合版本要求。注意:Elasticsearch 会优先使用系统配置的 JDK 。可将 Elasticsearch 自带的 JDK 配置到系统的环境变量中,如果不这样做的话,在安装 Logstash 时,启动会报没有配置 JDK 环境变量的错误。)
[root@localhost ~]# wget mirrors.huaweicloud/elasticsearch/7.8.0/elasticsearch-7.8.0-linux-x86_
# 新建文件夹
[root@localhost ~]# mkdir /usr/local/elasticsearch
# 解压到指定文件夹
[root@localhost ~]# tar -zxvf elasticsearch-7.8.0-linux-x86_ -C /usr/local/elasticsearch/
# 进入安装目录
[root@localhost ~]# cd /usr/local/elasticsearch/elasticsearch-7.8.0/
# 修改l
[root@localhost elasticsearch-7.8.0]# vim ./l
# 修改以下几项:
node.name: node-1 # 设置节点名
network.host: 0.0.0.0 # 允许外部 ip 访问
cluster.initial_master_nodes: ["node-1"] # 设置集群初始主节点
ES为了安全考虑不允许使用root用户启动ElasticSearch,所以需要新建一个普通用户启动程序。
# 添加用户 es
[root@localhost elasticsearch-7.8.0]# adduser es
# 设置用户 es 的密码(需要输入两遍密码)
# (如果设置密码过于简单可能会提示 BAD PASSWORD: XXX ,如果是用 root 用户操作可忽略提示继续输入第二遍密码强制设置密码)
[root@localhost elasticsearch-7.8.0]# passwd es
Changing password for user es.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
# 将对应的文件夹权限赋予用户 es
[root@localhost elasticsearch-7.8.0]# chown -R es /usr/local/elasticsearch
# 切换至用户 es
[root@localhost elasticsearch-7.8.0]# su es
# 启动 ElasticSearch (-d 表示在后台启动)
[es@localhost elasticsearch-7.8.0]$ ./bin/elasticsearch -d
启动之后可能会报以下三个错误:
ERROR: [3] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max number of threads [3795] for user [es] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决方式:
需切换到root用户解决错误:
# 切换到 root 用户
[es@localhost elasticsearch-7.8.0]$ su root
[1] 和 [2] 的解决方法:
# 修改 /etc/f 文件
[root@localhost elasticsearch-7.8.0]# vim /etc/f
# 添加以下四行
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
[3] 的解决方法:
# 修改 /f 文件
[root@localhost elasticsearch-7.8.0]# vim /f
# 添加下面一行
vm.max_map_count=655360
# 执行命令
[root@localhost elasticsearch-7.8.0]# sysctl -p
切换到用户 es 重新启动程序就可以了。
注意: 防火墙需要开放9200端口
[root@localhost elasticsearch-7.8.0]# firewall-cmd --permanent --add-port=9200/tcp
success
[root@localhost elasticsearch-7.8.0]# firewall-cmd --permanent --add-port=9200/udp
success
[root@localhost elasticsearch-7.8.0]# firewall-cmd --reload
success
访问:192.168.40.149:9200
返回结果:
{
"name": "node-1",
"cluster_name": "elasticsearch",
"cluster_uuid": "bZD6PjHIQ1uovZrQ6nXa8Q",
"version": {
"number": "7.8.0",
"build_flavor": "default",
"build_type": "tar",
"build_hash": "757314695644ea9a1dc2fecd26d1a43856725e65",
"build_date": "2020-06-14T19:35:50.234439Z",
"build_snapshot": false,
"lucene_version": "8.5.1",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}
华为镜像源:mirrors.huaweicloud/kibana
选择合适的版本下载(和Elasticsearch版本保持一致)。
# 下载安装包
[root@localhost ~]# wget mirrors.huaweicloud/kibana/7.8.0/kibana-7.8.0-linux-x86_
# 解压到当前目录
[root@localhost ~]# tar -zxvf kibana-7.8.0-linux-x86_
# 重命名并移动到指定目录
[root@localhost ~]# mv ./kibana-7.8.0-linux-x86_64 /usr/local/kibana-7.8.0
[root@localhost kibana-7.8.0]# vim ./l
# 服务端口
server.port: 5601
# 服务器ip 本机
server.host: "0.0.0.0"
# Elasticsearch 服务地址
elasticsearch.hosts: ["localhost:9200"]
# 设置语言为中文
i18n.locale: "zh-CN"
给 es 用户授予 kibana 目录的权限。
# 授权
[root@localhost ~]# chown -R es /usr/local/kibana-7.8.0
# 切换用户
root@localhost ~]# su es
注意:启动 Kibana 之前需要先启动 Elasticsearch
需要先配置防火墙打开5601端口:
[root@localhost kibana-7.8.0]# firewall-cmd --permanent --add-port=5601/tcp
success
[root@localhost kibana-7.8.0]# firewall-cmd --permanent --add-port=5601/udp
success
[root@localhost kibana-7.8.0]# firewall-cmd --reload
success
123456
# 前台启动方式
[es@localhost kibana-7.8.0]$ ./bin/kibana
# 后台启动方式
[es@localhost kibana-7.8.0]$ nohup ./bin/kibana &
访问地址:192.168.40.149:5601/
华为镜像源:mirrors.huaweicloud/logstash
选择合适版本的安装包(和 Elasticsearch 保持一致)。
[root@localhost ~]# wget mirrors.huaweicloud/logstash/7.8.0/logstash-7.8.
# 解压安装包
[root@localhost ~]# tar -zxvf logstash-7.8.
# 移动到指定目录
[root@localhost ~]# mv ./logstash-7.8.0 /usr/local/logstash-7.8.0
根据原有的 f 配置文件复制出一个新的配置文件并修改。
[root@localhost logstash-7.8.0]# cp f f
# 修改配置文件f
[root@localhost logstash-7.8.0]# vim f
修改成如下内容:
input {
tcp {
port => 9601
codec => json_lines
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
}
stdout {
codec => rubydebug
}
}
配置文件f内容说明:
input { # input输入源配置
tcp { # 使用tcp输入源
port => 9601 # 服务器监听端口9061接收日志,默认ip localhost
codec => json_lines # 使用json解析日志 需要安装json解析插件
}
}
output { # output 数据输出配置
elasticsearch { # 使用elasticsearch接收
hosts => ["localhost:9200"] # 集群地址 多个用,隔开
}
stdout {
codec => rubydebug # 输出到命令窗口
}
}
由于国内无法访问默认的gem source,需要将gem source改为国内的源。
# 修改Gemfile
[root@localhost logstash-7.8.0]# vim Gemfile
# 将source这一行改成如下所示:
source "ruby.taobao"
[root@localhost logstash-7.8.0]# ./bin/logstash-plugin install logstash-codec-json_lines
如果报以下错误,请检查是否已经配置了 JDK 环境变量。
could not find java; set JAVA_HOME or ensure java is in PATH
配置 JDK 环境变量可参考此文章:Linux 安装并配置JDK环境变量
# 后台启动Logstash
[root@localhost logstash-7.8.0]# nohup ./bin/logstash -f ./f &
本示例使用的是 Spring Boot 项目。
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.11</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!--指定logstash ip:监听端口-->
<destination>192.168.40.149:9601</destination>
<encoder charset="UTF-8" class="net.der.LogstashEncoder" />
</appender>
<!--引用springboot默认配置-->
<include resource="org/springframework/boot/logging/l"/>
<root level="INFO">
<!--使用上述订阅logstash数据tcp传输 -->
<appender-ref ref="LOGSTASH" />
<!--使用springboot默认配置 调试窗口输出-->
<appender-ref ref="CONSOLE" />
</root>
</configuration>
启动 Spring Boot 项目:
Kibana还需要配置日志读取的索引:
在Kinaba中查看结果:
本文发布于:2025-02-23 23:22:00,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/1740324130579869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |