理论+实验——ELK日志分析系统

一、ELK日志分析系统

日志服务器

  • 提高安全性
  • 集中存放日志
  • 缺陷
    ◆ 对日志的分析困难

1.1 概述

ELK 是elastic公司提供的一套完整的日志收集以及展示的解决方案,是三个产品的首字母缩写,分别是ElasticSearch、Logstash 和 Kibana。

  • ElasticSearch简称ES,它是一个实时的分布式搜索和分析引擎,它可以用于全文搜索,结构化搜索以及分析。它是一个建立在全文搜索引擎 Apache Lucene 基础上的搜索引擎,使用 Java 语言编写。
  • Logstash是一个具有实时传输能力的数据收集引擎,用来进行数据收集(如:读取文本文件)、解析,并将数据发送给ES。
  • Kibana为 Elasticsearch 提供了分析和可视化的 Web 平台。它可以在 Elasticsearch 的索引中查找,交互数据,并生成各种维度表格、图形。

注:
filebeat:轻量级日志搜集器
Logstash:比较占用资源,但是Logstash具有格式转换,可以转换为能被ES能识别的格式

1.2 日志处理步骤

  • 将日志进行集中化管理
  • 将日志格式化(Logstash)并输出到Elasticsearch
  • 对格式化后的数据进行索引和存储(Elasticsearch)
  • 前端数据的展示(Kibana)

在这里插入图片描述

1.3 Elasticsearch

■ Elasticsearch是用Java开发的,提供了一个分布式多用户能力的全文搜索引擎,设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
■ Elasticsearch核心概念

  • 接近实时(NRT)
    ◆ elasticsearch是一个接近实时的搜索平台,这意味着,从索引一个文档直到这个文档能够被搜索到有一个轻微的延迟(通常是1秒)
  • 集群(cluster)
    ◆ 集群包含多个节点,每个节点属于哪个集群是通过一个配置(集群名称,默认是elasticsearch)来决定的,对于中小型应用来说,刚开始一个集群就一个节点很正常、
  • 节点(node)
    ◆ 集群中的一个节点,节点也有一个名称(默认是随机分配的),节点名称很重要(在执行运维管理操作的时候),默认节点会去加入一个名称为“elasticsearch”的集群,如果直接启动一堆节点,那么它们会自动组成一个elasticsearch集群,当然一个节点也可以组成一个elasticsearch集群。
  • 索引(index)
    ◆ 一个索引就是一个拥有几分相似特征的文档的集合。比如说,你可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据的索引。一个索引由一个名字来标识(必须全部是小写字母的),并且当我们要对对应于这个索引中的文档进行索引、搜索、更新和删除的时候,都要使用到这个名字。在一个集群中,如果你想,可以定义任意多的索引。
    索引相对于关系型数据库的库。
    ◆ ElasticSearch将它的数据存储在一个或多个索引(index)中。用SQL领域的术语来类比,索引就像数据库,可以向索引写入文档或者从索引中读取文档,并通过ElasticSearch内部使用Lucene将数据写入索引或从索引中检索数据。
  • 文档(document)
    ◆ 文档(document)是ElasticSearch中的主要实体。对所有使用ElasticSearch的案例来说,他们最终都可以归结为对文档的搜索。文档由字段构成。
  • 分片(shards)
    ◆ 代表索引分片,es可以把一个完整的索引分成多个分片,这样的好处是可以把一个大的索引拆分成多个,分布到不同的节点上。构成分布式搜索。分片的数量只能在索引创建前指定,并且索引创建后不能更改。5.X默认不能通过配置文件定义分片
  • 副本(replicas)
    ◆ 代表索引副本,es可以设置多个索引的副本,副本的作用一是提高系统的容错性,当个某个节点某个分片损坏或丢失时可以从副本中恢复。二是提高es的查询效率,es会自动对搜索请求进行负载均衡。

1.4、Logstash 介绍

  • 一款强大的数据处理工具
  • 可以实现数据传输、格式处理、格式化输出
  • 数据输入(collect)、数据加工(如过滤、改写等enrich)以及数据输出(transport)

Logstash的理念很简单,它只做3件事情

  • Collect:数据输入
  • Enrich:数据加工,如过滤,改写等
  • Transport:数据输出

