ELK日志分析(es,logstash,kibana)系统及案例构建详细过程

  • 一,ELK日志分析系统简介
  • 1,日志服务器
  • 2,ELK日志分析系统
  • 3,日志处理步骤
  • 二,elasticsearch介绍
  • 1,ELasticsearch概述
  • 2,ELasticsearch核心概念
  • 三, logstash介绍
  • 1,logstash概述
  • 2,logstash主要组件
  • 四,Kibana介绍
  • 1,Kibana概述
  • 2,Kibana主要功能
  • 五,部署elk日志分析系统
  • 1.需求描述
  • 2, 案例拓扑
  • 1.需求描述
  • 2, 案例拓扑构建说明
  • 3,案例部署详细步骤
  • 1, Elasticsearch配置(下面的操作都需要在node1、node2上配置)
  • 2,Logstash部署(apache上部署)
  • 3,在node1主机安装kibana


一,ELK日志分析系统简介

1,日志服务器

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

2,ELK日志分析系统

  • ELasticsearch
  • logstash
  • kibana

3,日志处理步骤

  • 1,将日志进行集中化管理
  • 2,将日志格式化(logstash)并输出到elasticsearch
  • 3,对格式化后的数据进行索引和存储(elasticsearch )
  • 4,对前段数据的展示(kibana)

二,elasticsearch介绍

1,ELasticsearch概述

  • 提供了一个分布式多用户能力的全文搜索引擎

2,ELasticsearch核心概念

  • 接近实时
  • 集群
  • 节点
  • 索引
  • 索引(库)–类型(表)–文档(记录)
  • 分片和副本

elk 实现 elk项目实例子_elasticsearch

三, logstash介绍

1,logstash概述

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

2,logstash主要组件

  • Shipper :日志收集者。负责监控本地日志文件的变化,及时收集最新的日志文件内容。
  • lndexer : 日志存储者,负责接收日志并写入到本地文件。
  • Broker :日志 Hub。负责连接多个 Shipper 和多个Indexero
  • Search and Storage:允许对事件进行搜索和存储
  • Web Interface : 基于Web的展示界面。
    在Logstash中,包括了三个阶段,分别是输入(Input )、处理(Filter,非必需)和输出(Output)

四,Kibana介绍

1,Kibana概述

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

2,Kibana主要功能

  • ELasticsearch无缝之集成
  • 整合数据,复杂数据分析
  • 让更多团队成员收益
  • 接口灵活,分享很容易
  • 配置简单,可视化多数据源
  • 简单数据导出

五,部署elk日志分析系统

1.需求描述

  • 配置ELK日志分析群集
  • 使用Logstash收集日志
  • 使用Kibana查看分析日志

2, 案例拓扑

1.需求描述

  • 配置ELK日志分析群集
  • 使用Logstash收集日志
  • 使用Kibana查看分析日志

2, 案例拓扑构建说明

elk 实现 elk项目实例子_linux_02

node1

20.0.0.50

Elasticsearch Kinab

node2

20.0.0.60

Elasticsearch

apache

20.0.0.30

httpd Logstash

3,案例部署详细步骤

1, Elasticsearch配置(下面的操作都需要在node1、node2上配置)

3.1.1 导入包,并修改yum源

[root@node1 ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
[root@node1 ~]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# vi elasticsearch.repo
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enable=1

[root@node1 ~]# yum list       #查看yum清单
[root@node1 ~]# yum install elasticsearch -y    #安装elasticsearch
[root@node1 ~]# yum install java -y      #安装Java
[root@node1 ~]# java -version           #查看Java版本
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)

3.1.2 修改配置文件

[root@node1 yum.repos.d]# vi /etc/elasticsearch/elasticsearch.yml
17行 集群名称
: elastic

23行 节点名称
: linux-node1

33行 工作目录
path.data: /data/es-data
path.logs: /var/log/elasticsearch/

43行 防止交换swap分区
bootstrap.memory_lock: true

54行 监听网络
network.host: 0.0.0.0

58行 端口
http.port: 9200

3.1.3 创建目录及开启服务

1 [root@node1 yum.repos.d]# mkdir -p /data/es-data
2 [root@node1 yum.repos.d]# chown -R elasticsearch:elasticsearch /data/es-data
3 [root@node1 yum.repos.d]# systemctl start elasticsearch.service 
4 [root@node1 yum.repos.d]# netstat -anpt | grep 92005 tcp6       0      0 :::9200                 :::*                    LISTEN      54134/java

3.1.4测试

elk 实现 elk项目实例子_elasticsearch_03

[root@node1 yum.repos.d]# curl -i -XGET 'http://20.0.0.20:9200/_count?pretty' -d '{
> "query": {
>     "match_all": {}
> }
> }'
HTTP/1.1 200 OK       #输出项
Content-Type: application/json; charset=UTF-8
Content-Length: 95

{
  "count" : 0,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  }
}

