Linux日志分析ELK环境搭建

场景:ELK作为一个日志收集和检索系统,感觉功能还是相当的强大的。 

ELK是啥, 其实是是三个组件的缩写, 分别是elasticsearch, logstash, kibana. ELK平台可以用于实现日志收集、日志搜索和日志分析. 当然, 如果你有花不完的money, 完全可以使用商业版的Splunk, Orz...

ELK分别是什么

1). ELK现在都是属于elastic公司的产品, 产品主页.

1.elasticsearch(es): 它是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
2.logstash: 简单说是一个日志收集工具, 可以定义从哪里获取数据, 并且可以简单处理数据, 最后可以定义将数据输出到哪里, 一般输出到es. 
3.kibana: 其实就是一个数据展示界面, 并且可以分析数据, 例如从es中读取数据进行展示分析. es自己也带有简单的展示dashboard: http://127.0.0.1:9200/_plugin/head/, 当然 前提是你安装了head插件, 这个下面再说.

2). 简单流程

下面图简单的展示了三组件之间的协作,

1

总的来说分成四大部分, 第一, 应用产生数据, 第二, logstash从应用收集数据, 第三, 将数据放入es, 第四, 使用kibana进行数据展示以及分析.

1 准备

1.1 环境检测

本文介绍安装 logstash 2.2.0 和 elasticsearch 2.2.0,操作系统环境版本是

安装 JDK 是必须的,一般操作系统都会有,只是版本的问题,这里在安装的过程中发现ELK大部分都要求1.8以上的JDK,所以果断换1.8版本的JDK省事些。

 

而 Kibana 只是一个用纯 JavaScript 写的前端 UI。一定要注意 Kibana 的版本,它会要求 ES 的版本。下表展示了kibana和Elasticsearch之间的对应关系

 

1.2 软件下载

注意:这三个软件之间的版本如果选择不对,中间会出现很多想不到的问题。我这里的总结是elasticsearch和logstash最好保持版本一直,kibana和es的版本如上表所示的对应。

 在网站中搜索下面三个软件的压缩包。https://www.elastic.co/

这里我都是选择Linux 64位的压缩包,然后将这三个文件通过FTP工具上传到自己建立的目录下面,这里以Etest为例。

ps:ELK三个软件的安装基本就是解压,然后执行就可以运行了,所以安装的目录完全可以自己选择。

logstash all plugins 2.2.0

elasticsearch 2.2.0

kibana 4.4.1

 

1.3 解压缩软件

#将软件解压到/usr/local目录
##tar -xzvf elasticsearch-5.0.1.tar.gz -C /usr/local

#首先切换到软件下载目录,然后将三个软件直接解压到Etset目录
tar -xzvf elasticsearch-2.2.0.tar.gz

tar -xzvf logstash-all-plugins-2.2.0.tar.gz

tar -xzvf kibana-4.4.1-linux-x64.tar.gz

解压后文件如下图所示:

2 elasticsearch

2.1 启动elasticsearch

ps:启动时候不能以root用户启动,否则会报错。

cd /home/ipsp/Etest/elasticsearch-2.2.0

#启动命令
bin/elasticsearch

正常启动情况的状态如下图所示:

注意:为了能够通过浏览器访问elasticsearch,我们需要配置elasticsearch的配置文件,主要是修改network.host和http.port

vim config/elasticsearch.yml

#修改配置文件内容如下:
network.host: 0.0.0.0

 http.port: 9200

之所以修改端口,是因为ES 每次启动时,端口可能会变(端口被占用,ES 自己会改端口)~

如果还不能正常启动 ES,并提示端口被占用,就查看一下什么程序占用 9200 端口,kill 掉,重启 ES 就行。

我遇到的,Java 把 9200 端口占用了。

 通过上述配置在浏览器就能够访问ElasticSearch了,这里我使用的Chrome浏览器,用360浏览器每次打开都是下载文件,浏览器的问题。 

 

2.2 检测elasticsearch状态

 重新打开一个终端,用非root 用户登录, 输入指令来检测Elasticsearch的连通性:

curl -X GET http://localhost:9200

