ELK taught you how to build real-time log analysis platform

This article is mainly taught you how to build a real-time log analysis ELK platform, then, ELK in the end what is it?

ELK is the first letter of the abbreviation of three open source projects, three projects are: Elasticsearch, Logstash and Kibana.

  • Elasticsearch is a search and analysis engine .
  • Logstash a server-side data processing pipeline , the data can be simultaneously acquired from multiple sources, data conversion, and then transmits the data to the repository, such as a Elasticsearch the like.
  • Kibana allows users Elasticsearch the use of graphics and visual data charts .

ELK Stack

The core Elasticsearch is the search engine, users began to use it for logging use cases, and hope to be able to easily log collection and visualization. In view of this, Elastic introduces powerful and flexible collection pipe Logstash visualization tools Kibana.

ELK log system data flow diagram is as follows:

ELK log system data flow diagram

After a brief understanding ELK is a what, let us together build a hands-ELK real-time log analysis platform, first install Elasticsearch.

Note: ELK environment to build version is critical, the proposed unified version and avoid mistakes no place to start , I choose here is version 7.1.0.

Introduction and ElasticSearch

ElasticSearch introduction and mounted on an article already mentioned, will not be repeated here, and you can click on the link below to view:

Full-text search engine Elasticsearch entry: Cluster Setup

If you already know and installed Elasticsearch, then follow me marching to the next step: to understand and install Kibana.

Introduction and Kibana

This part explains how to download and install Kibana, and how to install Kibana plug, and it will be a brief introduction for Kibana interface.

首先让我们来看下 Kibana 究竟是何物?

什么是 Kibana?

Kibana 是为 Elasticsearch 设计的开源分析和可视化平台,你可以使用 Kibana 来搜索,查看存储在 Elasticsearch 索引中的数据并与之交互,你可以很容易实现高级的数据分析和可视化,以图标的形式展现出来。

在简单了解了 Kibana 后,让我们来到 Kibana 的下载网站 https://www.elastic.co/cn/downloads/kibana ,目前使用的是 Windows 系统,因此下载 Windows 版本的 Kibana 下载包 kibana-7.1.0-windows-x86_64.zip

运行 Kibana

下载完成后在本地解压,如果需要对 Kibana 做一些定制,可以在 config 目录下 编辑 kibana.yml 文件,在运行 Kibana 之前需要先运行 ElasticSearch(以下简称 ES),因为 Kibana 是基于 ES 运行的,现在进入 bin 目录下打开 kibana.bat 就可以运行 Kibana 了,我们现在打开浏览器,Kibana 是运行在 5601 端口上的,因此打开 http://localhost:5601,打开后会出现如下页面:

导入样例数据,查看 Dashboard

进入首页后会提示我们可以添加一些测试数据,ES 在 Kibana 开箱即用的版本中,已经为我们准备了三种样例数据,电商网站的订单,航空公司的飞行记录以及 WEB 网站的日志,我们可以点击 Add data,把他们添加进来,添加完成后,我们可以打开 Dashboards 界面,就可以看到系统已经为我们创建了数据的 Dashboard。

Dashboards

第一个是电商的利润报表,我们可以打开来看一下:

在 Dashboard 中,我们可以将多套可视结果整合至单一页面内,而后提供搜索查询或者点击可视结果内的某元素指定过滤条件,从而实现结果过滤,Dashboard 能够帮助我们更全面地了解总体日志内容,并将各可视结果同日志关联起来,以上就是 Kibana 的 Dashboard 功能。

Dev Tools

接下来介绍 Kibana 里面非常有用的工具 Dev Tools,其实就是可以很方便地在 Kibana 中执行 ES 中的一些 API,比如我们上文讲到的检测有哪些节点在运行: GET /_cat/nodes?v,这样我们就能在 Kibana 中运行 ES 命令了。

另外,Kibana 的 Dev Tools 还有许多的快捷菜单操作,比如 Ctrl + / 可以查看 API 帮助文档,其他的大家可以去自行摸索。

安装与查看插件

Kibana 可以通过插件的方式来提供一些 Kibana 中的特定应用或者增强图表展示的功能,Kibana 安装插件和 ES 非常相似。

输入 kibana-plugin install kibana-plugin install https://github.com/sivasamyk/logtrail/releases/download/v0.1.31/logtrail-7.1.0-0.1.31.zip 就可以下载 LogTrail 插件了。