[root@node2 yum.repos.d]# curl -i -XGET 'http://20.0.0.10:9200/_count?pretty' -d '{
> "query": {
>     "match_all": {}
> }
> }'
HTTP/1.1 200 OK      #输出项
Content-Type: application/json; charset=UTF-8
Content-Length: 95

{
  "count" : 0,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  }
}

3.1.5安装Elasticsearch插件

[root@node1 yum.repos.d]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-hea

3.1.6 测试

elk 实现 elk项目实例子_elk 实现_04

3.1.7 符合查询

  • 提交请求
  • elk 实现 elk项目实例子_elastic_05

  • 查询
  • elk 实现 elk项目实例子_elasticsearch_06

  • 删除后查询

elk 实现 elk项目实例子_elastic_07


elk 实现 elk项目实例子_apache_08

3.1.8 elasticsearch集群部署

[root@node1 yum.repos.d]# vi /etc/elasticsearch/elasticsearch.yml
69行 单播列表自动发现机制
discovery.zen.ping.unicast.hosts: ["20.0.0.10", "20.0.0.20"] 
重启服务
[root@node1 yum.repos.d]# systemctl restart elasticsearch.service

3.1.9 测试

elk 实现 elk项目实例子_apache_09

3.1.10 node-01和node-02日志提示不让锁内存

[root@node1 elasticsearch]# less /var/log/elasticsearch/elastic.log 
 ...... 
 [2020-11-18 10:36:42,959][WARN ][bootstrap ] These can be adjusted by modifying /etc/security/limits.conf, for example: 
 # allow user 'elasticsearch' mlockall 
 elasticsearch soft memlock unlimited 
 elasticsearch hard memlock unlimited
 修改配置文件
[root@node1 elasticsearch]# vim /etc/security/limits.conf
//末尾插入
# allow user 'elasticsearch' mlockall
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited

[root@node1 elasticsearch]# systemctl stop elasticsearch.service
[root@node1 elasticsearch]# systemctl start elasticsearch.service

3.1.11 安装监控组件

[root@node1 elasticsearch]# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kop

3.1.12测试

elk 实现 elk项目实例子_linux_10

elk 实现 elk项目实例子_elastic_11

2,Logstash部署(apache上部署)

3.2.1 安装httpd并开启服务

[root@apache ~]# yum -y install httpd
[root@apache ~]# systemctl start httpd
[root@apache ~]# netstat -anpt | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      15620/httpd
[root@apache ~]# echo '<h1>this is web!</h1>' > /var/www/html/index.html
[root@apache ~]# curl http://localhost
<h1>this is web!</h1>

3.2.2 安装密钥及Logstash源

[root@ apache]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
[root@ apache]# vi /etc/yum.repos.d/logstash.repo
[logstash-2.1]
name=Logstash repository for 2.1.x packages
baseurl=http://packages.elastic.co/logstash/2.1/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enable=1
安装Logstash
[root@ apache]# yum install logstash -y

3.2.3 安装java环境,没有自带安装使用yum -y install java安装

[root@apache ~]# yum -y install java
 [root@apache ~]# java -version
 openjdk version "1.8.0_131"4 OpenJDK Runtime Environment (build 1.8.0_131-b12)
 OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)6 
 [root@apache bin]# ln -s /opt/logstash/bin/logstash /usr/local/bin/

3.2.4 logstash(apache)与elasticsearch(node)功能是否正常,做对接测试

[root@apache ~]# logstash -e 'input { stdin{} } output { stdout{} }'
Settings: Default filter workers: 2
Logstash startup completed
www.baidu.com      #随便输入几个网址
2020-11-20T07:49:14.720Z apache www.baidu.com
www.aliyu^H^H^Hn.com
2020-11-20T07:49:26.443Z apache www.aln.com
www.sougou.com
2020-11-20T07:49:35.297Z apache www.sougou.com
  • logstash命令选项解释:
  • -f:指定logstash的配置文件,根据配置文件配置logstash
  • -e:后面跟着字符串,该字符串可以被当做logstash的配置(如果是“ ”,则默认使用stdin做输入,stdout为输出)
  • -t:测试配置文件是否正确,然后退出
  • #输入采用标准输入,输出采用标准输出

3.2.5 使用rubydebug显示详细输出,codec为一种编解码器

[root@apache ~]#  logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug } }'
Settings: Default filter workers: 2
Logstash startup completed
www.baidu.com
{
       "message" => "www.baidu.com",
      "@version" => "1",
    "@timestamp" => "2020-11-20T08:49:51.921Z",
          "host" => "apache"
}

3.2.6 使用logstash将信息输出给elasticsearch

[root@apache bin]# logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["20.0.0.10:9200"] } }'
Settings: Default filter workers: 1
Logstash startup completed
abc123      #输入内容
tom456
123jerry

3.2.7 打开浏览器输入http://20.0.0.50:9200/_plugin/head/,查看索引信息,显示新的数据信息说明输出到elasticsearch成功