#需要先修改elasticsearch的配置文件,参考2.1,这样也能够检测
curl -X GET http://168.33.130.224:9200

状态如图所示:

 

2.3 插件安装

先查看你的 ES 都有哪些插件,这里我已经安装了Head插件,所以直接能看到。

ipsp@ibps:~/Etest/elasticsearch-2.2.0> bin/plugin list
Installed plugins in /home/ipsp/Etest/elasticsearch-2.2.0/plugins:
- head
ipsp@ibps:~/Etest/elasticsearch-2.2.0>

1、安装Head插件

ES 支持在线和本地安装 Head。本地安装时,从 Github 上下载 Head 插件,然后上传到你的 ES 服务器,比如,/home/ipsp/Etest/elasticsearch-2.2.0 目录。

ps:在线安装总是安装失败,这里就讲解下本地安装的过程。

1、本地安装

  • 下载插件elasticsearch-head-master
  • 执行命令安装
     bin/plugin install -v file:plugins/elasticsearch-head-master.zip
    [root@vcyber elasticsearch]# bin/plugin install -v file:plugins/elasticsearch-head-master.zip
    -> Installing from file:plugins/elasticsearch-head-master.zip...
    Trying file:plugins/elasticsearch-head-master.zip ...
    Downloading .........DONE
    Verifying file:plugins/elasticsearch-head-master.zip checksums if available ...
    NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
    - Plugin information:
    Name: head
    Description: head - A web front end for an elastic search cluster
    Site: true
    Version: master
    JVM: false
     
    Installed head into /usr/local/elasticsearch/elasticsearch/plugins/head    //安装成功的标志

可以通过命令来查看插件是否安装完成:

 bin/plugin list

注意:install 选项,ES 的 2.* 版本,都不带“-”横线选项,即“-install”。

注意:若是本地安装,而且,你把 Head 压缩包放到了 Elasticsearch/plugins 目录下,安装后,一定要将 Elasticsearch-head zip 压缩包删掉,否则启动 ES 时会报“不能初始化插件”错误。

  • 浏览器访问 http://your ip adress:9200/_plugin/head你就会看到如下界面:

关于head插件会在后面的部分进行介绍,未完待续

2、在线安装(只是参考)

[root@vcyber elasticsearch]# bin/plugin install mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
Downloading ...................................................................................................................................................................................................................................................................................................................................................................................................................................DONE
Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed head into /usr/local/elasticsearch/elasticsearch/plugins/head

2.4 问题汇总

下面有些问题是从别的地方摘抄的,可以借鉴,有的是自己遇到的。

1、java版本不对——更换JDK版本

[root@vcyber elasticsearch]# bin/elasticsearch
 
Exception in thread "main" java.lang.RuntimeException: Java version: Oracle Cooration 1.7.0_51 [Java HotSpot(TM) 64-Bit Server VM 24.51-b03] suffers from crical bug https://bugs.openjdk.java.net/browse/JDK-8024830 which can cause dataorruption.
 
Please upgrade the JVM, see http://www.elastic.co/guide/en/elasticsearch/referce/current/_installation.html for current recommendations.
 
If you absolutely cannot upgrade, please add -XX:-UseSuperWord to the JAVA_OPT environment variable.
 
Upgrading is preferred, this workaround will result in degraded performance.
 
        at org.elasticsearch.bootstrap.JVMCheck.check(JVMCheck.java:123)
 
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:283)
 
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:3
 
Refer to the log for complete error details.

 

大意是:Java 运行时异常,本机版本 JDK 有 bug……让升级 JVM。如果实在不能升级,就向 JAVA_OPT 环境变量添加 -XX:-UseSuperWord 选项。

 

2、elasticsearch 不能用 root 用户运行——切换到非root用户运行

在root用户解压完elasticsearch后出现如下错误:

