分布式日志

1 日志管理

1.1 日志管理方案

服务器数量较少时

直接登录到目标服务器捞日志查看 → 通过 rsyslog 或shell/python 等脚本实现日志搜集并集中保存到统一的日志服务器

服务器数量较多时

ELK 大型的日志系统,实现日志收集、日志存储、日志检索和分析

容器环境

EFK Loki+Granfana

1.2 使用ELK的原因

日志主要包括系统日志、应用程序日志和安全日志。系统运维和开发人员可以通过日志了解服务器软硬件信息、检查配置过程中的错误及错误发生的原因。经常分析日志可以了解服务器的负荷,性能安全性,从而及时采取措施纠正错误。 往往单台机器的日志我们使用grep、awk等工具就能基本实现简单分析,但是当日志被分散的储存不同的设备上。如果你管理数十上百台服务器,你还在使用依次登录每台机器的传统方法查阅日志。这样是不是感觉很繁琐和效率低下。当务之急我们使用集中化的日志管理,例如:开源的syslog,将所有服务器上的日志收集汇总。集中化管理日志后,日志的统计和检索又成为一件比较麻烦的事情,一般我们使用 grep、awk和wc等Linux命令能实现检索和统计,但是对于要求更高的查询、排序和统计等要求和庞大的机器数量依然使用这样的方法难免有点力不从心。 一般大型系统是一个分布式部署的架构,不同的服务模块部署在不同的服务器上,问题出现时,大部分情况需要根据问题暴露的关键信息,定位到具体的服务器和服务模块,构建一套集中式日志系统,可以提高定位问题的效率。

1.3 完整日志系统的基本特征

收集:能够采集多种来源的日志数据 传输:能够稳定的把日志数据解析过滤并传输到存储系统 存储:存储日志数据 分析:支持 UI 分析 警告:能够提供错误报告,监控机制

1.4 ELK概述

ELK平台是一套完整的日志集中处理解决方案,将 ElasticSearch、Logstash 和 Kiabana 三个开源工具配合使用, 完成更强大的用户对日志的查询、排序、统计需求。

●ElasticSearch

对日志数据进行分片、存储日志数据,并创建索引,方便全文检索

是基于Lucene(一个全文检索引擎的架构)开发的分布式存储检索引擎,用来存储各类日志。 Elasticsearch 是用 Java 开发的,可通过 RESTful Web 接口,让用户可以通过浏览器与 Elasticsearch 通信。 Elasticsearch是一个实时的、分布式的可扩展的搜索引擎,允许进行全文、结构化搜索,它通常用于索引和搜索大容量的日志数据,也可用于搜索许多不同类型的文档。

●Kiabana:Kibana

接入ElasticSearch的数据源,将日志数据进行图形化展示,方便用户通过浏览器查看统计分析日志

通常与 Elasticsearch 一起部署,Kibana 是 Elasticsearch 的一个功能强大的数据可视化 Dashboard,Kibana 提供图形化的 web 界面来浏览 Elasticsearch 日志数据,可以用来汇总、分析和搜索重要数据。

●Logstash

采集日志数据,通过插件模块对数据进行过滤、格式化处理,再输出

作为数据收集引擎。它支持动态的从各种数据源搜集数据,并对数据进行过滤、分析、丰富、统一格式等操作,然后存储到用户指定的位置,一般会发送给 Elasticsearch。 Logstash 由 Ruby 语言编写,运行在 Java 虚拟机(JVM)上,是一款强大的数据处理工具, 可以实现数据传输、格式处理、格式化输出。Logstash 具有强大的插件功能,常用于日志处理。

可以添加的其它组件:
●Filebeat

替代Logstash采集日志数据

轻量级的开源日志文件数据搜集器。通常在需要采集数据的客户端安装 Filebeat,并指定目录与日志格式,Filebeat 就能快速收集数据,并发送给 logstash 进行解析,或是直接发给 Elasticsearch 存储,性能上相比运行于 JVM 上的 logstash 优势明显,是对它的替代。常应用于 EFLK 架构当中。

#filebeat 结合 logstash 带来好处: 1)通过 Logstash 具有基于磁盘的自适应缓冲系统,该系统将吸收传入的吞吐量,从而减轻 Elasticsearch 持续写入数据的压力 2)从其他数据源(例如数据库,S3对象存储或消息传递队列)中提取 3)将数据发送到多个目的地,例如S3,HDFS(Hadoop分布式文件系统)或写入文件 4)使用条件数据流逻辑组成更复杂的处理管道

●缓存/消息队列(redis、kafka、RabbitMQ等)

作为MQ消息队列实现流量削峰、缓冲等功能

可以对高并发日志数据进行流量削峰和缓冲,这样的缓冲可以一定程度的保护数据不丢失,还可以对整个架构进行应用解耦。

●Fluentd

替代Logstash采集日志数据和过滤转换功能,常用语k8s容器环境

是一个流行的开源数据收集器。由于 logstash 太重量级的缺点,Logstash 性能低、资源消耗比较多等问题,随后就有 Fluentd 的出现。相比较 logstash,Fluentd 更易用、资源消耗更少、性能更高,在数据处理上更高效可靠,受到企业欢迎,成为 logstash 的一种替代方案,常应用于 EFK 架构当中。在 Kubernetes 集群中也常使用 EFK 作为日志数据收集的方案。 在 Kubernetes 集群中一般是通过 DaemonSet 来运行 Fluentd,以便它在每个 Kubernetes 工作节点上都可以运行一个 Pod。 它通过获取容器日志文件、过滤和转换日志数据,然后将数据传递到 Elasticsearch 集群,在该集群中对其进行索引和存储。