LogStash的主要组件

  • Shipper:日志收集者 。负责监控本地日志文件的变化,及时把日志文件的最新内容收集起来。通常,远程代理端(agent)只需要运行这个组件即可
  • Indexer:日志存储者。负责接收日志并写入到本地文件
  • Broker:日志Hub。负责连接多个Shipper和多个Indexer
  • Search and Storage:允许对事件进行搜索和存储
  • Web Interface:基于Web的展示界面
    正是由于以上组件在LogStash架构中可独立部署,才提供了更好的集群扩展性

LogStash主机分类

  • 代理主机(agent host):作为事件的传递者(shipper),将各种日志数据发送至中心主机;只需运行Logstash 代理(agent)程序;
  • 中心主机(central host):可运行包括中间转发器(Broker)、索引器(Indexer)、搜索和存储器(Search and Storage)、Web界面端(Web Interface)在内的各个组件,以实现对日志数据的接收、处理和存储

1.5、Kiabana 介绍

  • 一个针对Elasticsearch的开源分析及可视化平台
  • 搜索、查看交互存储在Elasticsearch索引中的数据
  • 通过各种图表进行高级数据分析及展示

Kiabana主要功能

  • Elasticsearch无缝之集成
    ◆ Kibana架构为Elasticsearch定制,可以将任何结构化和非结构化加入Elasticsearch索引,Kibana还充分利用了Elasticsearch强大的搜索和分析功能
  • 整合数据,复杂数据分析
    ◆ 根据海量数据创建柱形图、折线图、三代泥土、直方图、饼图和地图;提升了Elasticsearch地分析能力,更够更加智能化地分析数据,执行数学转化并且根据要求对数据切割分块
  • 让更多团队成员受益
    ◆ 强大的数据可视化让各个业务岗位都可以直观了解数据
  • 接口灵活,分享更容易
    ◆ 使用Kibana可以更加方便地创建保存和分享数据,并将可视化数据快速交流
  • 配置简单,可视化多数据源
    ◆ Kibana地配置和启动非常简单,用户体验友好,Kibana自带web服务器,可以快速运行;kibana可以非常方便地把来自logstash、ES-hadoop、Beats或第三方技术整合到Elasticsearch,支持地第三方技术包括apacheflume、fluentd等
  • 简单数据导出
    ◆ Kibana可以方便地导出感兴趣地数据,与其他数据融合后快速建模分析,发现新结果

二、ELK日志分析系统

2.1 配置elasticsearch环境

登录20.0.0.23 更改主机名,配置域名解析。查看Java环境

[root@localhost ~]# hostnamectl set-hostname node1
[root@localhost ~]# su
[root@node1 ~]# vim /etc/hosts
20.0.0.23 node1
20.0.0.24 node2
[root@node1 ~]# ping node2 
PING node2 (20.0.0.24) 56(84) bytes of data.
64 bytes from node2 (20.0.0.24): icmp_seq=1 ttl=64 time=0.323 ms
[root@node1 ~]# 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)

登录20.0.0.24 更改主机名,配置域名解析。查看Java环境

[root@localhost ~]# hostnamectl set-hostname node2
[root@localhost ~]# su
[root@node2 ~]# vim /etc/hosts
20.0.0.23 node1
20.0.0.24 node2
[root@node2 ~]# 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)

部署elasticsearch软件
将elasticsearch-5.5.0.rpm拖到/opt目录下
登录20.0.0.23

1、安装elasticsearch—rpm包

[root@node1 opt]# rpm -ivh elasticsearch-5.5.0.rpm

2、加载系统服务

[root@node1 opt]# systemctl daemon-reload 
[root@node1 opt]# systemctl enable elasticsearch.service

3、更改elasticsearch主配置文件

[root@node1 opt]# cp -p /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak           ###备份
[root@node1 opt]# vim /etc/elasticsearch/elasticsearch.yml
17行 cluster.name: my-elk-cluster                ###集群名字
23行 node.name: node1                       ###节点名字
33行 path.data: /data/elk_data                  ###数据存放路径
37行 path.logs: /var/log/elasticsearch/            ###日志存放路径
43行 bootstrap.memory_lock: false              ###不再启动的时候锁定内存
55行 network.host: 0.0.0.0                     ###提供服务绑定的IP地址,0.0.0.0代表所有地址
59行 http.port: 9200                          ###监听端口为9200
68 discovery.zen.ping.unicast.hosts: ["node1", "node2"]     ###集群发现通过单播实现,节点名称
[root@node1 opt]# grep -v "^#" /etc/elasticsearch/elasticsearch.yml
cluster.name: my-elk-cluster
node.name: node1
path.data: /data/elk_data
path.logs: /var/log/elasticsearch/
bootstrap.memory_lock: false
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["node1", "node2"]

