您的位置:首页 > 其它

分布式实时日志分析平台ELK的部署与安装

2017-11-27 11:10 786 查看

一、概述

ELK 已经成为目前最流行的集中式日志解决方案,它主要是由Logstash、Elasticsearch、Kibana三个组件组成,来共同完成实时日志的收集,存储,展示等一站式的解决方案,本文主要讲ELK最常用的部署架构的组件安装,以及安装过程中的问题解决,后续文章会详细介绍ELK的常用架构,以及在生产环境中相关问题的解决方法。



二、安装部署

注:这里安装的ELK组件版本为5.6,需要JDK1.8及以上版本支持。

1、创建ELK用户

# groupadd elk //创建用户组
# useradd -g elk elk //创建用户,并指定用户组
# passwd elk //修改用户密码
# chown -R elk [ELK组件的安装目录] //给用户elk分配目录权限


2、从官网分别下载ELK组件

filebeat-5.6.3-linux-x86_64.tar.gz

logstash-5.6.3.tar.gz

elasticsearch-5.6.3.tar.gz

elasticsearch-head-master.zip

kibana-5.6.3-linux-x86_64.tar.gz

官网地址:https://www.elastic.co/cn/products

3、安装Elasticsearch

# tar -zxvf elasticsearch-5.6.3.tar.gz


3.1 启动

# ./bin/elasticsearch


3.2 Elasticsearch启动相关问题解决

(1)启动Elasticsearch5.6时报:can not run elasticsearch as root 错误!这里一般以rpm方式安装的话不存在该问题,如果安装的是tag.gz包,就会报该错误,解决办法,新建一个用户即可。

(2)启动Elasticsearch5.6报:process likely too low, increase to at least 错误,原因是新建的用户默认的可创建最大进程数被限制,解决方法:

切换到root用户

vi /etc/security/limits.conf

添加如下内容:

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

参数解释:

soft nproc: 单个用户可用的最大进程数量(软限制)

hard nproc: 单个用户可用的最大进程数量(硬限制)

soft nofile:单个用户可打开的最大文件描述符数量(软限制)

hard nofile:单个用户可打开的最大文件描述符数量(硬限制)

(3)Elasticsearch5.6启动后9200端口无法被访问,请修改config/elasticsearch.yml 文件,修改network.host项:network.host: localhost,或填写本机真实的IP

4、安装Elasticsearch-head-master

4.1 安装该插件后可在web页面查看Elasticsearch的相关信息,由于该插件运行的环境要依赖nodejs,所以需要先安装nodejs

(1)下载node-v8.9.0-linux-x64.tar.xz软件包

(2)$ xz -d node-v8.9.0-linux-x64.tar.xz //解压软件包

(3)$ tar -xvf node-v8.9.0-linux-x64.tar

(4)建立node与npm命令的软链接,这样可在任何地方访问该命令了

$ ln -s /home/project/elk/node-v8.9.0-linux-x64/bin/npm /usr/local/bin/npm

$ ln -s /home/project/elk/node-v8.9.0-linux-x64/bin/node /usr/local/bin/node

(5)验证是否安装成功:$ node -v

4.2 下载elasticsearch-head包:https://github.com/mobz/elasticsearch-head

(1)解压下载好的包

(2)$ cd elasticsearch-head-master/

(3)$ npm install -g cnpm --registry=https://registry.npm.taobao.org //安装依赖包

(4)安装好后启动该插件,$ npm run start

(5)该插件默认端口为9100,打开后发现无法连接到Elasticsearch,在Elasticsearch版本5以上需要在Elasticsearch配置文件中加如下参数即可:

http.cors.enabled: true

http.cors.allow-origin: "*"



5、安装Logstash

# tar -zxvf logstash-5.6.3.tar.gz


5.1 Logstash相关命令

(1)测试启动,看配置文件是否正确:bin/logstash -f first-pipeline.conf --config.test_and_exit