[root@localhost cs408]# /usr/local/elasticsearch-5.0.1/bin/elasticsearch
[2016-11-30T09:24:56,235][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:116) ~[elasticsearch-5.0.1.jar:5.0.1]
    at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:103) ~[elasticsearch-5.0.1.jar:5.0.1]
    at org.elasticsearch.cli.SettingCommand.execute(SettingCommand.java:54) ~[elasticsearch-5.0.1.jar:5.0.1]
    at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:96) ~[elasticsearch-5.0.1.jar:5.0.1]
    at org.elasticsearch.cli.Command.main(Command.java:62) ~[elasticsearch-5.0.1.jar:5.0.1]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:80) ~[elasticsearch-5.0.1.jar:5.0.1]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:73) ~[elasticsearch-5.0.1.jar:5.0.1]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
    at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:96) ~[elasticsearch-5.0.1.jar:5.0.1]
    at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:155) ~[elasticsearch-5.0.1.jar:5.0.1]
    at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:286) ~[elasticsearch-5.0.1.jar:5.0.1]
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:112) ~[elasticsearch-5.0.1.jar:5.0.1]
    ... 6 more
[root@localhost cs408]# vim /usr/local/elasticsearch-5.0.1/config/elasticsearch.yml

解决方案:

因为安全问题elasticsearch 不让用root用户直接运行,所以要创建新用户,这里以已有用户cs408为例:

第一步:liunx创建新用户  adduser  XXX  然后给创建的用户加密码 passwd XXX    输入两次密码。(已有可以忽略)

第二步:切换刚才创建的用户 su XXX  然后执行elasticsearch  会显示AccessDeniedException 权限不足。

第三步:给新建的XXX赋权限,chmod 777 *  这个不行,因为这个用户本身就没有权限,肯定自己不能给自己付权限。所以要用root用户登录赋予权限。

第四步:root给XXX赋权限,chown -R XXX /你的elasticsearch安装目录。

chown -R cs408 /usr/local/elasticsearch-5.0.1 

再次执行就能看到成功输出信息。

bin/elasticsearch

3 logstash

logstash 提供各种安装包,包括 tar.gz,ZIP,DEB 和 RPM。另外,又提供了一个包含所有插件的压缩包——logstash-all-plugins-2.2.0.tar.gz 。本文以它为例。解压后,配置 logstash,运行即可。

3.1 创建配置文件

logstash运行时需要使用配置文件,主要是用于定义内容的输入、输出、中间处理等。

创建配置文件所在目录,并编辑配置文件

cd /home/ipsp/Etest/logstash-2.2.0
vim
logstash_agent.conf

在配置文件logstash_agent.conf添加如下内容:

input {
    stdin{}
}

output {
  elasticsearch {
    action => "index"            # 在ES上操作index
    hosts  => "168.33.130.224:9200"   # ES地址
    index  => "logstash--%{+YYYY-MM}"             # 索引名
  }
}

上面的配置主要是在logstash命令段输入日志信息,然后将日志输出到elasticsearch中。

ps:注意这里的index选项,我命名的是logstash--%{+YYYY-MM},这个在后面的head插件和kibana中都会有所体现的。

3.2 启动

bin/logstash -f logstash_agent.conf 

看到如下的内容,便表示成功了

ipsp@ibps:~/Etest/logstash-2.2.0> bin/logstash -f logstash_agent.conf
Settings: Default pipeline workers: 40
Logstash startup completed

3.3 测试

 通过上面的配置,我们已经可以在logstash命令端输入信息,然后通过head插件进行查看了:

前提要保证elasticsearch和logstash都正常启动(需要先启动elasticsearch,再启动logstash,不然elasticsearch会报错),head插件也已经正确安装!

我们输入Hello world!

在head插件中的,我们可以看到如下信息:

在数据库浏览中,我们可以看到索引项——logstash--%{+YYYY-MM},这个是我们在配置logstash输出到elasticsearch的配置文件中定义的(logstash_agent.conf)

我在logstash命令端输入的字符串Hello world!在head的图形化界面能够查看到,其中还包含了其他一些默认的字段。

这里每输出一条信息,在head插件中都能够实时的看到,感觉很强大。

到此,elasticsearch和logstash之间的连通已经搭建成功。

3.4 插件

你可以查看 logstash 都安装了哪些插件

bin/plugin list

 

