写给大忙人的CentOS 7下最新版(6.2.4)ELK+Filebeat+Log4j日志集成环境搭建完整指南 ELK最新版6.2.4学习笔记-Logstash和Filebeat解析

如今的公司因为绝大部分项目都采用分布式架构,很早就采用ELK了,只不过最近由于额外的工做须要,仔细的研究了分布式系统中,怎么样的日志规范和架构才是合理和可以有效提升问题排查效率的。通过仔细的分析和研究,肯定下面的架构应该是比较合理的之一(Filebeat也支持直接写到ES),若是能够的话,Filebeat也能够不直接连到Logstash,先写到kafka,而后从kafka写到logstash的kafka插件。关于ELK的各类架构以及优缺点,能够参考https://www.ibm.com/developerworks/cn/opensource/os-cn-elk-filebeat/index.html。html

 

首先,不论是不是分布式架构,都应该解决请求日志上下文关联的问题,这能够经过Log4j自带的NDC来实现。以Spring MVC为例,可使用HandlerInterceptor在HandlerInterceptor中设置NDC.push(SessionBeanUtil.getSessionKey(request).substring(0, 8) + "_" + path + "_" + formatter.format(new Date()));,在postHandle中NDC.pop();。java

注:最近在学习spring cloud,spring cloud sleuth也包含了MDC跨节点日志上下文跟踪ID的特性,和笔者的思路基本一致。node

其次,对于分布式请求,还要解决全局的请求日志上下文关联的问题,这须要依赖与具体的RPC框架来实现,以dubbo为例,能够经过filter在请求端和服务端设置。linux

第三,服务的调用链应该能够算作是日志框架的范畴,这能够经过zipkin集成来实现。git

在实现上,对于服务的调用链可使用单独的ES存储、也能够在一个大的ES集群中存储为一个index。github

从ELK 6.0开始,对于源是log4j的场景,再也不建议使用logstash-input-log4j,而是使用Filebeat做为应用端的日志代理,具体能够参考https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html。web

因此,在本文的搭建过程当中,笔者采用的也是Filebeat的方案。spring

环境说明与准备

Filebeat通常安装在产生日志的服务器,这里tomcat在windows 10 x64下,因此Filebeat也须要安装在windows下。chrome

CentOS 7.4 64位 ip 192.168.230.128,ELK安装在centos下,统一安装在/usr/local/app目录下。shell

版本为安装时的最新版本:

Elasticsearch 6.2.4

Kibana 6.2.4

Logstash 6.2.4

从https://www.elastic.co/cn/downloads下载并解压,以下:

由于ES主要经过restful api对外提供服务,因此通常安装ES时顺带安装elasticsearch-head,它提供了web控制台。elasticsearch-head经过源码的方式托管在git上,因此须要安装下git,同时elasticsearch-head是一个nodejs应用,因此还须要具备node。

因此在正式开始前,咱们须要确保centos下下列基础设施已经具有:

一、git已经安装。yum install git

二、若是但愿源码安装nodejs,确保gcc知足node的要求,或者直接使用已经编译好的版本, 笔者直接使用编译好的版本,可从https://nodejs.org/en/download/releases/选择具体的版本,基础运行环境最好不要使用最新版本,这里咱们使用v4.9。下载解压后便可用。

三、由于elk不能使用root运行,因此须要新建一个elk用户并设置环境变量。

groupadd elk

useradd -g elk elk

设置elk用户的环境变量:

su - elk

vim .bash_profile  增长以下:

export NODE_HOME=/usr/local/app/node-v4.9.1-linux-x64
PATH=$NODE_HOME/bin:$PATH:$HOME/.local/bin:$HOME/bin
export NODE_PATH=$NODE_HOME/lib/node_modules
export PATH
四、下载ELK套件自己并解压:

cd /usr/local/app

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.tar.gz

wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.4-linux-x86_64.tar.gz

wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.4.tar.gz

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.4-linux-x86_64.tar.gz

git clone https://github.com/elastic/elasticsearch-head.git 

六、由于ES和logstash都是java应用,因此确保安装了JDK 1.8,可使用centos自带的openjdk,不过生产环境下通常不建议使用openjdk,此处仅为了测试方便。

ELK环境搭建

由于使用的是vmware,总共内存才分配了1G,因此为了不OOM,对ES以及logstash内存均进行了调整,限制为256M。

ES安装

由于es须要非root用户运行,因此对于ES相关的全部操做均在elk用户下运行。

su - elk

更改ES配置:

[elk@elk1 ~]$ cd /usr/local/app/elasticsearch-6.2.4/config/
[elk@elk1 config]$ ll
总用量 16
-rw-rw----. 1 elk elk 2976 6月 1 16:22 elasticsearch.yml
-rw-rw----. 1 elk elk 2771 6月 2 13:54 jvm.options
-rw-rw----. 1 elk elk 5091 4月 13 04:33 log4j2.properties
[elk@elk1 config]$ pwd
/usr/local/app/elasticsearch-6.2.4/config

vim elasticsearch.yml

# 确保下列参数被正确设置:

cluster.name: logger   # ES集群的名字

node.name: node-1

path.data: /usr/local/app/elasticsearch-6.2.4/data

path.logs: /usr/local/app/elasticsearch-6.2.4/log

bootstrap.memory_lock: false   # 对于非专用ES,建议设置为false,默认为true
bootstrap.system_call_filter: false

network.host: 0.0.0.0  # 支持远程访问

http.port: 9200 # restful api访问接口

http.cors.enabled: true      #容许ES head跨域访问
http.cors.allow-origin: "*"   #容许ES head跨域访问

vim jvm.options # JVM参数在这个文件中设置,固然命令行也能够

-Xms256m
-Xmx256m

上述配置调整完成后,就能够启动ES了。

[elk@elk1 bin]$ nohup ./elasticsearch &

[elk@elk1 bin]$ ps axu | grep elas

windows下浏览器访问下http://192.168.230.128:9200/

这样,ES就安装好了。

ES head安装

须要注意的是,虽然ES head能够认为是es的插件,可是它不能放在$ES_HOME/plugins目录下,由于它并不符合ES插件的规范,不然ES启动会失败。

[elk@elk1 elasticsearch-head]$ pwd
/usr/local/app/elasticsearch-head
[elk@elk1 elasticsearch-head]$ npm config set registry https://registry.npm.taobao.org

npm install

Please report this full log at https://github.com/Medium/phantomjs

npm ERR! Darwin 15.0.0

npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"

npm ERR! node v4.4.3

npm ERR! npm  v3.10.9

npm ERR! code ELIFECYCLE

npm ERR! phantomjs-prebuilt@2.1.14 install: `node install.js`

npm ERR! Exit status 1

npm ERR! 

npm ERR! Failed at the phantomjs-prebuilt@2.1.14 install script 'node install.js

若是出现上述错误,则执行下列名称:

npm install phantomjs-prebuilt@2.1.14 --ignore-scripts

而后从新执行

npm install

这样ES head就安装好了。不要急着启动!!!

若是只是本机访问,下面的配置修改不是必须的。若是要其余机器访问,则须要修改,通常来讲,只要是服务器应用,都是经过远程访问的。

[elk@elk1 elasticsearch-head]$ vim Gruntfile.js

搜索server,在其options对象属性下增长一个hostname属性,值为"*",以下:

如今就能够启动es head了,以下:

 访问下http://192.168.230.128:9100/吧,以下:

这里状态为yellow的缘由在于默认状况下,Elasticsearch为每一个索引分配5个分片和1个副本(5个主分片,5个副本分片),也就是双节点模式,而咱们只有一个节点。

 

logstash安装

logstash和kibana可使用root用户来启动。

建立一个logstash配置文件,好比logstash-es.conf,配置从filebeat读取数据源,输出到es,为了简化起见,忽略过滤器(实际生产中,通常须要配置过滤器对日志进行规范化处理和分类)

cd /usr/local/app/logstash-6.2.4/config

[elk@elk1 config]$ vim logstash-es.conf

input {
  stdin { }
  beats {
    port => 5000
    ssl => false
  }
}
output {
    elasticsearch {
        action => "index"
        hosts => "127.0.0.1:9200"
        index  => "logstash-%{+YYYY-MM}"
    }
    stdout { codec=> rubydebug }
}

为了测试方便,同时开启控制台输入和输出。

[elk@elk1 config]$ vim jvm.options   #设置最多使用256m内存

-Xms256m
-Xmx256m

启动logstash。

随便输入个字符串测试下,如上,看下http://192.168.230.128:9100/

这样logstash对于写入es和从控制台输入的配置就正确了,filebeat还须要等咱们后面验证。咱们先完成Kibana的安装与配置。

Kibana安装

kibana也是个nodejs应用。首先来修改kibana的配置:

[elk@elk1 app]$ cd kibana-6.2.4-linux-x86_64/config/
[elk@elk1 config]$ ll
总用量 8
-rw-r--r--. 1 zhjh256 zhjh256 4647 6月 2 12:32 kibana.yml
[elk@elk1 config]$ vim kibana.yml  # 确保下列配置正确

server.port: 5601

server.host: "192.168.230.128"

elasticsearch.url: "http://localhost:9200"

上述配置完成后,就能够启动了。

 