elk 实现 elk项目实例子_elk 实现_12

3.2.8 在apache主机中做对接配置,logstash收集系统日志

logstash配置文件主要由三部分组成:input、output、filter
[root@apache bin]# chmod o+r /var/log/messages   #允许其他用户访问
[root@apache bin]#  ll /var/log/messages
-rw----r--. 1 root root 250721 11月 18 16:40 /var/log/message

[root@apache ~]# vim /etc/logstash/conf.d/system.conf
input {                    #logstash输入:从/var/log/messages输入,类型为system,起始位                     置为从头开始
        file {
          path => "/var/log/messages"
          type => "system"
          start_position => "beginning"
        }
      }

output {                #logstash输出:输出给elasticsearch(以IP地址指定位置)
        elasticsearch {
        hosts => ["20.0.0.10:9200"]
        index => "system-%{+YYY.MM.dd}"
        }
      }

[root@apache ~]# systemctl restart logstash.service

3.2.9 打开浏览器输入http://20.0.0.50:9200/_plugin/dead/,查看索引信息,显示新的索引信息代表logstash搜集到的日志信息成功输出给elasticsearch

elk 实现 elk项目实例子_elk 实现_13

3,在node1主机安装kibana

3.3.1 在node1主机安装kibana

上传kibana-4.3.1-linux-x64.tar.gz到/root下
[root@node1 ~]# tar zxf kibana-4.3.1-linux-x64.tar.gz
[root@node1 ~]# mv kibana-4.3.1-linux-x64 kibana
[root@node1 ~]# mv kibana /usr/local/
[root@node1 ~]# cd /usr/local/kibana/
[root@node1 kibana]# vim config/kibana.yml

//2行 kibana打开的端口
server.port: 5601

//5行 kibana侦听的地址
server.host: "0.0.0.0"


//12行 和elasticsearch建立联系
elasticsearch.url: "http://20.0.0.10:9200"

//20行 在elasticsearch中添加.kibana索引
kibana.index: ".kibana"

[root@node1 kibana]# yum -y install screen

启动kibana
[root@node1 kibana]# /usr/local/kibana/bin/kibana
  log   [17:53:46.904] [info][status][plugin:kibana] Status changed from uninitialized to green - Ready
  log   [17:53:46.933] [info][status][plugin:elasticsearch] Status changed from uninitialized to yellow - Waiting for Elasticsearch
  log   [17:53:46.944] [info][status][plugin:kbn_vislib_vis_types] Status changed from uninitialized to green - Ready
  log   [17:53:46.951] [info][status][plugin:markdown_vis] Status changed from uninitialized to green - Ready
  log   [17:53:46.955] [info][status][plugin:metric_vis] Status changed from uninitialized to green - Ready
  log   [17:53:46.958] [info][status][plugin:spyModes] Status changed from uninitialized to green - Ready
  log   [17:53:46.966] [info][status][plugin:statusPage] Status changed from uninitialized to green - Ready
  log   [17:53:46.969] [info][status][plugin:table_vis] Status changed from uninitialized to green - Ready
  log   [17:53:46.978] [info][listening] Server running at http://0.0.0.0:5601
  log   [17:53:51.976] [info][status][plugin:elasticsearch] Status changed from yellow to yellow - No existing Kibana index found
  log   [17:53:54.602] [info][status][plugin:elasticsearch] Status changed from yellow to green - Kibana index ready

3.3.2 浏览器中登录20.0.0.10:5601,首次登录提示创建一个索引名字:填入system-*,即对接系统日志文件名

elk 实现 elk项目实例子_elasticsearch_14

3.3.3 同时也能看到刚才在kibana.yml中添加的在elasticsearch中添加.kibana索引配置生效了(用于验证kibana和elasticsearch是否成功连接)

elk 实现 elk项目实例子_elastic_15

3.3.4 添加logstash配置,收集apache服务的访问日志和错误日志中的数据

[root@apache ~]# cd /etc/logstash/conf.d/
[root@apache conf.d]# vim apache_log.conf
input {
        file {
          path => "/var/log/logstash/logstash.log"
          type => "access"
          start_position => "beginning"
        }

        file {
          path => "/var/log/logstash/logstash.err"
          type => "error"
          start_position => "beginning"
        }
   }
output {
        if [type] == "access" {
          elasticsearch {
            hosts => ["20.0.0.50:9200"]
            index => "apache_access-%{+YYY.MM.dd}"
          }
        }
        if [type] == "error" {
          elasticsearch {
            hosts => ["20.0.0.50:9200"]
            index => "apache_error-%{+YYY.MM.dd}"
          }
        }
    }

[root@apache conf.d]# logstash -f apache_log.conf    #指定logstash的配置文件,根据apache_log.conf配置logstash(-f可以不重启加载)

3.3.5 浏览器访问http://20.0.0.10:9200/_plugin/dead/,查看apache日志文件的索引是否建立成功

elk 实现 elk项目实例子_apache_16