【Elastic Stack】 搭建最新 ELK 日志分析系统 8.2.2版

3,838

前言

大家好,我是无名小歌。
今天给大家分享一个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网卡模式
Linux12G192.168.200.4NAT

推荐内存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版本日志分析系统自此搭建完成!!!

参考资料

www.elastic.co/guide/index…