1.5 ELK的工作原理

(1)在所有需要收集日志的服务器上部署Logstash;或者先将日志进行集中化管理在日志服务器上,在日志服务器上部署 Logstash。 (2)Logstash 收集日志,将日志格式化并输出到 Elasticsearch 群集中。 (3)Elasticsearch 对格式化后的数据进行索引和存储。 (4)Kibana 从 ES 群集中查询数据生成图表,并进行前端数据的展示。

总结:logstash作为日志搜集器,从数据源采集数据,并对数据进行过滤,格式化处理,然后交由Elasticsearch存储,kibana对日志进行可视化处理。

2 ELK实际部署

ElasticSearch:

集群化部署,节点分为三种类型:

Master/data/client,设置相同的cluster.name并使用Zen D通过单播实现集群中节点的服务发现

创建索引

curl -X PUT 'IP地址:9200'/<索引名>/<类型>/<文档>?pretty&pretty -H 'content-Type: application/json' -d {"键名1":"键值","键名2":"键值}'

删除索引

curl -X DELETE IP地址:9200/<索引名>

查看索引配置

curl -X GET IP地址:9200/<索引名>/_settings

修改索引配置

curl -X GET IP地址:9200/<索引名>/_settings \

-H 'content-Type: application/json' -d '{"键名":"键值"}'

创建索引别名

curl -X POST IP地址:9200/ aliases \ -H 'content-Type:application/json' -d '{"actions":[{"add":{index":"索引名","akisa":"索引别名"}}]}'

删除索引别名

curl -X POST IP地址:9200/ aliases \ -H 'content-Type:application/json' -d '{"actions":[{"remove":{index":"索引名","akisa":"索引别名"}}]}'

vim /etc/sysconfig/selinux

2.1 ELK日志分析系统

实验部署环境准备,关闭防火墙和安全终端
虚拟机名IP需要安装的应用
node01节点(2C/4G)192.168.88.10Elasticsearch
node02节点(2C/4G)192.168.88.205Elasticsearch
node03节点(2C/4G)192.168.88.202Elasticsearch
nginx节点192.168.88.201Logstash Kibana nginx
更改主机名以及检查 java环境;如果没有就 yum安装 java —— yum install -y java
#node01:192.168.88.10
[root@localhost ~]# hostnamectl set-hostname node01
[root@localhost ~]# su
[root@node01 ~]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
​
#node02:192.168.88.205
[root@localhost ~]# hostnamectl set-hostname node02
[root@localhost ~]# su
[root@node02 ~]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
​
#node03:192.168.88.202
[root@localhost ~]# hostnamectl set-hostname node03
[root@localhost ~]# su
[root@node03 ~]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
​
#nginx:192.168.88.201
[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# su
[root@nginx ~]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
​
在 node01、node02、node03上部署 Elasticsearch 软件,相同操作
#在 opt目录下传输 elasticsearch-6.7.2.rpm文件
[root@node01 ~]# cd /opt
[root@node01 opt]# rz -E
rz waiting to receive.
[root@node01 opt]# ls
elasticsearch-6.7.2.rpm  rh
​
#rpm安装
[root@node01 opt]# rpm -ivh elasticsearch-6.7.2.rpm 
警告:elasticsearch-6.7.2.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
准备中...                          ################################# [100%]
Creating elasticsearch group... OK
Creating elasticsearch user... OK
正在升级/安装...
   1:elasticsearch-0:6.7.2-1          ################################# [100%]
### 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
Created elasticsearch keystore in /etc/elasticsearch
​
#复制备份
[root@node01 opt]# cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
​
#进入配置文件,进行修改
[root@node01 opt]# vim /etc/elasticsearch/elasticsearch.yml

#过滤没 #符号的段落,检查修改的配置
[root@node01 opt]# grep -v "^#" /etc/elasticsearch/elasticsearch.yml
cluster.name: mhy-elk-cluster
node.name: node01 #另外两台为 node02,node03
node.master: true
node.data: true
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.zen.ping.unicast.hosts: ["192.168.88.10:9300", "192.168.88.205:9300", "192.168.88.202:9300"]
​
#进入配置文件进行添加配置
[root@node01 opt]# vim /etc/security/limits.conf 
*                soft    nofile          65536
*                hard    nofile          65536
*                soft    nproc           32000
*                hard    nproc           32000
*                soft    memlock         unlimited
*                hard    memlock         unlimited

#进入配置文件,修改配置
[root@node01 opt]# vim /etc/systemd/system.conf 

#进入配置文件,添加配置
[root@node01 opt]# vim /etc/sysctl.conf 
vm.max_map_count=262144

#检查配置
[root@node01 opt]# sysctl -p
vm.max_map_count = 262144
[root@node01 opt]# sysctl -a | grep vm.max_map_count
sysctl: reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.ens32.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
sysctl: reading key "net.ipv6.conf.virbr0.stable_secret"
sysctl: reading key "net.ipv6.conf.virbr0-nic.stable_secret"
vm.max_map_count = 262144
完成上述操作后重启 node01、node02、node03节点服务器,关闭防火墙和安全终端并开启服务
#重启
[root@node01 opt]# reboot
​
#重启之后
[root@node01 ~]# systemctl stop firewalld.service 
[root@node01 ~]# setenforce 0
​
#开启服务
[root@node01 ~]# systemctl start elasticsearch.service 
​
#设置开机
[root@node01 ~]# systemctl enable elasticsearch.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
​
#查看服务
[root@node01 ~]# netstat -natp|grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      2665/java  
浏览器访问测试
浏览器访问  http://IP:9200查看节点 node01、node02、node03 的信息
​
浏览器访问 http://IP:9200/_cluster/health?pretty 查看群集的健康情况,可以看到 status 值为 green(绿色), 表示节点健康运行
​
浏览器访问 http://IP:9200/_cluster/state?pretty  检查群集状态信息

挑选一台节点服务器进行后续操作,这里选择 node01节点服务器
在 node01上安装 node
#在 opt目录下传输文件 node-v8.2.1.tar.gz
[root@node01 ~]# cd /opt
[root@node01 opt]# rz -E
rz waiting to receive.
[root@node01 opt]# ls
elasticsearch-6.7.2.rpm  node-v8.2.1.tar.gz  rh
​
#yum安装编译环境
[root@node01 opt]# yum install gcc gcc-c++ make -y
​
#解压压缩包
[root@node01 opt]# tar xf node-v8.2.1.tar.gz 
​
#cd到指定目录下,进行编译安装
[root@node01 opt]# cd node-v8.2.1/
[root@node01 node-v8.2.1]# ./configure
[root@node01 node-v8.2.1]# make -j 4 && make install
在 node01上安装 phantomjs
#cd到 opt目录下传输文件 phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@node01 node-v8.2.1]# cd /opt/
[root@node01 opt]# rz -E
rz waiting to receive.
[root@node01 opt]# ls
elasticsearch-6.7.2.rpm  phantomjs-2.1.1-linux-x86_64.tar.bz2
node-v8.2.1              rh
node-v8.2.1.tar.gz
​
#解压安装包
[root@node01 opt]# tar xf phantomjs-2.1.1-linux-x86_64.tar.bz2 
​
#cd到指定目录下将 phantomjs文件复制到指定位置
[root@node01 opt]# cd phantomjs-2.1.1-linux-x86_64/bin/
[root@node01 bin]# ls
phantomjs
[root@node01 bin]# cp phantomjs /usr/local/bin/
​
在 node01上安装 Elasticsearch-head 数据可视化工具,并启动
#cd到 opt目录下传输 elasticsearch-head-master.zip
[root@node01 bin]# cd /opt/
[root@node01 opt]# rz -E
rz waiting to receive.
[root@node01 opt]# ls
elasticsearch-6.7.2.rpm
elasticsearch-head-master.zip
node-v8.2.1
node-v8.2.1.tar.gz
phantomjs-2.1.1-linux-x86_64
phantomjs-2.1.1-linux-x86_64.tar.bz2
rh
​
#解压压缩包
[root@node01 opt]# unzip elasticsearch-head-master.zip 
​
#cd到指定目录下,进行安装
[root@node01 opt]# cd elasticsearch-head-master/
[root@node01 elasticsearch-head-master]# npm install

最后是以下结果为成功,第一次大概率失败,再来一次大概率成功

#进入配置文件,添加配置
[root@node01 elasticsearch-head-master]# vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"

#重启服务
[root@node01 elasticsearch-head-master]# systemctl restart elasticsearch.service 
​
#后台启动服务,最后 Ctrl+c即可
[root@node01 elasticsearch-head-master]# npm run start &
[1] 48865
[root@node01 elasticsearch-head-master]# 
> elasticsearch-head@0.0.0 start /opt/elasticsearch-head-master
> grunt server
​
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
^C
​
#查看服务端口
[root@node01 elasticsearch-head-master]# netstat -napt|grep 9100
tcp        0      0 0.0.0.0:9100            0.0.0.0:*               LISTEN      48875/grunt 
浏览器访问 http://192.168.88.10:9100验证

插入索引
#创建一个索引,user定义名字,mesg定义内容
[root@node01 elasticsearch-head-master]# curl -X PUT 'localhost:9200/index-demo/test/1?pretty&pretty' -H 'content-Type: application/json' -d '{"user":"superman","mesg":"welcome to yuanshen"}'
{
  "_index" : "index-demo",
  "_type" : "test",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
浏览器访问 http://192.168.88.10:9100验证

在 nginx节点服务器进行部署 nginx操作
#进入 yum源目录下添加 nginx.repo
[root@nginx ~]# cd /etc/yum.repos.d/
[root@nginx yum.repos.d]# rz -E
rz waiting to receive.
[root@nginx yum.repos.d]# ls
CentOS-Base.repo       CentOS-fasttrack.repo  CentOS-Vault.repo
CentOS-CR.repo         CentOS-Media.repo      nginx.repo
CentOS-Debuginfo.repo  CentOS-Sources.repo
​
#设置 nginx服务开机自启
[root@nginx yum.repos.d]# systemctl enable --now nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
浏览器访问 http://192.168.88.201验证

检查 nginx服务的正确和错误的文件
#cd到指定目录下
[root@nginx yum.repos.d]# cd /var/log/nginx/
[root@nginx nginx]# ls
access.log  error.log
Logstash 命令常用选项
选项作用
-f通过这个选项可以指定 Logstash 的配置文件,根据配置文件配置 Logstash 的输入和输出流
-e从命令行中获取,输入、输出后面跟着字符串,该字符串可以被当作 Logstash 的配置(如果是空,则默认使用 stdin 作为输入,stdout 作为输出)
-t测试配置文件是否正确,然后退出
-w指定 filter线程数量,默认线程数是 5
-l指定日志文件名称
在 nginx节点服务器上安装 logstash
#cd到 opt目录下传输 logstash-6.7.2.rpm安装包
[root@nginx nginx]# cd /opt/
[root@nginx opt]# rz -E
rz waiting to receive.
[root@nginx opt]# ls
logstash-6.7.2.rpm  rh
​
#安装
[root@nginx opt]# rpm -ivh logstash-6.7.2.rpm 
警告:logstash-6.7.2.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:logstash-1:6.7.2-1               ################################# [100%]
Using provided startup.options file: /etc/logstash/startup.options
/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/pleaserun-0.0.30/lib/pleaserun/platform/base.rb:112: warning: constant ::Fixnum is deprecated
Successfully created system startup script for Logstash
​
#检查 logstash文件
[root@nginx opt]# cd /usr/share/logstash/bin/
[root@nginx bin]# ls
benchmark.sh         logstash-keystore      pqrepair
cpdump               logstash-keystore.bat  ruby
dependencies-report  logstash.lib.sh        setup.bat
ingest-convert.sh    logstash-plugin        system-install
logstash             logstash-plugin.bat
logstash.bat         pqcheck
​
#优化路径
[root@nginx bin]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/
​
#设置
[root@nginx bin]# systemctl enable logstash.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/logstash.service to /etc/systemd/system/logstash.service.
定义输入和输出流,Ctrl+c退出
#输入采用标准输入,输出采用标准输出(类似管道),新版本默认使用 rubydebug 格式输出
[root@nginx bin]# logstash -e 'input { stdin{} } output { stdout{} }'
.......... #显示类似以下内容成功
[INFO ] 2024-01-20 18:39:38.757 [Ruby-0-Thread-1: /usr/share/logstash/lib/bootstrap/environment.rb:6] agent - Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[INFO ] 2024-01-20 18:39:38.885 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600}
​
#直接自定义输入内容
mhy
/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/awesome_print-1.7.0/lib/awesome_print/formatters/base_formatter.rb:31: warning: constant ::Fixnum is deprecated
#显示以下内容成功
{
       "message" => "mhy",
      "@version" => "1",
    "@timestamp" => 2024-01-20T10:40:15.112Z,
          "host" => "nginx"
}
^C[WARN ] 2024-01-20 18:40:32.725 [SIGINT handler] runner - SIGINT received. Shutting down.
[INFO ] 2024-01-20 18:40:32.951 [[main]-pipeline-manager] pipeline - Pipeline has terminated {:pipeline_id=>"main", :thread=>"#<Thread:0x45300334 run>"}
[INFO ] 2024-01-20 18:40:32.969 [LogStash::Runner] runner - Logstash shut down.
​
#使用 rubydebug 输出详细格式显示,codec 为一种编解码器
[root@nginx bin]# logstash -e 'input { stdin{} } output { stdout{ codec=>rubydebug } }'
.......... #显示类似以下内容成功
[INFO ] 2024-01-20 18:42:32.798 [Ruby-0-Thread-1: /usr/share/logstash/lib/bootstrap/environment.rb:6] agent - Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[INFO ] 2024-01-20 18:42:32.923 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600}
​
#直接自定义输入内容
banana
/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/awesome_print-1.7.0/lib/awesome_print/formatters/base_formatter.rb:31: warning: constant ::Fixnum is deprecated
#显示以下内容成功
{
       "message" => "banana",
    "@timestamp" => 2024-01-20T10:42:45.156Z,
      "@version" => "1",
          "host" => "nginx"
}
^C[WARN ] 2024-01-20 18:42:50.636 [SIGINT handler] runner - SIGINT received. Shutting down.
[INFO ] 2024-01-20 18:42:50.835 [[main]-pipeline-manager] pipeline - Pipeline has terminated {:pipeline_id=>"main", :thread=>"#<Thread:0x545c933b run>"}
[INFO ] 2024-01-20 18:42:50.855 [LogStash::Runner] runner - Logstash shut down.
​
#使用 Logstash 将信息写入 Elasticsearch 中
[root@nginx bin]# logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["192.168.88.10:9200"] } }'
.......... #显示类似以下内容成功
[INFO ] 2024-01-20 18:46:42.523 [Converge PipelineAction::Create<main>] pipeline - Pipeline started successfully {:pipeline_id=>"main", :thread=>"#<Thread:0x3f11050f run>"}
The stdin plugin is now waiting for input:
[INFO ] 2024-01-20 18:46:42.565 [Ruby-0-Thread-1: /usr/share/logstash/lib/bootstrap/environment.rb:6] agent - Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[INFO ] 2024-01-20 18:46:42.697 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600}
​
#直接自定义输入内容,并前往浏览器进行访问验证
hyh is shen xu
wx is da shuai be  
^C[WARN ] 2024-01-20 18:47:23.055 [SIGINT handler] runner - SIGINT received. Shutting down.
[INFO ] 2024-01-20 18:47:23.622 [[main]-pipeline-manager] pipeline - Pipeline has terminated {:pipeline_id=>"main", :thread=>"#<Thread:0x3f11050f run>"}
[INFO ] 2024-01-20 18:47:23.624 [LogStash::Runner] runner - Logstash shut down.
浏览器访问 http://192.168.88.10:9100验证