ipsp@ibps:~/Etest/logstash-2.2.0> bin/plugin list
logstash-codec-avro
logstash-codec-cef
logstash-codec-cloudfront
logstash-codec-cloudtrail
logstash-codec-collectd
logstash-codec-compress_spooler
logstash-codec-dots
logstash-codec-edn
logstash-codec-edn_lines
logstash-codec-es_bulk
logstash-codec-fluent
logstash-codec-graphite
logstash-codec-gzip_lines
logstash-codec-json
logstash-codec-json_lines
logstash-codec-line
logstash-codec-msgpack
logstash-codec-multiline
logstash-codec-netflow
logstash-codec-nmap
logstash-codec-oldlogstashjson
logstash-codec-plain
logstash-codec-rubydebug
logstash-codec-s3plain
logstash-codec-spool
logstash-filter-aggregate
logstash-filter-alter
logstash-filter-anonymize
logstash-filter-checksum
logstash-filter-cidr
logstash-filter-cipher
logstash-filter-clone
logstash-filter-collate
logstash-filter-csv
logstash-filter-date
logstash-filter-de_dot
logstash-filter-dns
.......................
.......................

 

4 kibana

4.1 启动

ipsp@ibps:~/Etest> cd kibana-4.4.1-linux-x64/
ipsp@ibps:~/Etest/kibana-4.4.1-linux-x64> bin/kibana

如果看到类似如下输出,就代表成功了:

ipsp@ibps:~/Etest/kibana-4.4.1-linux-x64> bin/kibana
log [16:59:39.891] [info][status][plugin:kibana] Status changed from uninitialized to green - Ready
log [16:59:39.942] [info][status][plugin:elasticsearch] Status changed from uninitialized to yellow - Waiting for Elasticsearch
log [16:59:39.961] [info][status][plugin:kbn_vislib_vis_types] Status changed from uninitialized to green - Ready
log [16:59:39.974] [info][status][plugin:markdown_vis] Status changed from uninitialized to green - Ready
log [16:59:39.983] [info][status][plugin:metric_vis] Status changed from uninitialized to green - Ready
log [16:59:39.987] [info][status][plugin:spyModes] Status changed from uninitialized to green - Ready
log [16:59:39.991] [info][status][plugin:statusPage] Status changed from uninitialized to green - Ready
log [16:59:39.995] [info][status][plugin:table_vis] Status changed from uninitialized to green - Ready
log [16:59:40.026] [info][listening] Server running at http://168.33.130.224:5601
log [16:59:40.040] [info][status][plugin:elasticsearch] Status changed from yellow to green - Kibana index ready

通过浏览器访问http://168.33.130.224:5601/就可以看到kibana界面了。

配置ES索引:

kibana第一次使用时,会要求创建index,只要按照默认值即可。

 

首次会提示没有索引。。。。

首先需要加上我们刚刚建立的索引index => "logstash--%{+YYYY-MM}" (这里请参考3.1中的logstash_agent.conf的配置), 点击setting->indices, 在这里我们可以Configure an index pattern, 也就是说可以配置 正则匹配的index,

可以看到默认的index是"logstash-*", 默认是从logstash导出的数据, 因为我们在logstash中配置的索引就是logstash开头的,所以这里我们保持默认不变.

下面还有一个Time-field name, 一般来说time都是一个必要的字段, 并且这个字段的类型是date类型! 不是string!!! 如果没有时间字段, 那么将上面的" Index contains time-based events" 取消就OK.

创建完索引是这样的:

点击菜单栏: Discover, 如果没有看到数据, 那么点击右边的时间, 改成Today, 现在你应该能看到数据了吧...反正我能看到:

ps:这里的时间是以Linux服务器中的时间为主。我这里点today看不到,点的This Week才看到:

在上面可以直接搜索,很方便。其他功能待开发。

4.2 问题

1、出现Configure an index pattern create是灰色的的问题

这个问题是配置文件的问题,修改logstash的配置文件,使其输出到elasticsearch,其中的index选项配置为kibana上的index,比如我是index => "logstash--%{+YYYY-MM}" ,这个时候重启logstash,kibana就保持默认的logstash-*索引既可,应该能够看到Create按钮了

posted @ 2016-11-30 10:06  CS408  阅读(13104)  评论(2编辑  收藏  举报