Mac上搭建ELK

转载自个人我的博客:http://blog.ywheel.cn/post/2017/03/04/setup_elk_on_mac/html

最近的项目须要对文本数据各字段进行快速检索、组合查询、模糊查询,在架构选择上选择了Elasticsearch做为支撑这些功能的存储和搜索引擎。其余的不说了,恰好个人第一台mac到了,直接搞起。java

什么是ELK

日志分析平台能够有多种技术架构的选型,但通过了多年的演变,如今比较流行的应该就是ELK了。 ELK三个字母的意义以下:程序员

  1. Elasticsearch
  2. Logstash
  3. Kibana

架构图下图:web

ELK架构图

图中的Shipper和Indexer均可以是Logstash, Broker通常为Redis,也能够是kafka等。而Search & Storage则主要是Elasticsearch了,一方面接收上游index好的文档,另外一方面提供API支持对内容的检索。而kibana则是一个web interface, 能够提供简单易用的界面让用户方便的写出搜索的表达式来访问Elasticsearch.redis

对于这三部分都有不少深刻的点,之后有机会要深刻学习和记录。sql

使用brew安装

既然使用了mac,那么使用brew安装程序则是一个最简单不过的方式了。json

首先安装Elasticsearch,直接输入如下命令便可:xcode

brew install elasticsearch
  • 1

但可能遇到问题,好比要求Java的版本是1.8(我安装的Elasticsearch的按本是5.2.2),这里面可能涉及到还要安装brew cast用来安装java8, 而后又提示还有其余依赖(后悔没记录下来。。。),而须要xcode-command-tool, 折腾了很多时间。浏览器

安装完成后,能够查看elasticsearch的版本:安全

$ elasticsearch --version Version: 5.2.2, Build: f9d9b74/2017-02-24T17:26:45.835Z, JVM: 1.8.0_121
  • 1
  • 2

启动和中止elasticsearch也很简单:

brew services start elasticsearch brew services stop elasticsearch
  • 1
  • 2

浏览器访问http://localhost:9200能够看到Elasticsearch的信息:

{
name: "bWXgrRX", cluster_name: "elasticsearch_ywheel", cluster_uuid: "m99a1gFWQzKECuwnBfnTug", version: { number: "5.2.2", build_hash: "f9d9b74", build_date: "2017-02-24T17:26:45.835Z", build_snapshot: false, lucene_version: "6.4.1" }, tagline: "You Know, for Search" }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

接着安装logstash:

brew install logstash
  • 1

安装好后查看版本:

$ logstash --version logstash 5.2.2
  • 1
  • 2

浏览器访问http://localhost:9600能够看到以下信息:

{
host: "ywheeldeMacBook-Pro.local", version: "5.2.2", http_address: "127.0.0.1:9600", id: "70b78f4a-fe0f-4187-bf71-fe1f60b74e0a", name: "ywheeldeMacBook-Pro.local", build_date: "2017-02-24T17:46:55Z", build_sha: "57984d20eb28b0df40a59077c600ec1a399d46f5", build_snapshot: false }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Kibana不须要经过brew安装,直接下载压缩包后,解压后执行./kibana便可。不过我仍是在/usr/local/bin/下建立了kibanakibana-plugin的软链接, elasticsearch,elasticsearch-plugin,logstashlogstash-plugin都在这个目录下,之后安装插件的话,还都须要用上这些*-plugin.

Kibana安装完成后,须要在config/kibana.yml文件中,确认elasticsearch.url: "http://localhost:9200"

测试写入和查询

写入Elasticsearch能够很简单,其自己就提供了RESTFul的API接口,参考https://www.elastic.co/guide/en/kibana/3.0/import-some-data.html ,经过如下命令建立shakespeare index:

curl -XPUT http://localhost:9200/shakespeare -d ' { "mappings" : { "_default_" : { "properties" : { "speaker" : {"type": "string", "index" : "not_analyzed" }, "play_name" : {"type": "string", "index" : "not_analyzed" }, "line_id" : { "type" : "integer" }, "speech_number" : { "type" : "integer" } } } } } ';
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

经过如下命令将数据写入Elasticsearch:

curl -XPUT localhost:9200/_bulk --data-binary @shakespeare.json
  • 1

数据写入后,到kibana目录运行./kibana,启动后访问:http://localhost:5601/ , 看到kibana界面后会提示”Configure an index pattern”。, 以下图:

kibana 1

刚才在写入数据的时候已经建立了shakespeare index, 且不是按照时间分布的日志文件(shakespeare只有一个json文件),所以,取消勾选Index contains time-based envents,输入shakespeare后,就能看到create按钮了。