定义 logstash配置文件

Logstash 配置文件基本由三部分组成:input、output 以及 filter(可选,根据需要选择使用)。

—— input:表示从数据源采集数据,常见的数据源如Kafka、日志文件等;file beats kafka redis stdin

—— filter:表示数据处理层,包括对数据进行格式化处理、数据类型转换、数据过滤等,支持正则表达式

  • grok 对若干个大文本字段进行再分割成一些小字段 (?<字段名>正则表达式) 字段名: 正则表达式匹配到的内容

  • date 对数据中的时间格式进行统一和格式化

  • mutate 对一些无用的字段进行剔除,或增加字段

  • mutiline 对多行数据进行统一编排,多行合并或拆分

—— output:表示将Logstash收集的数据经由过滤器处理之后输出到Elasticsearch;elasticsearch stdout

#cd到指定目录,赋予日志文件权限
[root@nginx bin]# cd /var/log/
[root@nginx log]# chmod a+r messages
[root@nginx log]# ll
..........
-rw-r--r--. 1 root     root     1909 1月  20 18:50 messages
..........
#cd到指定目录
[root@nginx log]# cd /etc/logstash/
​
#创建 sincedb_path目录
[root@nginx logstash]# mkdir sincedb_path
[root@nginx logstash]# ls
conf.d       log4j2.properties     logstash.yml   sincedb_path
jvm.options  logstash-sample.conf  pipelines.yml  startup.options
​
#cd到 sincedb_path目录下创建一个 log_progress文件
[root@nginx logstash]# cd sincedb_path/
[root@nginx sincedb_path]# touch log_progress
​
#更改 log_progress的属组和属主;检查
[root@nginx sincedb_path]# chown logstash. log_progress 
[root@nginx sincedb_path]# ll
总用量 0
-rw-r--r--. 1 logstash logstash 0 1月  20 19:00 log_progress
​
#进入 conf.d/目录下
[root@nginx sincedb_path]# cd ..
[root@nginx logstash]# cd conf.d/
[root@nginx conf.d]# pwd
/etc/logstash/conf.d
​
#创建一个 syslog.conf脚本文件
[root@nginx conf.d]# vim syslog.conf
input {
    file {
       path => "/var/log/messages"
       type => "syslog"
       start_position => "beginning"
       sincedb_path => "/etc/logstash/sincedb_path/log_progress"
       add_field => { "log_hostname" => "${HOSTNAME}" }
    }
}
​
#filter {}
​
output {
   elasticsearch {
       hosts => ["192.168.88.202:9200", "192.168.88.205:9200" , "192.168.88.10:9200"]
       index => "syslog-%{+yyyy.MM.dd}"
   }
}

