ELK简介

ELK是Elasticsearch、Logstash、Kibana三大开源框架首字母大写简称(然而前期呈现的Filebeat(beats中的一种)能够用来代替Logstash的数据收集性能,比拟轻量级)。市面上也被成为Elastic Stack。

Filebeat是用于转发和集中日志数据的轻量级传送工具。Filebeat监督您指定的日志文件或地位,收集日志事件,并将它们转发到Elasticsearch或 Logstash进行索引。Filebeat的工作形式如下:启动Filebeat时,它将启动一个或多个输出,这些输出将在为日志数据指定的地位中查找。对于Filebeat所找到的每个日志,Filebeat都会启动收集器。每个收集器都读取单个日志以获取新内容,并将新日志数据发送到libbeat,libbeat将汇集事件,并将汇集的数据发送到为Filebeat配置的输入。

Logstash是收费且凋谢的服务器端数据处理管道,可能从多个起源采集数据,转换数据,而后将数据发送到您最喜爱的“存储库”中。Logstash可能动静地采集、转换和传输数据,不受格局或复杂度的影响。利用Grok从非结构化数据中派生出构造,从IP地址解码出地理坐标,匿名化或排除敏感字段,并简化整体处理过程。

Elasticsearch是Elastic Stack外围的分布式搜寻和剖析引擎,是一个基于Lucene、分布式、通过Restful形式进行交互的近实时搜寻平台框架。Elasticsearch为所有类型的数据提供近乎实时的搜寻和剖析。无论您是结构化文本还是非结构化文本,数字数据或天文空间数据,Elasticsearch都能以反对疾速搜寻的形式无效地对其进行存储和索引。

Kibana是一个针对Elasticsearch的开源剖析及可视化平台,用来搜寻、查看交互存储在Elasticsearch索引中的数据。应用Kibana,能够通过各种图表进行高级数据分析及展现。并且能够为Logstash和ElasticSearch提供的日志剖析敌对的 Web 界面,能够汇总、剖析和搜寻重要数据日志。还能够让海量数据更容易了解。它操作简略,基于浏览器的用户界面能够疾速创立仪表板(Dashboard)实时显示Elasticsearch查问动静

残缺日志零碎基本特征

收集:可能采集多种起源的日志数据

传输:可能稳固的把日志数据解析过滤并传输到存储系统

存储:存储日志数据

剖析:反对UI剖析

正告:可能提供错误报告,监控机制

装置jdk17环境

root@elk:~# mkdir jdkroot@elk:~# cd jdkroot@elk:~/jdk# wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gzroot@elk:~/jdk# tar xf jdk-17_linux-x64_bin.tar.gzroot@elk:~/jdk# cd ..root@elk:~#root@elk:~# mv jdk/ /root@elk:~# vim /etc/profileroot@elk:~#root@elk:~#root@elk:~# tail -n 4 /etc/profileexport JAVA_HOME=/jdk/jdk-17.0.1/export PATH=$JAVA_HOME/bin:$PATHexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarroot@elk:~#root@elk:~# source /etc/profileroot@elk:~# chmod -R 777  /jdk/

创立elk文件夹,并下载所需包

root@elk:~# mkdir elkroot@elk:~# cd elkroot@elk:~/elk# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.15.1-linux-x86_64.tar.gzroot@elk:~/elk# wget https://artifacts.elastic.co/downloads/kibana/kibana-7.15.1-linux-x86_64.tar.gzroot@elk:~/elk# wget https://artifacts.elastic.co/downloads/logstash/logstash-7.15.1-linux-x86_64.tar.gz

解压安装包

root@elk:~/elk# tar xf elasticsearch-7.15.1-linux-x86_64.tar.gzroot@elk:~/elk# tar xf kibana-7.15.1-linux-x86_64.tar.gzroot@elk:~/elk# tar xf logstash-7.15.1-linux-x86_64.tar.gzroot@elk:~/elk# lltotal 970288drwxr-xr-x  5 root root      4096 Oct 20 06:09 ./drwx------  7 root root      4096 Oct 20 06:04 ../drwxr-xr-x  9 root root      4096 Oct  7 22:00 elasticsearch-7.15.1/-rw-r--r--  1 root root 340849929 Oct 14 13:28 elasticsearch-7.15.1-linux-x86_64.tar.gzdrwxr-xr-x 10 root root      4096 Oct 20 06:09 kibana-7.15.1-linux-x86_64/-rw-r--r--  1 root root 283752241 Oct 14 13:34 kibana-7.15.1-linux-x86_64.tar.gzdrwxr-xr-x 13 root root      4096 Oct 20 06:09 logstash-7.15.1/-rw-r--r--  1 root root 368944379 Oct 14 13:38 logstash-7.15.1-linux-x86_64.tar.gz

