ELK日志分析系统

ELK日志分析系统

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

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

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

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

可以添加的其它组件

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

filebeat结合logstash带来好处

1.通过Logstash具有基于磁盘的自适应缓冲系统,该系统将吸收传入的吞吐量,从而减轻Elasticsearch持续写入数据的压力

2.从其他数据源(例如数据库,s3对象存储或消息传递队列)中提取

3.将数据发送到多个目的地,例如s3,HDFS (Hadoop分布式文件系统)或写入文件

4.使用条件数据流逻辑组成更复杂的处理管道

  • 缓存/消息队列(redis、 kafka、RabbitNQ等):可以对高并发日志数据进行流量削峰和缓冲,这样的缓冲可以一定程度的保护数据不丢失,还可以对整个架构进行应用解耦

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


在这里插入图片描述

为什么要使用ELK

日志主要包括系统日志、应用程序日志和安全日志。系统运维和开发人员可以通过日志了解服务器软硬件信息、检查配置过程中的错误及错误发生⒂原因。经常分析日志可以了解服务器的负荷,性能安全性,从而及时采取措施纠正错误

往往单台机器的日志我们使用grep、awk等工具就能基本实现简单分析,但是当日志被分散的储存不同的设备上。如果你管理数十上百台服务器,你还在使用依次登录每台机器的传统方法查阅日志。这样是不是感觉很繁琐和效率低下。当务之急我们使用集中化的日志管理,例如。开源的syslog,将所有服务器上的日志收集汇总。集中化管理日志后,日志的统计和检索又成为一件比较麻烦的事情,一般使用grep、awk和wc等Linux命令能实现检索和统计,但是对于要求更高的查询、排序和统计等要求和庞大的机器数量依然使用这样的方法难免有点力不从心

一般大型系统是一个分布式部署的架构,不同的服务模块部署在不同的服务器上,问题出现时,大部分情况需要根据问题暴露的关键信息,定位到具体的服务器和服务模块,构建一套集中式日志系统,可以提高定位问题的效率

完整目志系统基本特征

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

ELK的工作原理

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

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

ELK日志分析系统集群部署

服务器类型系统和IP地址需要安装的组件硬件方面
Node1节点CentOS7.4(64 位),192.168.237.148Elasticsearch 、 Kibana、Filebeat4G/4核
Node2节点CentOS7.4(64 位),192.168.237.138Elasticsearch4G/4核
Apache节点CentOS7.4(64 位),192.168.237.130Logstash4G/4核

ELK Elasticsearch 集群部署

node1,node2(这里仅演示node1的操作,两边操作一样)

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/hosts
192.168.237.148   node1
192.168.237.138   node2
:wq
[root@localhost ~]# 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)
[root@localhost ~]# cd /opt
[root@node1 opt]# rpm -ivh elasticsearch-5.5.0.rpm 				#安装elasticsearch
警告:elasticsearch-5.5.0.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
准备中...                          ################################# [100%]
Creating elasticsearch group... OK
Creating elasticsearch user... OK
正在升级/安装...
   1:elasticsearch-0:5.5.0-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

[root@node1 opt]# systemctl daemon-reload    					#加载系统服务
[root@node1 opt]# systemctl enable elasticsearch.service		#加载系统服务
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.

[root@node1 opt]# cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak	#备份配置文件
[root@node1 opt]# vim /etc/elasticsearch/elasticsearch.yml			#修改配置文件
--17--取消注释,指定集群名字
cluster.name: my-elk-cluster
--23--取消注释,指定节点名字:Node1节点为node1,Node2节点为node2
node.name: node1
--33--取消注释,指定数据存放路径
path.data: /data/elk_data
--37--取消注释,指定日志存放路径
path.logs: /var/log/elasticsearch/
--43--取消注释,改为在启动的时候不锁定内存
bootstrap.memory_lock: false
--55--取消注释,设置监听地址,0.0.0.0代表所有地址
network.host: 0.0.0.0
--59--取消注释,ES 服务的默认监听端口为9200
http.port: 9200
--68--取消注释,集群发现通过单播实现,指定要发现的节点 node1、node2
discovery.zen.ping.unicast.hosts: ["node1", "node2"]
:wq
[root@node1 opt]# grep -v "^#" /etc/elasticsearch/elasticsearch.yml		#验证修改的配置项
cluster.name: my-elk-cluster
node.name: node-1
path.data: /path/to/data
path.logs: /path/to/logs
bootstrap.memory_lock: false
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["node1", "node2"]