kibana 2

点击kibana的Discover页面,输入WESTMORELAND查询,能够看到有110个结果:

kibana 3

监控和安全

在Elasticsearch 5.x的时代,监控和管理由X-Pack统一完成,包含:

  1. 安全:用户权限管理
  2. 告警:自动告警
  3. 监控:监控Elasticsearch集群的状态
  4. 报告:发送报告、导出数据
  5. 图表:可视化数据

在安装X-Pack以前,须要中止Kibana和Elasticsearch:

elasticsearch-plugin install x-pack kibana-plugin install x-pack
  • 1
  • 2

安装完成后,启动elasticsearch和kibana,访问kibana时发现须要登陆了, 默认用户名和密码是elastic/changeme。

kibana login

后续能够在Management面板中进行用户和角色的配置,也能够看到新增了Reporting。

kibana management

在Monitoring页面中能够看到Elasticsearch和Kibana的状态,点击Indices还能够看到具体索引的状态。

kibana monitoring 1

kibana monitoring 1

告警功能和报表功能后续再进行详细研究。以前在A家的时候,记得有个基于日志的告警功能:当service的日志中出现了ERROR或FATAL,能够自动触发告警。有了X-Pack后,这个功能应该也是能够经过ELK来实现的啦。

经过访问http://localhost:9200/_cat/indices?v查看Elasticsearch的Index, 能够发现几个新的与监控有关的index:

health status index                           uuid                   pri rep docs.count docs.deleted store.size pri.store.size yellow open .monitoring-es-2-2017.03.04 COZvO_dlSkqdEtntrZrzFA 1 1 10240 154 4.5mb 4.5mb green open .security XEeHRF5NT0ud2jpxOzsoHw 1 0 1 0 2.8kb 2.8kb yellow open .kibana p-cJGBCXQySNGR0924jRdQ 1 1 2 1 9.8kb 9.8kb yellow open .monitoring-data-2 QZt0hpTISUO_58pWoG5Hyw 1 1 3 0 6.9kb 6.9kb yellow open .monitoring-kibana-2-2017.03.04 nLHKuL1KTiCE2lsWz8tdkA 1 1 849 0 245kb 245kb yellow open shakespeare zPCLp4KmTkiu7m4tYcA_Iw 5 1 111396 0 28.1mb 28.1mb
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

使用Logstash导入博文

在上面的操做中,直接使用了elasticsearch的API接口来进行数据的导入,而使用logstash也可以很方便的写入elasticsearch。 首先得生成一个logstash的conf文件,好比我想创建个人博客的索引,在个人家目录下建立了my_blog.conf文件:

input{
    file{
        path => ["/Users/ywheel/my_blog/content/about.md"] } } output{ elasticsearch{ hosts => ["localhost:9200"] index => "my_blog" user => "elastic" password => "changeme" } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

注意拜X-Pack所赐,这配置文件里面对elasticsearch须要用户名和密码。而后敲入logstash -f my_blog.conf来执行,但却一直不成功。后来翻logstash的文档 https://www.elastic.co/guide/en/logstash/current/pipeline.html ,里面写了一句这样的话:

Inputs

You use inputs to get data into Logstash. Some of the more commonly-used inputs are: - file: reads from a file on the filesystem, much like the UNIX command tail -0F - syslog: listens on the well-known port 514 for syslog messages and parses according to the RFC3164 format - redis: reads from a redis server, using both redis channels and redis lists. Redis is often used as a "broker" in a centralized Logstash installation, which queues Logstash events from remote Logstash "shippers". - beats: processes events sent by Filebeat.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

file这个input至关于使用tail来获取文件中的数据的啊, 个人about.md压根就没有变化,所以也没有内容被写入了Elasticsearch。因而,我把conf改为了这样:

input{
    file{
        path => ["/Users/ywheel/test.md"] } } output{ elasticsearch{ hosts => ["localhost:9200"] index => "my_blog" user => "elastic" password => "changeme" } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

运行logstash -f my_blog_conf后,再运行cat /Users/ywheel/my_blog/content/about.md > /Users/ywheel/test.md, 而后发现数据写入了Elasticsearch, index也多了一个my_blog。到Kibana中添加my_blog这个index pattern后,就能够在Discover进行搜索了。好比我搜索“程序员”:

kibana search

看来中文分词得改进一下,不过如今也已经很酷了! 之后能够对整个博客进行全文检索了~~

相关文章
相关标签/搜索