#检查配置,显示 ok为正确
[root@nginx conf.d]# logstash -t -f syslog.conf 
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
[WARN ] 2024-01-20 19:13:03.249 [LogStash::Runner] multilocal - Ignoring the 'pipelines.yml' file because modules or command line options are specified
Configuration OK #ok说明配置无误
[INFO ] 2024-01-20 19:13:06.277 [LogStash::Runner] runner - Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash
​
#启动服务
[root@nginx conf.d]# logstash -f syslog.conf 
.......... #显示类似以下内容成功
[INFO ] 2024-01-20 19:18:22.785 [Converge PipelineAction::Create<main>] pipeline - Pipeline started successfully {:pipeline_id=>"main", :thread=>"#<Thread:0x7fabeac3 sleep>"}
[INFO ] 2024-01-20 19:18:22.800 [[main]<file] observingtail - START, creating Discoverer, Watch with file and sincedb collections
[INFO ] 2024-01-20 19:18:22.820 [Ruby-0-Thread-1: /usr/share/logstash/lib/bootstrap/environment.rb:6] agent - Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[INFO ] 2024-01-20 19:18:22.995 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600}
​
#直接自定义输入内容,并前往浏览器进行访问验证
jhc is fei wu
^C[WARN ] 2024-01-20 19:18:49.489 [SIGINT handler] runner - SIGINT received. Shutting down.
[INFO ] 2024-01-20 19:18:49.554 [Converge PipelineAction::Stop<main>] observingtail - QUIT - closing all files and shutting down.
[INFO ] 2024-01-20 19:18:50.668 [[main]-pipeline-manager] pipeline - Pipeline has terminated {:pipeline_id=>"main", :thread=>"#<Thread:0x7fabeac3 run>"}
[INFO ] 2024-01-20 19:18:50.670 [LogStash::Runner] runner - Logstash shut down.
浏览器访问 http://192.168.88.10:9100验证