4、创建数据存放路径并授权

[root@node1 opt]# mkdir -p /data/elk_data
[root@node1 opt]# chown elasticsearch.elasticsearch /data/elk_data/

5、启动elasticsearch,查看是否成功开启

[root@node1 opt]# systemctl start elasticsearch.service 
[root@node1 opt]# netstat -anpt | grep 9200                   ###有时候可能会慢,等3、4秒就会显示了
tcp6       0      0 :::9200                 :::*                    LISTEN      3222/java           

6、查看节点信息,用真机浏览器登录:20.0.0.23:9200,有文件打开,下面是节点的信息
在这里插入图片描述
登录20.0.0.24
步骤与20.0.0.23一样

在这里插入图片描述
集群检查健康和状态
在真机浏览器登录:20.0.0.23:9200/_cluster/health?pretty ###检查群集健康情况
20.0.0.24同样如此

在这里插入图片描述
在真机浏览器登录:20.0.0.23:9200/_cluster/state?pretty,检查群集状态信息
20.0.0.24同样如此

在这里插入图片描述
【安装elasticsearch-head插件】上述查看集群的方式及其不方便,我们可以通过安装elasticsearch-head插件后,来管理集群
登录20.0.0.23

1、将node软件包拖到/opt目录下

[root@node1 opt]# yum -y install gcc gcc-c++ make

2、编译安装node组件依赖包,耗时时间较长

