ELK日志系统:Elasticsearch + Logstash + Kibana 搭建教程

时间:2022-04-22
本文章向大家介绍ELK日志系统:Elasticsearch + Logstash + Kibana 搭建教程,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

环境:OS X 10.10.5 + JDK 1.8

步骤:

一、下载ELK的三大组件

Elasticsearch下载地址: https://www.elastic.co/downloads/elasticsearch (目前最新版本:2.1.1)

Logstash下载地址: https://www.elastic.co/downloads/logstash (目前最新版本:2.1.1)

Kibana下载地址: https://www.elastic.co/downloads/kibana (目前最新版本:4.3.1)

下载后将其解压到某个目录即可,本文中的解压目录为:

~/app/elasticsearch-2.1.1

~/app/logstash-2.1.1

~/app/kibana-4.3.1-darwin-x64

注:这3个组件相互之间的关系及作用如下:

Logstash(收集服务器上的日志文件) --》然后保存到 ElasticSearch(搜索引擎) --》Kibana提供友好的web界面(从ElasticSearch读取数据进行展示)

二、启动elasticsearch

2.1

进入elasticsearch目录bin

./elasticsearch

顺利的话,启动成功后,在浏览器里输入http://localhost:9200/ 应该能看到类似下面的输出:

 1 {
 2   "name" : "Atalanta",
 3   "cluster_name" : "elasticsearch",
 4   "version" : {
 5     "number" : "2.1.1",
 6     "build_hash" : "40e2c53a6b6c2972b3d13846e450e66f4375bd71",
 7     "build_timestamp" : "2015-12-15T13:05:55Z",
 8     "build_snapshot" : false,
 9     "lucene_version" : "5.3.1"
10   },
11   "tagline" : "You Know, for Search"
12 }

2.2 安装kopf插件

先按Ctrl+C停止elasticsearch,接下来准备安装插件,elasticsearch有大量插件资源,用于增加其功能,bin目录下,输入

./plugin list 可以查看当前安装的插件列表,我们刚刚全新安装,输出的是一个空列表,继续输入

./plugin install lmenezes/elasticsearch-kopf

将会联网安装kopf插件,安装完成后,再次用./plugin list确认下:

Installed plugins in /Users/yjmyzz/app/elasticsearch-2.1.1/plugins:
    - .DS_Store
    - kopf

如果输出上述类似,表明kopf安装成功。

然后重启elasticsearch,浏览器里输入http://localhost:9200/_plugin/kopf,将会看到类似下面的界面,可以很直观的看到elasticsearch的一些运行状况

以上操作都ok后,建议Ctrl+C关掉,改用nohup ./elasticsearch & 将其做为后台进程运行,以免退出。

三、logstash的启动与配置

3.1 新建索引配置文件

~/app/logstash-2.1.1/bin 下

mkdir conf

vi conf/logstash-indexer.conf

内容如下:

 1 input {
 2  file {
 3    path => ["/var/opt/log/a.log","/var/opt/log/b.log"]
 4  }
 5 }
 6 
 7 output {
 8   elasticsearch { hosts => ["localhost:9200"] }
 9   stdout { codec => rubydebug }
10 }

上面几个步骤的意思就是创建一个名为logstash-indexer.conf的配置文件,input{file{...}}部分指定的是日志文件的位置(可以多个文件),一般来说就是应用程序log4j输出的日志文件。output部分则是表示将日志文件的内容保存到elasticsearch,这里hosts对应的是一个数组,可以设置多个elasticsearch主机,相当于一份日志文件的内容,可以保存到多个elasticsearch中。

至于第9行的stdout,则表示终端的标准输出,方便部署时验证是否正常运行,验证通过后,可以去掉。

3.2 启动

继续保持在logstash的bin目录下,输入

./logstash -f conf/logstash-indexer.conf

稍等片刻,如果看到Logstash startup completed,则表示启动成功。然后另开一个终端窗口,随便找个文本编辑工具(比如:vi),向/var/opt/log/a.log里写点东西,比如:hello world之类,然后保存。观察logstash的终端运行窗口,是否有东西输出,如果有以下类似输出:

1 {
2        "message" => "hello world",
3       "@version" => "1",
4     "@timestamp" => "2016-01-08T14:35:16.834Z",
5           "host" => "yangjunmingdeMacBook-Pro.local",
6           "path" => "/var/opt/log/a.log"
7 }

说明logstash工作正常,此时浏览http://localhost:9200/_search?pretty 也应该能看到一堆输出,表明elasticsearch接收到logstash的数据了。

四、kibana的配置及启动

4.1 修改配置文件

~/app/kibana-4.3.1-darwin-x64/config 下有一个配置文件kibana.yml,大概在12行的位置,改成下面这样:

1 # The Elasticsearch instance to use for all your queries.
2 elasticsearch.url: "http://localhost:9200"

即:指定elasticsearch的访问位置

4.2 启动

~/app/kibana-4.3.1-darwin-x64/bin 下,输入:

./kibana

注:如果启动不成功,请检查版本是否为4.3.1,kibana必须与elasticsearch的版本匹配,一般说来,都从官网下载最新版即可。

启动完成后,在浏览器里输入http://localhost:5601/ 即可看到kibana界面,首次运行,会提示创建index,直接点击Create按钮即可。

然后,就能看到类似下面的界面了:

参考文章:

http://baidu.blog.51cto.com/71938/1676798?utm_source=tuicool&utm_medium=referral

https://www.elastic.co/products

http://www.cnblogs.com/yjf512/p/4199105.html

http://kibana.logstash.es/content/