[root@node1 opt]# mkdir -p /data/elk_data			#创建数据存放路径
[root@node1 opt]# chown elasticsearch:elasticsearch /data/elk_data/		#授权
[root@node1 opt]# systemctl start elasticsearch.service			#启动服务
[root@node1 opt]# netstat -antp | grep 9200						#验证
tcp6       0      0 :::9200                 :::*                    LISTEN      53120/java  

验证:浏览器分别访问192.168.237.148:9200192.168.237.138:9200
node1
{
  "name" : "node1",
  "cluster_name" : "my-elk-cluster",
  "cluster_uuid" : "1O1LXRpGSxOSpYOSGNK6ww",
  "version" : {
    "number" : "5.5.0",
    "build_hash" : "260387d",
    "build_date" : "2017-06-30T23:16:05.735Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}
node2
{
  "name" : "node2",
  "cluster_name" : "my-elk-cluster",
  "cluster_uuid" : "1O1LXRpGSxOSpYOSGNK6ww",
  "version" : {
    "number" : "5.5.0",
    "build_hash" : "260387d",
    "build_date" : "2017-06-30T23:16:05.735Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}

浏览器访问192.168.237.148:9200/_cluster/health?pretty和http://192.168.237.138:9200/_cluster/health?pretty查看群集的健康情况,可以看到 status 值为 green(绿色),表示节点健康运行。

node1
{
  "cluster_name" : "my-elk-cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}
node2
{
  "cluster_name" : "my-elk-cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}

安装 Elasticsearch-head 插件,可以更方便地管理群集
#Elasticsearch在5.0 版本后,Elasticsearch-head插件需要作为独立服务进行安装,需要使用npm工具(NodeJS的包管理工具)安装
#安装Elasticsearch-head 需要提前安装好依赖软件 node 和 phantomjs
#node:是一个基于Chrome V8引擎的 JavaScript 运行环境
#phantomjs:是一个基于webkit的JavaScriptAPI,可以理解为一个隐形的浏览器,任何基于webkit浏览器做的事情,它都可以做到

node1
[root@node1 opt]# yum install gcc gcc-c++ make -y	#安装依赖
[root@node1 opt]# tar zxvf node-v8.2.1.tar.gz		#解压数据包
[root@node1 opt]# cd node-v8.2.1/
[root@node1 node-v8.2.1]# ./configure				#编译
......
creating ./config.gypi
creating ./config.mk
[root@node1 node-v8.2.1]# make && make install		#安装

[root@node1 node-v8.2.1]# tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /usr/local/src/	#解压
[root@node1 node-v8.2.1]# cd /usr/local/src/phantomjs-2.1.1-linux-x86_64/bin		
[root@node1 bin]# cp phantomjs /usr/local/bin			#备份
[root@node1 bin]# cd /opt
[root@node1 bin]# tar zxvf elasticsearch-head.tar.gz -C /usr/local/src/			#解压
[root@node1 opt]# cd /usr/local/src/elasticsearch-head/

[root@node1 elasticsearch-head]# npm install						#安装
npm WARN deprecated fsevents@1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/karma/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN elasticsearch-head@0.0.0 license should be a valid SPDX license expression

up to date in 2.247s

[root@node1 elasticsearch-head]# vim /etc/elasticsearch/elasticsearch.yml
--末尾添加以下内容--
http.cors.enabled: true				#开启跨域访问支持,默认为 false
http.cors.allow-origin: "*"			#指定跨域访问允许的域名地址为所有
:wq
[root@node1 elasticsearch-head]# systemctl restart elasticsearch		#重启服务
[root@node1 elasticsearch-head]# netstat -antp |grep 9200			#验证
tcp6       0      0 :::9200                 :::*                    LISTEN      98919/java          
	
[root@node1 elasticsearch-head]# cd /usr/local/src/elasticsearch-head/	#必须在解压后的elasticsearch-head目录下启动服务,进程会读取该目录下的gruntfile.js 文件,否则可能启动失败。
[root@node1 elasticsearch-head]# npm run start &						#启动head服务
[1] 99014
[root@node1 elasticsearch-head]# 
> elasticsearch-head@0.0.0 start /usr/local/src/elasticsearch-head
> grunt server

Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100

[root@node1 elasticsearch-head]# netstat -natp |grep 9100		#验证服务
tcp        0      0 0.0.0.0:9100            0.0.0.0:*               LISTEN      99033/grunt         

验证:浏览器登陆192.168.237.148:9100并且在Elasticsearch这一栏旁边输入服务器IP加端口信息,如果看到群集健康值为 green 绿色,代表群集很健康

#插入索引测试,索引为 index-demo,类型为 test
[root@node1 elasticsearch-head]# curl -X PUT 'localhost:9200/index-demo/test/1?pretty&pretty' -H 'content-Type: application/json' -d '{"user":"zhangsan","mesg":"hello world"}'
{
  "_index" : "index-demo",
  "_type" : "test",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : false
}

浏览器访问 http://192.168.237.148:9100查看索引信息,可以看见索引默认被分片5个,并且有一个副本。
点击“数据浏览”,会发现在node1和node2上创建的索引为 index-demo,类型为 test 的相关信息。

ELK Logstash 部署

apache服务器

[root@localhost ~]# hostnamectl set-hostname apache
[root@localhost ~]# su
[root@apache ~]# yum -y install httpd
[root@apache ~]# systemctl stop firewalld
[root@apache ~]# setenforce 0
[root@apache ~]# systemctl start httpd
[root@apache ~]# 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)
[root@apache opt]# cd /opt
[root@apache opt]# rpm -ivh logstash-5.5.1.rpm     #安装logstash                     
警告:logstash-5.5.1.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:logstash-1:5.5.1-1               ################################# [100%]
Using provided startup.options file: /etc/logstash/startup.options
Successfully created system startup script for Logstash
[root@apache opt]# systemctl start logstash.service			#开启服务                      
[root@apache opt]# systemctl enable logstash.service		#开机自启
Created symlink from /etc/systemd/system/multi-user.target.wants/logstash.service to /etc/systemd/system/logstash.service.

[root@apache opt]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/		#创建软链接
  • Logstash 命令常用选项
选项说明
-f通过这个选项可以指定 Logstash 的配置文件,根据配置文件配置 Logstash 的输入和输出流
-e从命令行中获取,输入、输出后面跟着字符串,该字符串可以被当作 Logstash 的配置(如果是空,则默认使用 stdin 作为输入,stdout 作为输出)
-t测试配置文件是否正确,然后退出
[root@apache opt]# logstash -e 'input { stdin{} } output { stdout{} }'	#输入采用标准输入,输出采用标准输出(类似管道)
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
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 to console
20:52:22.268 [main] INFO  logstash.setting.writabledirectory - Creating directory {:setting=>"path.queue", :path=>"/usr/share/logstash/data/queue"}
20:52:22.270 [main] INFO  logstash.setting.writabledirectory - Creating directory {:setting=>"path.dead_letter_queue", :path=>"/usr/share/logstash/data/dead_letter_queue"}
20:52:22.285 [LogStash::Runner] INFO  logstash.agent - No persistent UUID file found. Generating new UUID {:uuid=>"3506ea60-f6eb-4fd1-87f0-da20af47ad34", :path=>"/usr/share/logstash/data/uuid"}
20:52:22.366 [[main]-pipeline-manager] INFO  logstash.pipeline - Starting pipeline {"id"=>"main", "pipeline.workers"=>4, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>500}
20:52:22.385 [[main]-pipeline-manager] INFO  logstash.pipeline - Pipeline main started
The stdin plugin is now waiting for input:
20:52:22.421 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com			#输入内容
2022-07-02T12:53:33.542Z apache www.baidu.com		#输出结果

#ctrl+c 退出

#使用 rubydebug 输出详细格式显示,codec 为一种编解码器
[root@apache opt]# logstash -e 'input { stdin{} } output { stdout{ codec=>rubydebug } }'
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
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 to console
20:57:42.038 [[main]-pipeline-manager] INFO  logstash.pipeline - Starting pipeline {"id"=>"main", "pipeline.workers"=>4, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>500}
20:57:42.079 [[main]-pipeline-manager] INFO  logstash.pipeline - Pipeline main started
The stdin plugin is now waiting for input:
20:57:42.197 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com				#键入内容(标准输入)
{
    "@timestamp" => 2022-07-02T12:58:01.751Z,		#输出结果(处理后的结果)
      "@version" => "1",
          "host" => "apache",
       "message" => "www.baidu.com"
}

[root@apache opt]# logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["192.168.237.148:9200"] } }'					#使用Logstash将信息写入Elasticsearch中
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
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 to console
20:59:43.508 [[main]-pipeline-manager] INFO  logstash.outputs.elasticsearch - Elasticsearch pool URLs updated {:changes=>{:removed=>[], :added=>[http://192.168.237.148:9200/]}}
20:59:43.509 [[main]-pipeline-manager] INFO  logstash.outputs.elasticsearch - Running health check to see if an Elasticsearch connection is working {:healthcheck_url=>http://192.168.237.148:9200/, :path=>"/"}
20:59:43.559 [[main]-pipeline-manager] WARN  logstash.outputs.elasticsearch - Restored connection to ES instance {:url=>#<Java::JavaNet::URI:0x5d957264>}
20:59:43.563 [[main]-pipeline-manager] INFO  logstash.outputs.elasticsearch - Using mapping template from {:path=>nil}
20:59:43.663 [[main]-pipeline-manager] INFO  logstash.outputs.elasticsearch - Attempting to install template {:manage_template=>{"template"=>"logstash-*", "version"=>50001, "settings"=>{"index.refresh_interval"=>"5s"}, "mappings"=>{"_default_"=>{"_all"=>{"enabled"=>true, "norms"=>false}, "dynamic_templates"=>[{"message_field"=>{"path_match"=>"message", "match_mapping_type"=>"string", "mapping"=>{"type"=>"text", "norms"=>false}}}, {"string_fields"=>{"match"=>"*", "match_mapping_type"=>"string", "mapping"=>{"type"=>"text", "norms"=>false, "fields"=>{"keyword"=>{"type"=>"keyword", "ignore_above"=>256}}}}}], "properties"=>{"@timestamp"=>{"type"=>"date", "include_in_all"=>false}, "@version"=>{"type"=>"keyword", "include_in_all"=>false}, "geoip"=>{"dynamic"=>true, "properties"=>{"ip"=>{"type"=>"ip"}, "location"=>{"type"=>"geo_point"}, "latitude"=>{"type"=>"half_float"}, "longitude"=>{"type"=>"half_float"}}}}}}}}
20:59:43.672 [[main]-pipeline-manager] INFO  logstash.outputs.elasticsearch - Installing elasticsearch template to _template/logstash
20:59:43.726 [[main]-pipeline-manager] INFO  logstash.outputs.elasticsearch - New Elasticsearch output {:class=>"LogStash::Outputs::ElasticSearch", :hosts=>[#<Java::JavaNet::URI:0x4c4f6bdc>]}
20:59:43.730 [[main]-pipeline-manager] INFO  logstash.pipeline - Starting pipeline {"id"=>"main", "pipeline.workers"=>4, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>500}
20:59:43.765 [[main]-pipeline-manager] INFO  logstash.pipeline - Pipeline main started
The stdin plugin is now waiting for input:
20:59:43.830 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com							#输入内容
www.sina.com.cn							#输入内容
www.google.com							#输入内容
##没有输出结果

#结果不在标准输出显示,而是发送至 Elasticsearch 中,可浏览器访问 http://192.168.237.148:9100/ 查看索引信息和数据浏览
定义logstash配置文件
Logstash 配置文件基本由三部分组成:input、output 以及 filter(可选,根据需要选择使用)。

#格式如下:
input {...}
filter {...}
output {...}

#在每个部分中,也可以指定多个访问方式。例如,若要指定两个日志来源文件,则格式如下:
input {
	file { path =>"/var/log/messages" type =>"syslog"}
	file { path =>"/var/log/httpd/access.log" type =>"apache"}
}
#修改 Logstash 配置文件,让其收集系统日志/var/log/messages,并将其输出到 elasticsearch 中

[root@apache opt]# chmod +r /var/log/messages			#让 Logstash 可以读取日志
[root@apache opt]# vim /etc/logstash/conf.d/system.conf	#修改配置文件
input {
    file{
        path =>"/var/log/messages"		#指定要收集的日志的位置
        type =>"system"					#自定义日志类型标识
        start_position =>"beginning"	#表示从开始处收集
    }
}
output {
    elasticsearch {						#输出到elasticsearch
        hosts => ["192.168.237.148:9200","192.168.237.138:9200"]	#指定elasticsearch服务器的地址和端口
        index =>"system-%{+YYYY.MM.dd}"		#指定输出到elasticsearch的索引格式
    }
}
                          
[root@apache opt]# systemctl restart logstash 

验证
浏览器访问 http://192.168.237.148:9100/ 查看数据浏览--索引对应的日志文件

ELK Kiabana 部署

node1

[root@node1 opt]# rpm -ivh kibana-5.5.1-x86_64.rpm			#安装kibana
警告:kibana-5.5.1-x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:kibana-5.5.1-1                   ################################# [100%]

--2--取消注释,Kiabana 服务的默认监听端口为5601
server.port: 5601
--7--取消注释,设置 Kiabana 的监听地址,0.0.0.0代表所有地址
server.host: "0.0.0.0"
--28--取消注释,配置es服务器的ip,如果是集群则配置该集群中master节点的ip
elasticsearch.url:  ["http://192.168.80.10:9200","http://192.168.80.11:9200"] 
--37--取消注释,设置在 elasticsearch 中添加.kibana索引
kibana.index: ".kibana"
--86--取消注释,配置kibana的日志文件路径(需手动创建),不然默认是messages里记录日志
logging.dest: /var/log/kibana.log
:wq

[root@node1 opt]# systemctl start kibana.service			#启动kibana
[root@node1 opt]# systemctl enable kibana.service			#加入自启动
Created symlink from /etc/systemd/system/multi-user.target.wants/kibana.service to /etc/systemd/system/kibana.service.

[root@node1 opt]# netstat -natp | grep 5601					#验证服务
tcp        0      0 0.0.0.0:5601            0.0.0.0:*               LISTEN      100970/node       

验证
浏览器访问 http://192.168.237.148:5601

第一次登录需要添加一个Elasticsearch索引
Index name or pattern
输入:system-*			#在索引名中输入之前配置的 Output 前缀“system”

单击 “create” 按钮创建,单击 “Discover” 按钮可查看图表信息及日志信息。
数据展示可以分类显示,在“Available Fields”中的“host”,然后单击 “add”按钮,可以看到按照“host”筛选后的结果
将Apache服务器的日志(访问的、错误的)添加到Elasticsearch并通过Kibana显示

apache服务器
[root@apache opt]# vim /etc/logstash/conf.d/apache_log.conf
vim /etc/logstash/conf.d/apache_log.conf
input {
    file{
        path => "/etc/httpd/logs/access_log"
        type => "access"
        start_position => "beginning"
    }
    file{
        path => "/etc/httpd/logs/error_log"
        type => "error"
        start_position => "beginning"
    }
}
output {
    if [type] == "access" {
        elasticsearch {
            hosts => ["192.168.80.10:9200"]
            index => "apache_access-%{+YYYY.MM.dd}"
        }
    }
	if [type] == "error" {
        elasticsearch {
            hosts => ["192.168.80.10:9200"]
            index => "apache_error-%{+YYYY.MM.dd}"
        }
    }
}
[root@apache opt]# cd /etc/logstash/conf.d/
[root@apache conf.d]# /usr/share/logstash/bin/logstash -f apache_log.conf


#浏览器访问http://192.168.237.130,apache服务器,产生访问记录,才能生成apache_access索引

#浏览器访问http://192.168.80.10:5601登录Kibana,单击“Create Index Pattern”按钮添加索引, 在索引名中输入之前配置的Output 前缀 apache_access-*,并单击“Create”按钮。在用相同的方法添加 apache_error-*索引。
 
#选择“Discover”选项卡,在中间下拉列表中选择刚添加的 apache_access-* 、apache_error-* 索引,可以查看相应的图表及日志信息

Filebeat+ELK 部署

node1

[root@node1 opt]# tar zxvf filebeat-6.2.4-linux-x86_64.tar.gz
[root@node1 opt]# cd /usr/local/filebeat
[root@node1 filebeat]# vim filebeat.yml
filebeat.prospectors:
- type: log         #指定log类型,从日志文件中读取消息
  enabled: true
  paths:
    - /var/log/messages       #指定监控的日志文件
    - /var/log/*.log
  fields:           #可以使用 fields 配置选项设置一些参数字段添加到output中
    service_name: filebeat
    log_type: log
    service_id: 192.168.237.148

--------------Elasticsearch output-------------------
(全部注释掉)

----------------Logstash output---------------------
output.logstash:
  hosts: ["192.168.237.130:5044"]      #指定 logstash 的 IP 和端口
:wq

[root@node1 filebeat]# ./filebeat -e -c filebeat.yml				#启动filebeat
apache服务器

[root@apache opt]# cd /etc/logstash/conf.d
[root@apache conf.d]# vim logstash.conf
input {
    beats {
        port => "5044"
    }
}
output {
    elasticsearch {
        hosts => ["192.168.237.148:9200"]
        index => "%{[fields][service_name]}-%{+YYYY.MM.dd}"
    }   
    stdout {
        codec => rubydebug
    }   
}   
:wq

[root@apache conf.d]# logstash -f logstash.conf			#启动logstash

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

总结:配置文件需要修改的很多,并且配错了,启动时不提示的,需要严谨,谨慎!一招配错,满盘皆输!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值