【Elastic Stack-初识篇】 ELK介绍、搭建最新 ELK 日志分析系统

简介: 【Elastic Stack-初识篇】 ELK介绍、搭建最新 ELK 日志分析系统

前言

大家好,我是无名小歌,欢迎加入云社区


今天给大家分享一个centos7系统搭建2022年最新ELK日志分析系统,目前版本是8.2.2。值得注意的是安装 ELK 时,您必须在整个ELK中使用相同的版本,如:Elasticsearch 8.2.2,则安装Kibana 8.2.2 和 Logstash 8.2.2,如果出现不对应的情况,如:Elasticsearch 是8.2.2版本、Kibana-6.8等或是其他版本,则需要进行对应版本的升级到8.2.2版本。

就说这么多,下面正式开始吧!!!


ELK日志分析系统(介绍)

  • Elasticsearch:ELK中最核心的是E(elasticsearch),我们可以从单词上理解翻译过来就是“弹性搜索”。提供搜索、分析、存储数据三大功能,特点稳定,可靠,快速(弹性)。
  • Logstash:一个具有实时流水线功能的开源数据收集引擎。Logstash 可以动态地统一来自不同来源的数据,并将数据规范化到您选择的目的地。为各种高级下游分析和可视化用例清理和普及所有数据。简单理解的话就是用来收集日志log的。
  • Kibana:ELK日志分析系统非常友好的 Web 界面,可以帮助我们搜索、观察、保护数据、分析数据、管理、监控和保护 Elastic Stack( Elasticsearch、Kibana、Beats 和 Logstash等组合在一起的统称)

日志是非结构化的数据(数据量大,不易查询),这也是使用ELK的主要原因之一。


环境准备

本次教程采用单节点方式

系统 内存 IP 网卡模式
Linux 12G 192.168.200.4 NAT

推荐内存12G,以下ELK系统占用内存为接近8G,所以只要大于8G应该问题不大,但为了保证体验推荐10G、12G。


部署ELK


Elasticsearch

  1. 调整进程最大打开文件数数量
cat >> /etc/security/limits.conf << EOF
hard nofile 65535
soft nofile 65535
EOF
  1. 调整进程最大虚拟内存区域数量
cat >> /etc/sysctl.conf << EOF
vm.max_map_count=262144
EOF
sysctl -p
  1. 安装公共签名密钥
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
  1. 配置 elasticsearch yum源