在 cmd 中输入 kibana-plugin list 可以查看本机已安装的 Kibana 插件。

如果想移除插件可以使用 kibana-plugin remove logtrail 命令来进行移除插件。

到此为止,我们就下载并安装完成 Kibana,并对 Kibana 主要功能进行简单介绍,还介绍了 Dev Tools,大家可以自己在本地进行实践操作下。

目前就差 ELK 三兄弟的最后一个:Logstash,让我们一起学习下。

Logstash 介绍与安装

这部分主要是下载并安装 Logstash,并通过 Logstash 将测试数据集导入到 ES 中。

话不多说,首先让我们来了解下 Logstash 是个啥?

什么是 Logstash?

Logstash 是开源的服务器端数据处理管道,能够同时从多个来源采集数据,转换数据,然后将数据发送到您最喜欢的存储库中。

Logstash 能够动态地采集、转换和传输数据,不受格式或复杂度的影响。利用 Grok 从非结构化数据中派生出结构,从 IP 地址解码出地理坐标,匿名化或排除敏感字段,并简化整体处理过程。

数据往往以各种各样的形式,或分散或集中地存在于很多系统中。Logstash 支持各种输入选择 ,可以在同一时间从众多常用来源捕捉事件,能够以连续的流式传输方式,轻松地从您的日志、指标、Web 应用、数据存储以及各种 AWS 服务采集数据。

再了解过后,让我们去下载安装 Logstash。

安装 Logstash

还是来到 Logstash 的官网,进入到下载页面 https://www.elastic.co/cn/downloads/logstash,下载的时候注意要和 ES 和 Kibana 的版本相同,这里下载的为 7.1.0 版本 logstash-7.1.0.zip

下载后进行解压,也可以进入 conf 目录下修改 logstash.conf 进行配置,运行的时候可以通过指定配置文件 logstash -f logstash.conf 就可以执行数据的插入和转换的工作。

再安装完成之后,让我们来使用 Logstash 往 ES 中导入数据。

用 Logstash 导入 ES

下面我们来导入测试数据集,首先修改 logstash.conf 文件,内容为:

input {
  file {
    path => ["D:/SoftWare/logstash-7.1.0/csv/movies.csv"]
    start_position => "beginning"
    sincedb_path => "D:/SoftWare/logstash-7.1.0/csv/null"
  }
}
filter {
  csv {
    separator => ","
    columns => ["id","content","genre"]
  }

  mutate {
    split => { "genre" => "|" }
    remove_field => ["path", "host","@timestamp","message"]
  }

  mutate {

    split => ["content", "("]
    add_field => { "title" => "%{[content][0]}"}
    add_field => { "year" => "%{[content][1]}"}
  }

  mutate {
    convert => {
      "year" => "integer"
    }
    strip => ["title"]
    remove_field => ["path", "host","@timestamp","message","content"]
  }

}
output {
   elasticsearch {
     hosts => "http://localhost:9200"
     index => "movies"
     document_id => "%{id}"
   }
  stdout {}
}

测试数据集来自 Movielens :https://grouplens.org/datasets/movielens/,大家可以前往下载。配置文件中的 path 根据自己下载的测试文件路径去修改。另外,配置文件的逻辑将在以后的文章中进行讲解。

Now to execute the command logstash -f logstash.confto import the data ES. When you see the data printed to the console, the data being written to the ES.

So far, we have successfully installed Logstash, and the test data set by Logstash write ES, ELK while our real-time log analysis platform to build complete.

supplement

After writing ES by Logstash the test data set, the junior partner will find movies index status is yellow, do not worry, yellow representatives a copy of the fragment is not assigned.

Since this machine only the start of a node, and the index movies provided a copy of a master slice slice, primary and secondary slice is not allocated on one node.

Solution: modify the index setting, the copy of the replica set to 0, or to add a cluster node, the status will change to green.

to sum up

This paper learned what ELK, then practice with everyone to build a platform ELK log analysis, if there are any problems in the building process, the welcome message exchanges and discussions.

If ELK installation package or set of test data download speed is slow, you can Wupei Xuan in [public] No. reply elk information can be obtained.

references

https://www.elastic.co/guide/en/kibana/7.1/index.html

https://www.elastic.co/guide/en/logstash/7.1/index.html

Elasticsearch core technology and combat

Guess you like

Origin www.cnblogs.com/wupeixuan/p/12370159.html