[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
[root@node1 node-v8.2.1]# make -j3
[root@node1 node-v8.2.1]# make install

3、安装phantomjs###前端架构

[root@node1 opt]# tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@node1 opt]# cd /usr/local/src/bin/
[root@node1 bin]# cp phantomjs /usr/local/bin/
**4、安装elasticsearch-head###数据可视化工具**
[root@node1 opt]# 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

5、修改主备注文件

[root@node1 elasticsearch-head]# cd ~
[root@node1 ~]# vim /etc/elasticsearch/elasticsearch.yml      ###下面配置文件,插末尾
http.cors.enabled: true
http.cors.allow-origin: "*"
[root@node1 ~]# systemctl restart elasticsearch.service

6、启动elasticsearch-head 启动服务器

[root@node1 ~]# cd /usr/local/src/elasticsearch-head/
[root@node1 elasticsearch-head]# npm run start &                 ###切换到后台运行
[2] 88054
[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 -anpt | grep 9100
tcp        0      0 0.0.0.0:9100            0.0.0.0:*               LISTEN      88064/grunt         
[root@node1 elasticsearch-head]# netstat -anpt | grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      87951/java          

在真机浏览器登录:20.0.0.23:9100
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
登录20.0.0.23 ###索引为index-demo,类型为test,可以看到创建成功

[root@node1 ~]# curl -XPUT '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
}

在这里插入图片描述
登录20.0.0.24
与20.0.0.23一样

2.2 配置logstash环境

1、更改主机名

[root@localhost ~]# hostnamectl set-hostname apache
[root@localhost ~]# su

2、安装apache服务(httpd)

[root@apache ~]# yum -y install httpd
[root@apache ~]# systemctl start httpd
[root@apache ~]# systemctl enable httpd

3、安装Java环境,如果没有装,yum -y install java

[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)

4、安装logstash
将logstash压缩包拖到/opt目录下

[root@apache opt]# rpm -ivh logstash-5.5.1.rpm
[root@apache opt]# systemctl start logstash.service 
[root@apache opt]# systemctl enable logstash.service 
[root@apache opt]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/

5、Logstash(Apache)与elasticsearch(node)功能是否正常,做对接测试
Logstash这个命令测试
字段面试解释:

  • f:通过这个选项可以指定logstash的配置文件,根据配置文件配置logstash
  • e:后面跟着字符串,该字符串可以被当做logstash的配置(如果是” ”,则默认使用stdin作为输入、stdout作为输出)
  • t:测试配置文件是否正确,然后退出

6、输入采用标准输入、输出采用标准输出

[root@apache opt]# logstash -e 'input { stdin{} } output { stdout{} }'
'###这里的error是不影响的'
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
12:04:53.791 [[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}
12:04:53.866 [[main]-pipeline-manager] INFO  logstash.pipeline - Pipeline main started
The stdin plugin is now waiting for input:
12:04:53.957 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com
2020-10-29T04:09:20.501Z apache www.baidu.com
www.sina.com.cn
2020-10-29T04:09:33.559Z apache www.sina.com.cn

7、使用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
12:13:43.787 [[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}
12:13:43.872 [[main]-pipeline-manager] INFO  logstash.pipeline - Pipeline main started
The stdin plugin is now waiting for input:
12:13:43.955 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com
{
    "@timestamp" => 2020-10-29T04:13:51.356Z,
      "@version" => "1",
          "host" => "apache",
       "message" => "www.baidu.com"
}

'###使用logstash将信息写入elasticsearch中'

[root@apache opt]# logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["20.0.0.23:9200"] } }'

The stdin plugin is now waiting for input:
12:19:04.803 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com
www.sinacon.com
www.goole.com.cn

8、真机登录浏览器:20.0.0.23:9100 查看索引信息,多出logstash-2020.10.29,点击浏览查看相应内容
在这里插入图片描述
9、登录 20.0.0.21 apache主机做对接配置

'###logstash配置文件###'
Logstash配置文件主要由三部分组成:input、output、filter(根据需要)
###filter:文件管理者或者过滤器
[root@apache opt]# ll /var/log/messages 
-rw-------. 1 root root 1086007 10月 29 12:27 /var/log/messages
[root@apache opt]# chmod o+r /var/log/messages    ###给其他用户读取权限
[root@apache opt]# ll /var/log/messages 
-rw----r--. 1 root root 1088649 10月 29 12:28 /var/log/messages
[root@apache conf.d]# vim system.conf
input {
    file{
     path => "/var/log/messages"
     type => "system"
     start_position => "beginning"
     }
    }
output {
    elasticsearch {
     hosts => ["20.0.0.23:9200"]
     index => "system-%{+YYYY.MM.dd}"
     }
    }
[root@apache conf.d]# systemctl restart logstash.service

真机浏览器登录:20.0.0.23:9100
查看索引信息,多出system-2020.10.29

在这里插入图片描述

2.3 配置kibana环境

登录20.0.0.23 node1主机
在node1安装kibana

'将kibana的压缩包拖到/usr/local/src目录下'
[root@node1 src]# rpm -ivh kibana-5.5.1-x86_64.rpm
[root@node1 src]# cd /etc/kibana/
[root@node1 kibana]# cp -p kibana.yml kibana.yml.bak                 ###备份
[root@node1 kibana]# vim kibana.yml
2行 server.port: 5601                 ### kibana打开的端口
7行 server.host: "0.0.0.0"              ### kibana监听的地址
21行 elasticsearch.url: "http://20.0.0.23:9200"      ### 和elasticsearch建立联系
'###生产环境中需要做负载均衡,用负载均衡地址或者是Vip地址###'
30行 kibana.index: ".kibana"       ### 在elasticsearch中添加.kibana索引
[root@node1 kibana]# systemctl start kibana.service 
[root@node1 kibana]# systemctl enable kibana.service

真机浏览器登录:20.0.0.23:5601
首次登录创建一个索引名字:system-*,这是对接系统日志文件

步骤如下:
① 找到Index name or pattern,下面输入system-*
② 点最下面出现的create按钮创建
③ 点击最左上角的Discover按钮,会发现system-*信息
④ 点下面的host旁边的add,会发现右面的图只有Time和host选项了,这个比较友好
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
对接Apache主机的日志文件(访问的、错误的)

[root@apache conf.d]# touch apache_log.conf
[root@apache conf.d]# vim 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 => ["20.0.0.23:9200"]
         index => "apache_access-%{+YYYY.MM.dd}"
         }
        }
        if [type] == "error" {
        elasticsearch {
         hosts => ["20.0.0.23:9200"]
         index => "apache_error-%{+YYYY.MM.dd}"
         }
        }
}
[root@apache conf.d]# logstash -f apache_log.conf

真机浏览器输入:20.0.0.23:9100
可以看到索引信息多了两条
apache_error-2020.10.29
apache_access-2020.10.29

在这里插入图片描述
真机浏览:20.0.0.23:5601
20.0.0.40:5601
左边选项最后一个Management ----> Index Patterns ---->Create Index Pattern
分别创建 apache_access-
和 apache_error-
的索引
创建完之后要等一段时间才能有数据显示**
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值