cat >> /etc/yum.repos.d/ELK.repo << EOF
[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
EOF
  1. 使用yum install命令安装,安装过程需要等待一段时间,因为下载的包有足足的502M(挺大的)

注意看安装完成后会出现 Security autoconfiguration information 字段(安全自动配置信息)

安装 Elasticsearch 时,默认启用并配置安全功能,会自动进行以下安全配置:

  • 启用身份验证和授权,并为elastic内置超级用户生成密码。
  • 为传输层和 HTTP 层生成 TLS 的证书和密钥,并使用这些密钥和证书启用和配置 TLS。

过程中如果出现下载中断,继续执行下列命令即可直至完成。

$ yum install --enablerepo=elasticsearch elasticsearch -y
...
...
--------------------------- Security autoconfiguration information ------------------------------
Authentication and authorization are enabled.
TLS for the transport and HTTP layers is enabled and configured.
#弹性内置超级用户生成的密码为:se_pNHcZwLawBfF7pxUs
The generated password for the elastic built-in superuser is : se_pNHcZwLawBfF7pxUs
#如果此节点应加入现有群集,则可以使用下列命令
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'.
#使用为Kibana实例生成注册令牌
Generate an enrollment token for Kibana instances with
 '/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana'.
#使用为Elasticsearch节点生成注册令牌
Generate an enrollment token for Elasticsearch nodes with
'/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node'.
-------------------------------------------------------------------------------------------------
### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
#自启动命令
 sudo systemctl daemon-reload
 sudo systemctl enable elasticsearch.service
### You can start elasticsearch service by executing
#启动命令
 sudo systemctl start elasticsearch.service
  1. 启动Elasticsearch

使用上述生成的启动命令,启动也需要等待一段时间,这里内存占用大约6G

sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
sudo systemctl start elasticsearch.service
  1. 测试Elasticsearch

这里你会发现我们浏览器使用http协议是不能访问的,并不是我们服务有问题。

本地宿主机访问Elasticsearch,服务端口9200:

$ curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200
Enter host password for user 'elastic':
{
  "name" : "node1",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "xuxHYu-0SN-PFcghs8V0ow",
  "version" : {
    "number" : "8.2.2",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "9876968ef3c745186b94fdabd4483e01499224ef",
    "build_date" : "2022-05-25T15:47:06.259735307Z",
    "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,服务端口9200,如:https://192.168.200.4:9200

浏览器访问请注意,Elasticsearch-8.2.2版本,如果使用http协议是无法访问的必须使用https协议。

用户名:elastic

密码:se_pNHcZwLawBfF7pxUs

注意:这里浏览器会出现提示不安全的情况(不要慌)依次点击:高级–>接收风险,就会跳转到登录界面了。

密码对应弹性内置超级用户生成的密码。

出现和本地访问的内容是一致的,这样我们的Elasticsearch安装完成。

Kibana

  1. 配置 Kibana yum源
cat >> /etc/yum.repos.d/ELK.repo << EOF
[kibana-8.x]
name=Kibana 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
  1. 使用yum install命令安装,安装过程也需要等待一段时间(248M)

过程中如果出现下载中断,继续执行下列命令即可直至完成。

yum install kibana -y
  1. 修改配置文件 /etc/kibana/kibana.yml

解除注释:server.port、server.host

server.host:填写自己主机ip

  1. 启动Kibana
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable kibana.service
sudo systemctl start kibana.service
  1. 测试Kibana
    启动等待一分钟后,浏览器访问Kibana,服务端口5601,如:http://192.168.200.4:5601/

访问Kibana时,使用http协议是可以访问的,https协议不能访问。

如下图:kibana-8.2.2版本需要令牌登录,下面使用命令生成令牌。

注意:如果访问时出现下列问题,点击解决方法

  1. 使用elasticsearch-create-enrollment-token命令为Kibana生成一个注册令牌:

输入注册令牌,将 Kibana 与 Elasticsearch 连接起来。

下列命令是安装elasticsearch时安全自动配置信息中所生成。

/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
  1. 使用kibana-verification-code命令生成验证码

输入验证码之后,kibana会进行:保存设置–>启动elastic–>完成设置操作。

$ /usr/share/kibana/bin/kibana-verification-code
Your verification code is:  409 686

  1. 输入用户密码登录

用户密码使用安装elasticsearch时生成的用户密码。

用户名:elastic

密码:se_pNHcZwLawBfF7pxUs

Logstash

  1. 配置 Logstash yum源
cat >> /etc/yum.repos.d/ELK.repo << EOF
[logstash-8.x]
name=Elastic 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
  1. 使用yum install命令安装,安装过程也需要等待一段时间(322 M)

过程中如果出现下载中断,继续执行下列命令即可直至完成。

yum install logstash -y
  1. 测试Logstash

我们通过运行最基本的Logstash Pipeline来测试。

Logstash 管道具有两个必需元素input和output和一个可选元素filter。


运行步骤:输入插件使用来自源的数据,过滤器插件根据您的指定修改数据,输出插件将数据写入目标。

运行 Logstash Pipeline

输出内容第8行出现 Successfully started Logstash API endpoint,说明成功启动Logstash


stdin 插件出现等待输入提示(The stdin plugin is now waiting for input:),输入 hello world 测试。


Ctrl+D 退出 Logstash Pipeline

[root@node1 ~]# /usr/share/logstash/bin/logstash -e 'input { stdin { } } output { stdout {} }'
Using bundled JDK: /usr/share/logstash/jdk
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
WARNING: Could not find logstash.yml which is typically located in $LS_HOME/config or /etc/logstash. You can specify the path using --path.settings. Continuing using the defaults
Could not find log4j2 configuration at path /usr/share/logstash/config/log4j2.properties. Using default config which logs errors to the console
[INFO ] 2022-05-30 10:19:11.092 [main] runner - Starting Logstash {"logstash.version"=>"8.2.2", "jruby.version"=>"jruby 9.2.20.1 (2.5.8) 2021-11-30 2a2962fbd1 OpenJDK 64-Bit Server VM 11.0.14.1+1 on 11.0.14.1+1 +indy +jit [linux-x86_64]"}
[INFO ] 2022-05-30 10:19:11.110 [main] runner - JVM bootstrap flags: [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djruby.compile.invokedynamic=true, -Djruby.jit.threshold=0, -XX:+HeapDumpOnOutOfMemoryError, -Djava.security.egd=file:/dev/urandom, -Dlog4j2.isThreadContextMapInheritable=true, -Djruby.regexp.interruptible=true, -Djdk.io.File.enableADS=true, --add-opens=java.base/java.security=ALL-UNNAMED, --add-opens=java.base/java.io=ALL-UNNAMED, --add-opens=java.base/java.nio.channels=ALL-UNNAMED, --add-opens=java.base/sun.nio.ch=ALL-UNNAMED, --add-opens=java.management/sun.management=ALL-UNNAMED]
[WARN ] 2022-05-30 10:19:12.090 [LogStash::Runner] multilocal - Ignoring the 'pipelines.yml' file because modules or command line options are specified
[INFO ] 2022-05-30 10:19:15.968 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600, :ssl_enabled=>false}
[INFO ] 2022-05-30 10:19:17.783 [Converge PipelineAction::Create<main>] Reflections - Reflections took 272 ms to scan 1 urls, producing 120 keys and 395 values
[INFO ] 2022-05-30 10:19:19.375 [Converge PipelineAction::Create<main>] javapipeline - Pipeline `main` is configured with `pipeline.ecs_compatibility: v8` setting. All plugins in this pipeline will default to `ecs_compatibility => v8` unless explicitly configured otherwise.
[INFO ] 2022-05-30 10:19:19.723 [[main]-pipeline-manager] javapipeline - Starting pipeline {:pipeline_id=>"main", "pipeline.workers"=>4, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>50, "pipeline.max_inflight"=>500, "pipeline.sources"=>["config string"], :thread=>"#<Thread:0x7c068ea7 run>"}
[INFO ] 2022-05-30 10:19:21.346 [[main]-pipeline-manager] javapipeline - Pipeline Java execution initialization time {"seconds"=>1.61}
[INFO ] 2022-05-30 10:19:21.520 [[main]-pipeline-manager] javapipeline - Pipeline started {"pipeline.id"=>"main"}
# stdin 插件出现等待输入提示,请输入hello world
The stdin plugin is now waiting for input:
hello world
{
      "@version" => "1",
         "event" => {
        "original" => "hello world"
    },
       "message" => "hello world",
    "@timestamp" => 2022-05-30T02:27:01.888886Z,
          "host" => {
        "hostname" => "node1"
    }
}

ELK-8.2.2版本日志分析系统自此搭建完成!!!


总结

我是无名小歌,欢迎大家一键三连、加入云社区

(⊙o⊙),我们下期再见!!!


参考资料

https://www.elastic.co/guide/index.html


相关实践学习
通过日志服务实现云资源OSS的安全审计
本实验介绍如何通过日志服务实现云资源OSS的安全审计。
目录
相关文章
WGLOG日志管理系统是怎么收集日志的
WGLOG通过部署Agent客户端采集日志,Agent持续收集指定日志文件并上报Server,Server负责展示与分析。Agent与Server需保持相同版本。官网下载地址:www.wgstart.com
|
6月前
|
Prometheus 监控 Cloud Native
基于docker搭建监控系统&日志收集
Prometheus 是一款由 SoundCloud 开发的开源监控报警系统及时序数据库(TSDB),支持多维数据模型和灵活查询语言,适用于大规模集群监控。它通过 HTTP 拉取数据,支持服务发现、多种图表展示(如 Grafana),并可结合 Loki 实现日志聚合。本文介绍其架构、部署及与 Docker 集成的监控方案。
549 122
基于docker搭建监控系统&日志收集
|
6月前
|
消息中间件 Java Kafka
搭建ELK日志收集,保姆级教程
本文介绍了分布式日志采集的背景及ELK与Kafka的整合应用。传统多服务器环境下,日志查询效率低下,因此需要集中化日志管理。ELK(Elasticsearch、Logstash、Kibana)应运而生,但单独使用ELK在性能上存在瓶颈,故结合Kafka实现高效的日志采集与处理。文章还详细讲解了基于Docker Compose构建ELK+Kafka环境的方法、验证步骤,以及如何在Spring Boot项目中整合ELK+Kafka,并通过Logback配置实现日志的采集与展示。
1100 64
搭建ELK日志收集,保姆级教程
|
9月前
|
监控 API 开发工具
HarmonyOS Next的HiLog日志系统完全指南:从入门到精通
本文深入解析HarmonyOS Next的HiLog日志系统,涵盖日志级别、核心API、隐私保护与高级回调功能,助你从入门到精通掌握这一重要开发工具。
|
6月前
|
Ubuntu
在Ubuntu系统上设置syslog日志轮替与大小限制
请注意,在修改任何系统级别配置之前,请务必备份相应得原始档案并理解每项变更可能带来得影响。
721 2
|
12月前
|
数据可视化 关系型数据库 MySQL
ELK实现nginx、mysql、http的日志可视化实验
通过本文的步骤,你可以成功配置ELK(Elasticsearch, Logstash, Kibana)来实现nginx、mysql和http日志的可视化。通过Kibana,你可以直观地查看和分析日志数据,从而更好地监控和管理系统。希望这些步骤能帮助你在实际项目中有效地利用ELK来处理日志数据。
855 90
|
存储 前端开发 数据可视化
Grafana Loki,轻量级日志系统
本文介绍了基于Grafana、Loki和Alloy构建的轻量级日志系统。Loki是一个由Grafana Labs开发的日志聚合系统,具备高可用性和多租户支持,专注于日志而非指标,通过标签索引而非内容索引实现高效存储。Alloy则是用于收集和转发日志至Loki的强大工具。文章详细描述了系统的架构、组件及其工作流程,并提供了快速搭建指南,包括准备步骤、部署命令及验证方法。此外,还展示了如何使用Grafana查看日志,以及一些基本的LogQL查询示例。最后,作者探讨了Loki架构的独特之处,提出了“巨型单体模块化”的概念,即一个应用既可单体部署也可分布式部署,整体协同实现全部功能。
4769 69
Grafana Loki,轻量级日志系统
|
8月前
|
存储
WGLOG日志管理系统可以采集网络设备的日志吗
WGLOG日志审计系统提供开放接口,支持外部获取日志内容后发送至该接口,实现日志的存储与分析。详情请访问:https://www.wgstart.com/wglog/docs9.html
|
12月前
|
存储 消息中间件 缓存
MiniMax GenAI 可观测性分析 :基于阿里云 SelectDB 构建 PB 级别日志系统
基于阿里云SelectDB,MiniMax构建了覆盖国内及海外业务的日志可观测中台,总体数据规模超过数PB,日均新增日志写入量达数百TB。系统在P95分位查询场景下的响应时间小于3秒,峰值时刻实现了超过10GB/s的读写吞吐。通过存算分离、高压缩比算法和单副本热缓存等技术手段,MiniMax在优化性能的同时显著降低了建设成本,计算资源用量降低40%,热数据存储用量降低50%,为未来业务的高速发展和技术演进奠定了坚实基础。
524 1
MiniMax GenAI 可观测性分析 :基于阿里云 SelectDB 构建 PB 级别日志系统
|
12月前
|
网络安全
window系统下安装elk
本文介绍了Elasticsearch、Logstash和Kibana(统称ELK栈)8.17.3版本的安装与配置流程。主要内容包括: - **Elasticsearch**:详细描述了从下载到启动服务的步骤,以及`elasticsearch.yml`的关键配置项,并提供了Postman操作示例及常见问题解决方案。 - **Logstash**:涵盖了插件安装、配置文件`logstash.conf`编写及其启动命令。 - **Kibana**:讲解了下载、配置`kibana.yml`和启动过程,确保与Elasticsearch正确连接。