在 node01上部署 kibanna
#cd到 opt下传输 kibana-6.7.2-x86_64.rpm
[root@node01 elasticsearch-head-master]# cd /opt/
[root@node01 opt]# rz -E
rz waiting to receive.
[root@node01 opt]# ls
elasticsearch-6.7.2.rpm
elasticsearch-head-master
elasticsearch-head-master.zip
kibana-6.7.2-x86_64.rpm
node-v8.2.1
node-v8.2.1.tar.gz
phantomjs-2.1.1-linux-x86_64
phantomjs-2.1.1-linux-x86_64.tar.bz2
rh
​
#rpm安装
[root@node01 opt]# rpm -ivh kibana-6.7.2-x86_64.rpm 
警告:kibana-6.7.2-x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:kibana-6.7.2-1                   ################################# [100%]
​
#进入配置文件,末行添加配置
[root@node01 opt]# vim /etc/kibana/kibana.yml 

#检查修改的配置文件
[root@node01 opt]# grep -v "^#" /etc/kibana/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://192.168.88.10:9200", "http://192.168.88.205:9200", "http://192.168.88.202:9200"]
kibana.index: ".kibana"
​
logging.dest: /var/log/kibana.log
​
#在指定目录下创建 kibana.log文件
[root@node01 opt]# touch /var/log/kibana.log
​
#更改 kibana.log其属主和属组
[root@node01 opt]# chown kibana. /var/log/kibana.log 
​
#开启 kibana.service服务
[root@node01 opt]# systemctl start kibana.service 
​
#设置开机自启
[root@node01 opt]# systemctl enable --now kibana.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/kibana.service to /etc/systemd/system/kibana.service.
​
#查看服务端口
[root@node01 opt]# netstat -natp|grep 5601
tcp        0      0 0.0.0.0:5601            0.0.0.0:*               LISTEN      88066/node  
​
#进入配置文件,将语言改成中文
[root@node01 opt]# vim /etc/kibana/kibana.yml 