创立用户并设置权限

root@elk:~/elk# cdroot@elk:~# useradd elkroot@elk:~# mkdir /home/elkroot@elk:~# cp -r elk/ /home/elk/root@elk:~# chown -R elk:elk /home/elk/

批改零碎配置文件

root@elk:~# vim /etc/security/limits.confroot@elk:~#root@elk:~#root@elk:~# tail -n 3 /etc/security/limits.conf*       soft    nofile          65536*       hard    nofile          65536root@elk:~#root@elk:~# vim /etc/sysctl.confroot@elk:~#root@elk:~# tail -n 2 /etc/sysctl.confvm.max_map_count=262144root@elk:~#root@elk:~# sysctl -pvm.max_map_count = 262144root@elk:~#

批改elk配置文件

root@elk:~# su - elk$ bashelk@elk:~$ cd /elk/elasticsearch-7.15.1/configelk@elk:~/elk/elasticsearch-7.15.1/config$ vim elasticsearch.ymlelk@elk:~/elk/elasticsearch-7.15.1/config$elk@elk:~/elk/elasticsearch-7.15.1/config$ tail -n 20 elasticsearch.yml#设置data寄存的门路为/data/es-datapath.data: /home/elk/data/#设置logs日志的门路为/log/es-logpath.logs: /home/elk/data/#设置内存不应用替换分区bootstrap.memory_lock: false#配置了bootstrap.memory_lock为true时反而会引发9200不会被监听,起因不明#设置容许所有ip能够连贯该elasticsearchnetwork.host: 0.0.0.0#开启监听的端口为9200http.port: 9500#减少新的参数,为了让elasticsearch-head插件能够拜访es (5.x版本,如果没有能够本人手动加)http.cors.enabled: truehttp.cors.allow-origin: "*"cluster.initial_master_nodes: ["elk"]node.name: elkroot@elk:~/elk/elasticsearch-7.15.1/config#

应用elk用户去启动elasticsearch

root@elk:~# su - elk$ bashelk@elk:~$elk@elk:~$ mkdir dataelk@elk:~/elk/elasticsearch-7.15.1/bin$ cdelk@elk:~$ cd /home/elk/elk/elasticsearch-7.15.1/binelk@elk:~/elk/elasticsearch-7.15.1/bin$ ./elasticsearch

启动之后拜访测试:

root@elk:~# curl -I http://192.168.1.19:9500/HTTP/1.1 200 OKX-elastic-product: ElasticsearchWarning: 299 Elasticsearch-7.15.1-83c34f456ae29d60e94d886e455e6a3409bba9ed "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.15/security-minimal-setup.html to enable security."content-type: application/json; charset=UTF-8content-length: 532root@elk:~#

放到后盾运行

elk@elk:~/elk/elasticsearch-7.15.1/bin$ nohup /home/elk/elk/elasticsearch-7.15.1/bin/elasticsearch >> /home/elk/elk/elasticsearch-7.15.1/output.log 2>&1 &[1] 8811elk@elk:~/elk/elasticsearch-7.15.1/bin$
elk@elk:~$ cd elk/kibana-7.15.1-linux-x86_64/config/elk@elk:~/elk/kibana-7.15.1-linux-x86_64/config$ vim kibana.ymlelk@elk:~/elk/kibana-7.15.1-linux-x86_64/config$ tail -n 18 kibana.yml#设置监听端口为5601server.port: 5601#设置可拜访的主机地址server.host: "0.0.0.0"#设置elasticsearch主机地址elasticsearch.hosts: ["http://localhost:9500"]#如果elasticsearch设置了用户名明码,那么须要配置该两项,如果没配置,那就不必管#elasticsearch.username: "user"#elasticsearch.password: "pass"elk@elk:~/elk/kibana-7.15.1-linux-x86_64/config$elk@elk:~$ cd /home/elk/elk/kibana-7.15.1-linux-x86_64/binelk@elk:~/elk/kibana-7.15.1-linux-x86_64/bin$ ./kibana

测试拜访

root@elk:~# curl -I http://192.168.1.19:5601/app/home#/tutorial_directoryHTTP/1.1 200 OKcontent-security-policy: script-src 'unsafe-eval' 'self'; worker-src blob: 'self'; style-src 'unsafe-inline' 'self'x-content-type-options: nosniffreferrer-policy: no-referrer-when-downgradekbn-name: elkkbn-license-sig: aaa69ea6a0792153cde61e88d0cd9bbad7ddcdaec87b613f281dd275e9dbad47content-type: text/html; charset=utf-8cache-control: private, no-cache, no-store, must-revalidatecontent-length: 144351vary: accept-encodingDate: Wed, 20 Oct 2021 07:11:10 GMTConnection: keep-aliveKeep-Alive: timeout=120root@elk:~#