访问下http://192.168.230.128:5601。

第一次访问的时候,会要求设置Index Pattern,由于咱们在logstash-es.conf中设置为logstash-%{+YYYY-MM},因此设置为logstash-*就能够了。

Discover是主要的查询交互界面,以下所示:

 

搜索下,咱们刚才在控制台输入的测试文字:

 

有时候在访问discover的时候,提示no results found,以下所示:

 这一般是因为默认的查询时间范围过短的缘由,能够经过右上角的TimeRange来设置查询的时间范围。

到这里,ELK的环境搭建与基本配置就完成了。

更多的配置与优化参见各官方文档https://www.elastic.co/guide/index.html。后续有时间的话,笔者会再写一篇。

Filebeat安装与配置

采用Filebeat做为源端代理以后,准确的说,跟log4j已经没有关系了。因此这里假设读者知道log4j的配置,生成的文件在d:\httx\logs目录。

由于windows下Filebeat的启动脚本是使用powershell脚本编写的,因此确保安装了ps,windows 10下自带。

从https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.4-windows-x86_64.zip下载windows版本的filebeat。filebeat能够安装在任何目录,这里以D:\chrome下载\filebeat为例。

打开配置文件D:\chrome下载\filebeat\filebeat.yml,确保下列设置正确:

filebeat.prospectors:
- type: log
  enabled: true  #启用配置
  paths:
    - D:\httx\logs\*  #设置监控路径
#output.elasticsearch:   #禁用ES写入
output.logstash:            #启用logstash写入
  # The Logstash hosts
  hosts: ["192.168.230.128:5000"]

安装与启动filebeat。

打开powershell,执行以下命令:

PS C:\Users\admin> cd 'D:\chrome下载\filebeat'
PS D:\chrome下载\filebeat> .\install-service-filebeat.ps1

安全警告
请只运行你信任的脚本。虽然来自 Internet 的脚本会有必定的用处,但此脚本可能会损坏你的计算机。若是你信任此脚本,请使用
Unblock-File cmdlet 容许运行该脚本,而不显示此警告消息。是否要运行 D:\chrome下载\filebeat\install-service-filebeat.ps1?
[D] 不运行(D)  [R] 运行一次(R)  [S] 暂停(S)  [?] 帮助 (默认值为“D”): R

Status   Name               DisplayName
------   ----               -----------
Stopped  filebeat           filebeat


PS D:\chrome下载\filebeat> Start-Service filebeat
PS D:\chrome下载\filebeat> Stop-Service filebeat

filebeat的日志在C:\ProgramData\filebeat\logs目录下,这是写死的,不知道哪里能够更改。

filebeat会按期输出日志以及遇到的异常信息。

最后,咱们再回到kibana控制台,看下log4j相关的日志,以下:

左边控制要显示哪些列,好比显示来源和主机:

 

 到此为止,整个ELK+log4j的集成自己就完成了。可是要达到高效可用的步骤,下列问题还须要进一步研究:

一、filebeat读取文件彷佛是以行为单位,这在exception堆栈下是不可接受的,须要logstash或者其余方式二次处理。

二、es head的控制台应该来讲做为es的监控还能够,可是做为日志交互基本上没什么用,效果太差了。kibana还不错,只不过filter不是起码应该固定么??

三、elk的详细配置以及优化。

四、filebeat日志中出现"sync.go:105: ERR Failed to publish events (host: 10.172.0.165:5044:10200), caused by: write tcp ",可是不影响日志的发送。

参见ELK最新版6.2.4学习笔记-Logstash和Filebeat解析。对于log4j以及log4j2日志解析为每一个字段,后续将补充“使用grok将log4j/log4j2日志解析为ES字段”。

 

参考:

https://serverfault.com/questions/911440/filebeat-cant-connect-to-logstash-on-another-server

https://elasticsearch.cn/question/3157

其余:

[ERROR][logstash.plugins.registry] Tried to load a plugin's code, but failed. {:exception=>#<LoadError: no such file to load -- logstash/inputs/log4j>, :path=>"logstash/inputs/log4j", :type=>"input", :name=>"log4j"}
解决方法:运行bin/logstash-plugin install logstash-input-log4j 安装log4j插件,默认状况下,插件没有绑定,参考https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html
log4j:ERROR Could not connect to remote log4j server at [192.168.230.128]. We will try again later
解决方法:logstash的log4j监听地址由127.0.0.1改为ip好比192.168.230.128

log4j:WARN Detected problem with connection: java.net.SocketException: Connection reset by peer: socket write error采用低版本的ELK好比2.x版本,ELK 2.x以后的版本就是5.x、6.x,3.x、4.x是跳版了的。

相关文章
相关标签/搜索