#重启 kibana.service服务
[root@node01 opt]# systemctl restart kibana.service 
浏览器访问 http://192.168.88.10:5601测试

浏览器访问 http://192.168.88.10:9100查看

在 nginx节点服务器上将 nginx服务器的日志(访问的、错误的)添加到 Elasticsearch 并通过 Kibana 显示
[root@nginx conf.d]# pwd
/etc/logstash/conf.d
[root@nginx conf.d]# vim nginx_log.conf
input {
    file{
        path => "/var/log/nginx/access.log"
        type => "access"
        start_position => "beginning"
    }
    file{
        path => "/var/log/nginx/error.log"
        type => "error"
        start_position => "beginning"
    }
}
output {
    if [type] == "access" {
        elasticsearch {
            hosts => ["192.168.88.10:9200","192.168.88.205:9200","192.168.88.202:9200"]
            index => "nginx_access-%{+YYYY.MM.dd}"
        }
    }
    if [type] == "error" {
        elasticsearch {
            hosts => ["192.168.88.10:9200","192.168.88.205:9200","192.168.88.202:9200"]
            index => "nginx_error-%{+YYYY.MM.dd}"
        }
    }
}

[root@nginx conf.d]# /usr/share/logstash/bin/logstash -f nginx_log.conf 
.......... #显示类似以下内容成功
[INFO ] 2024-01-20 20:24:34.108 [[main]-pipeline-manager] file - No sincedb_path set, generating one based on the "path" setting {:sincedb_path=>"/usr/share/logstash/data/plugins/inputs/file/.sincedb_d883144359d3b4f516b37dba51fab2a2", :path=>["/var/log/nginx/access.log"]}
[INFO ] 2024-01-20 20:24:34.126 [[main]-pipeline-manager] file - No sincedb_path set, generating one based on the "path" setting {:sincedb_path=>"/usr/share/logstash/data/plugins/inputs/file/.sincedb_c1bc5cb2bf1f2e3ee39c0f9e9bfcaedc", :path=>["/var/log/nginx/error.log"]}
[INFO ] 2024-01-20 20:24:34.139 [Converge PipelineAction::Create<main>] pipeline - Pipeline started successfully {:pipeline_id=>"main", :thread=>"#<Thread:0x16d83b3 run>"}
[INFO ] 2024-01-20 20:24:34.173 [[main]<file] observingtail - START, creating Discoverer, Watch with file and sincedb collections
[INFO ] 2024-01-20 20:24:34.175 [[main]<file] observingtail - START, creating Discoverer, Watch with file and sincedb collections
[INFO ] 2024-01-20 20:24:34.197 [Ruby-0-Thread-1: /usr/share/logstash/lib/bootstrap/environment.rb:6] agent - Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[INFO ] 2024-01-20 20:24:34.474 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600}
​
#Ctrl+c退出
^C[WARN ] 2024-01-20 20:26:30.552 [SIGINT handler] runner - SIGINT received. Shutting down.
[INFO ] 2024-01-20 20:26:30.621 [Converge PipelineAction::Stop<main>] observingtail - QUIT - closing all files and shutting down.
[INFO ] 2024-01-20 20:26:30.624 [Converge PipelineAction::Stop<main>] observingtail - QUIT - closing all files and shutting down.
[INFO ] 2024-01-20 20:26:32.551 [[main]-pipeline-manager] pipeline - Pipeline has terminated {:pipeline_id=>"main", :thread=>"#<Thread:0x16d83b3 run>"}
[INFO ] 2024-01-20 20:26:32.554 [LogStash::Runner] runner - Logstash shut down.
在 nginx节点服务器上将 nginx服务关闭,然后浏览器访问 http://192.168.88.201显示访问失败获得错误日志,再将 nginx服务器开启,然后再去浏览器上访问 http://192.168.88.201显示 nginx界面获得成功日志,最后再去浏览器访问 http://192.168.88.10:9100验证结果
[root@nginx conf.d]# systemctl stop nginx.service 
浏览器访问 http://192.168.88.201

[root@nginx conf.d]# systemctl start nginx
浏览器访问 http://192.168.88.201

浏览器访问 http://192.168.88.10:9100验证结果