放到后盾运行

elk@elk:~/elk/kibana-7.15.1-linux-x86_64/bin$ nohup /home/elk/elk/kibana-7.15.1-linux-x86_64/bin/kibana >> /home/elk/elk/kibana-7.15.1-linux-x86_64/output.log 2>&1 &[2] 9378elk@elk:~/elk/kibana-7.15.1-linux-x86_64/bin$

将日志信息输入到屏幕上

elk@elk:~$ cd elk/logstash-7.15.1/bin/elk@elk:~/elk/logstash-7.15.1/bin$ ./logstash -e 'input {stdin{}} output{stdout{}}'输出个123而后回车,会把后果输入到屏幕上{          "host" => "elk",    "@timestamp" => 2021-10-20T07:15:54.230Z,      "@version" => "1",       "message" => ""}123{          "host" => "elk",    "@timestamp" => 2021-10-20T07:15:56.453Z,      "@version" => "1",       "message" => "123"}elk@elk:~/elk/logstash-7.15.1/bin$ cd ../config/elk@elk:~/elk/logstash-7.15.1/config$ vim logstashelk@elk:~/elk/logstash-7.15.1/config$ cat logstashinput {    # 从文件读取日志信息      file {          path => "/var/log/messages"          type => "system"          start_position => "beginning"           }}filter {}output {      # 规范输入      stdout {}}elk@elk:~/elk/logstash-7.15.1/config$ mv logstash logstash.confelk@elk:~/elk/logstash-7.15.1/config$

启动测试

elk@elk:~/elk/logstash-7.15.1/config$ cd ../bin/elk@elk:~/elk/logstash-7.15.1/bin$ ./logstash -f ../config/logstash.conf

后盾启动

elk@elk:~$ nohup /home/elk/elk/logstash-7.15.1/bin/logstash -f /home/elk/elk/logstash-7.15.1/config/logstash.conf >> /home/elk/elk/logstash-7.15.1/output.log 2>&1 &[3] 10177elk@elk:~$

设置开机自启

elk@elk:~$ vim startup.shelk@elk:~$elk@elk:~$ cat startup.sh#!/bin/bashnohup /home/elk/elk/elasticsearch-7.15.1/bin/elasticsearch >> /home/elk/elk/elasticsearch-7.15.1/output.log 2>&1 &nohup /home/elk/elk/kibana-7.15.1-linux-x86_64/bin/kibana >> /home/elk/elk/kibana-7.15.1-linux-x86_64/output.log 2>&1 &nohup /home/elk/elk/logstash-7.15.1/bin/logstash -f /home/elk/elk/logstash-7.15.1/config/logstash.conf >> /home/elk/elk/logstash-7.15.1/output.log 2>&1 &elk@elk:~$elk@elk:~$ crontab -eno crontab for elk - using an empty oneSelect an editor.  To change later, run 'select-editor'.  1. /bin/nano        <---- easiest  2. /usr/bin/vim.basic  3. /usr/bin/vim.tiny  4. /bin/edChoose 1-4 [1]: 2crontab: installing new crontabelk@elk:~$elk@elk:~$elk@elk:~$ crontab -l@reboot /home/elk/startup.shelk@elk:~$

logstash插件

logstash是通过插件对其性能进行增强

插件分类:

inputs 输出

codecs 解码

filters 过滤

outputs 输入

在Gemfile文件里记录了logstash的插件

elk@elk:~$ cd elk/logstash-7.15.1elk@elk:~/elk/logstash-7.15.1$ ls GemfileGemfileelk@elk:~/elk/logstash-7.15.1$

去其github上下载插件,地址为:https://github.com/logstash-p...

应用filter插件logstash-filter-mutate

elk@elk:~/elk/logstash-7.15.1/config$  vim logstash2.conf#创立一个新的配置文件用来过滤input {    stdin {    }}filter {   mutate {        split => ["message", "|"]    }}output {    stdout {    }}

当输出sss|sssni|akok223|23即会依照|分隔符进行分隔

其数据处理流程:input–>解码–>filter–>解码–>output

启动服务

而后去启动logstash服务

elk@elk:~$ nohup /home/elk/elk/logstash-7.15.1/bin/logstash -f /home/elk/elk/logstash-7.15.1/config/logstash2.conf >> /home/elk/elk/logstash-7.15.1/output.log 2>&1 &

Linux运维交换社区

Linux运维交换社区,互联网新闻以及技术交换。

41篇原创内容

公众号

本文应用 文章同步助手 同步