(2)以config.reload.automatic方式启动,这样在修改配置文件后无需重新启动,它会自动加载:bin/logstash -f first-pipeline.conf --config.reload.automatic

(3)列出所有已安装的插件:bin/logstash-plugin list

(4)安装外部的插件:bin/logstash-plugin install [插件名称]

(5)更新所有插件:bin/logstash-plugin update

(6)更新指定插件:bin/logstash-plugin update logstash-output-kafka

(7)删除指定插件:bin/logstash-plugin remove logstash-output-kafka

6、安装Filebeat

# tar -zxvf filebeat-5.6.3-linux-x86_64.tar.gz

(1)启动:./filebeat -e -c filebeat.yml -d "publish"

(2)Filebeat的输出方向为Elasticsearch,默认启动Filebeat加载的ES索引模板是 filebeat.template.json,如:

output.elasticsearch: hosts: ["localhost:9200"] template.name: "filebeat" template.path: "filebeat.template.json" template.overwrite: false

默认情况下,如果一个模板已经存在于索引中,那么它就不会被覆盖。要覆盖现有的模板,需要设置

template.overwrite: true

如:Filebeat的输出方向为Logstash,则需手动指定加载的ES索引模板,手动载入ES模板命令:

url -XPUT 'http://localhost:9200/_template/filebeat' -d@/etc/filebeat/filebeat.template.json

删除索引模板:curl -XDELETE 'http://localhost:9200/filebeat-*'

(3)启用已经构建好的模块,-modules项后可填写多个模块,第一次启动需要设置-setup,之后无需-setup

./filebeat -e -modules=system,nginx,mysql -setup

注:命令参数解释

-E <setting>=<value> //覆盖特定的配置设置,如:

./filebeat -c filebeat.yml -E name=mybeat

-c <file> //指定配置文件的路径

详细参数请看:https://www.elastic.co/guide/en/beats/filebeat/current/command-line-options.html

(4)使Filebeat重新从指定日志文件的开头读取数据

先停掉Filebeat

# rm data/registry

7、安装Kibana

# tar kibana-5.6.3-linux-x86_64.tar.gz


7.1 问题解决

启动kibana后在浏览器中无法访问,解决方法:修改kibana.yml文件,修改server.host值 为 "localhost"

7.2 查询技巧:

(1)要精确查询字符串请使用双引号,如查询"hello, how are you"

(2)* 表示匹配0到多个字符,如 hello*

(3)? 表示匹配单个字符,如h?o

(4)+ 表示搜索结果中必须包含此项,- 表示搜索中不包含此项

(5)运算符AND、OR、NOT表示与或非,注意必须大写,如 hello AND you,表示查询结果中必须同时包含hello与you

(6)@version:1 表示查询@version字段值为1的结果项

(7)@version:[1 TO 10] 表示查询@version的范围在1到10的结果项

(8)转义特殊字符 + – && || ! ( ) { } [ ] ^ ” ~ * ? : \ 转义特殊字符只需在字符前加上符号\

8、配置

8.1 配置Logstash

新建logstash.conf配置文件,内容为:

input {
beats {
port => 5044 //配置启动端口为5044

}
}

output {
elasticsearch { //配置输出目的地为elasticsearch
hosts => "localhost:9200"
}
}


8.2 配置Filebeat

新建filebeat-test.yml,内容为:

filebeat.prospectors:
-
paths:
- /home/project/elk/logs/test.log //日志路径
input_type: log
multiline: //日志多行内容合并
pattern: '^\['
negate: true
match: after
document_type: test //文档类型,可用于索引的构建

output:
logstash: //输出目的地为logstash
hosts: ["localhost:5044"]


9、运行

分别启动:Elasticsearch -> Logstash -> Filebeat -> Kibana



三、总结

本文主要介绍了ELK相关组件的安装,和安装过程中相关问题的解决,以及各组件的使用技巧,这里建议多看看Kibana中的查询技巧,可以在生产环境根据日志来快速定位问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息