浏览器访问 http://192.168.88.10:5601

Filebeat+ELK 部署
安装 Filebeat
上传软件包 filebeat-6.7.2-linux-x86_64.tar.gz 到/opt目录
tar zxvf filebeat-6.7.2-linux-x86_64.tar.gz
mv filebeat-6.7.2-linux-x86_64/ /usr/local/filebeat
​
​
设置 filebeat 的主配置文件
cd /usr/local/filebeat
​
vim filebeat.yml
filebeat.inputs:
- type: log         #指定 log 类型,从日志文件中读取消息
  enabled: true
  paths:
    - /var/log/messages       #指定监控的日志文件
    - /var/log/*.log
  tags: ["sys"]     #设置索引标签
  fields:           #可以使用 fields 配置选项设置一些参数字段添加到 output 中
    service_name: filebeat
    log_type: syslog
    from: 192.168.80.13
​
--------------Elasticsearch output-------------------
(全部注释掉)
​
----------------Logstash output---------------------
output.logstash:
  hosts: ["192.168.80.12:5044"]      #指定 logstash 的 IP 和端口

启动 filebeat
nohup ./filebeat -e -c filebeat.yml > filebeat.out &
#-e:输出到标准输出,禁用syslog/文件输出
#-c:指定配置文件
#nohup:在系统后台不挂断地运行命令,退出终端不会影响程序的运行

在 Logstash 组件所在节点上新建一个 Logstash 配置文件
cd /etc/logstash/conf.d
​
vim filebeat.conf
input {
    beats {
        port => "5044"
    }
}
​
#filebeat发送给logstash的日志内容会放到message字段里面,logstash使用grok插件正则匹配message字段内容进行字段分割
#Kibana自带grok的正则匹配的工具:http://<your kibana IP>:5601/app/kibana#/dev_tools/grokdebugger
# %{IPV6}|%{IPV4} 为 logstash 自带的 IP 常量
filter {
  grok {
    match => ["message", "(?<remote_addr>%{IPV6}|%{IPV4})[\s\-]+\[(?<logTime>.*)\]\s+\"(?<method>\S+)\s+(?<url_path>.+)\"\s+(?<rev_code>\d+) \d+ \"(?<req_addr>.+)\" \"(?<content>.*)\""]
  }
}
​
output {
    elasticsearch {
        hosts => ["192.168.80.10:9200","192.168.80.11:9200"]
        index => "%{[fields][service_name]}-%{+YYYY.MM.dd}"
    }
    stdout {
        codec => rubydebug
    }
}
​
#启动 logstash
logstash -f filebeat.conf

浏览器访问 http://192.168.80.10:5601 登录 Kibana,单击“Create Index Pattern”按钮添加索引“filebeat-*”,单击 “create” 按钮创建,单击 “Discover” 按钮可查看图表信息及日志信息。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/342435.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Redis学习——入门篇①

Redis学习——入门篇① 1、2&#xff1a;Redis入门概述3&#xff1a;Redis安装配置10 安装Redis-cli命令 4&#xff1a;Redis——十大数据类型11 redis数据类型12 命令查阅13 key常用命令介绍14 类型大小写和帮助命令15 String 命令&#xff08;上&#xff09;16 String 命令&a…

peer eslint-plugin-vue@“^7.0.0“ from @vue/eslint-config-standard@6.1.0

问题&#xff1a; 用vue/cli脚手架安装项目时&#xff0c;选择ESlint&#xff0c;再安装依赖包的时候&#xff0c;会报以下错误&#xff0c; 原因&#xff1a; npmV7 之前的版本遇到依赖冲突时&#xff0c;会忽视冲突&#xff0c;继续安装&#xff1b; npmV7版本开始不再自动忽…

安裝火狐和穀歌流覽器插件FoxyProxy管理海外動態IP代理

代理生態系統擁有大量有用的實用程式&#xff0c;使海外代理IP代理設置的使用變得簡單起來。其中一種類型叫做代理管理工具&#xff0c;像FoxyProxy就是該工具集比較受歡迎的。 本文將全面解析FoxyProxy擴展的功能和特性、Foxyproxy怎麼下載、以及如何在穀歌流覽器和火狐流覽器…

9款最新文生图模型汇总!含华为、谷歌、Stability AI等大厂创新模型(附论文和代码)

2023年真是文生图大放异彩的一年&#xff0c;给数字艺术界和创意圈注入了新鲜血液。从起初的基础图像创作跃进到现在的超逼真效果&#xff0c;这些先进的模型彻底变革了我们制作和享受数字作品的途径。 最近&#xff0c;一些大公司比如华为、谷歌、还有Stability AI等人工智能巨…

信用评价研究MATLAB仿真代码

信用评价是各种店铺卖家分析买家信用行为的重要内容, 本文给出随机仿真代码模拟实际交易过程的信用评价. 主要研究内容有: (1)研究最大交易额和信用度的关系 (2)研究买家不评价率对信用度影响 (3)研究交易次数对信用度影响 MATLAB程序如下: 主程序main.m %% clc;close a…

宝塔FTP文件传输服务结合cpolar内网穿透实现远程连接本地服务

⛳️ 推荐 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到网站。 文章目录 ⛳️ 推荐1. Linux安装Cpolar2. 创建FTP公网地址3. 宝塔FTP服务设置4. FTP服务远程连接小结 5. 固定FTP公网地址6. 固…

NVMe TCG安全数据存储简介

NVMe&#xff08;非易失性内存主机控制器接口规范&#xff09;与TCG&#xff08;可信计算组&#xff09;的集成主要体现在数据安全、固件验证和硬件信任根等方面&#xff0c;以确保存储设备的数据保护能力和安全性。 TCG Opal定义了一套针对自加密硬盘&#xff08;SED, Self-En…

人工智能原理实验4(2)——贝叶斯、决策求解汽车评估数据集

&#x1f9e1;&#x1f9e1;实验内容&#x1f9e1;&#x1f9e1; 汽车数据集 车子具有 buying,maint,doors,persons,lug_boot and safety六种属性&#xff0c;而车子的好坏分为uncc,ucc,good and vgood四种。 &#x1f9e1;&#x1f9e1;贝叶斯求解&#x1f9e1;&#x1f9e1;…

gmpy2与一些python库在vscode下没有自动补全的一种缓解方案

经过一定的研究&#xff0c;该问题的原因初步判断是gmpy2这个库天生没有把补全的函数doc说明附在pip包中。且因gmpy2是由C编译而来&#xff0c;以dll或so的形式作为动态链接库给python调用&#xff0c;这意味着无法从源码薅到可用的源码注释。 接下来先讲解决方案&#xff0c;再…

vue3路由报错解决方法

报错&#xff1a; 解决办法&#xff1a; createWebHashHistory是否为 createWebHashHistory()方法 //指定路由模式为哈希模式 修改&#xff1a;改为方法就行了

春节声量高涨213%!如何撬动过年市场?小红书数据洞察入场玩家

春节将临&#xff0c;网上讨论声量渐起。近来关于新年的讨论层出不穷&#xff0c;春运、春晚、放假回家的大学生“德华”……人们开始为新春做准备。前两天&#xff0c;小红书牵手春晚&#xff0c;登上热门。可以预见的是&#xff0c;今年小红书与春节话题将深度绑定&#xff0…

JUC并发编程-集合不安全情况以及Callable线程创建方式

6. 集合不安全 1&#xff09;List 不安全 //java.util.ConcurrentModificationException 并发修改异常&#xff01; public class ListTest {public static void main(String[] args) {List<Object> arrayList new ArrayList<>();for(int i1;i<30;i){new Thr…

【书生·浦语大模型实战营06】《OpenCompass 大模型评测》学习笔记

《OpenCompass 大模型评测》 文档&#xff1a;OpenCompass大模型评测教程 1、主观评测 2、提示词工程 李华每周给2个不同的朋友写一封3页的信&#xff0c;一周写两次。他一年总共写了多少页的信? 李华每周给2个不同的朋友写一封3页的信&#xff0c; 一周写两次。他一年总共…

[代码随想录2]51单片机1T/12T到底怎么选?

为什么说51单片机怎么选&#xff1f; 时至今日&#xff0c;44年来51单片机自强不息&#xff0c;怎么描述它&#xff0c;堪称控制芯片中的王者&#xff01;&#xff01;&#xff01; 假设你21岁大学毕业进入社会&#xff0c;交社保交到今天恭喜你成功退休了214465 传统即标准5…

【docker】安装 CentOS

查看可用的 CentOS版本 docker search centOS拉取 CentOS最新镜像 docker pull centos:latest 查看本地镜像 docker images运行容器 docker run -itd --name centos-demo centos查看进程 docker ps进入centos容器 docker exec -it centos-demo /bin/bash停止容器 docker …

w23靶场安装

一、实验环境 服务器&#xff1a;phpstudyv8.1.13 靶场&#xff1a;Bees二、实验目的 提供一个靶场环境 三、实验步骤 bees靶场安装 1.启动小皮的apache和mysql 2.在小皮V8.1.1.3版本上创建bees网站&#xff0c;选择的php版本最好在5.x&#xff0c;不然会有php解析错误。…

ubuntu 安装protobuf

apt 安装 sudo apt install protobuf-compiler 编译安装 – 方式1 资料链接&#xff1a;ubuntu环境 安装ncnn_ubuntu ncnn_jbyyy、的博客-CSDN博客 git clone https://github.com/google/protobuf.git cd protobuf git submodule update --init --recursive ./autogen.sh …

性能优化-OpenCL kernel 开发

「发表于知乎专栏《移动端算法优化》」 本文主要介绍OpenCL的 Kernel&#xff0c;包括代码的实例以及使用注意的详解。 &#x1f3ac;个人简介&#xff1a;一个全栈工程师的升级之路&#xff01; &#x1f4cb;个人专栏&#xff1a;高性能&#xff08;HPC&#xff09;开发基础教…

Leetcode刷题笔记题解(C++):670. 最大交换

思路&#xff1a; 假设数字 9923676 从右边找最大的数字的下标maxindex&#xff0c;然后向左边寻找小于最大数字的数的下标&#xff0c;直到找到最左边&#xff0c;交换两者得出新的数字&#xff0c;比如从左到右递减的数字如9621则不需要变化&#xff0c;在寻找中记录这种数…

如何查看Linux CPU占有率

目录 1、top 2、htop 3、vmstat 4、mpstat 5、iostat 查看嵌入式设备CPU占有率是评估系统资源使用情况的重要方式。 在Linux系统中&#xff0c;有多种方法可以查看CPU占有率&#xff0c;这里介绍几种常用的命令行工具。 1、top 这是最常用的命令之一&#xff0c;它提供了…
最新文章