ELK-Stack简介及安装手册

ELK Stack简介及安装手册

1、ELK Stack 简介

  • ELK是Elasticsearch、Logstash、Kibana的简称,这三者是核心套件,但并非全部,而且还可以添加Redis,kafka,filebeat等软件,后文的四种基本架构中将逐一介绍应用到的其它套件。

  • Elasticsearch:是一个分布式的实时全文搜索和分析引擎,提供搜集、分析、存储数据三大功能;是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统。它构建于Apache Lucene搜索引擎库之上。

  • Logstash:是一个用来搜集、分析、过滤日志的工具。它支持几乎任何类型的日志,包括系统日志、错误日志和自定义应用程序日志。它可以从许多来源接收日志,这些来源包括 syslog、消息传递(例如 RabbitMQ)和JMX,它能够以多种方式输出数据,包括电子邮件、websockets和Elasticsearch。

  • Kibana:是一个基于Web的图形界面,用于搜索、分析和可视化存储在 Elasticsearch指标中的日志数据。它利用Elasticsearch的REST接口来检索数据,不仅允许用户创建他们自己的数据的定制仪表板视图,还允许他们以特殊的方式查询和过滤数据。

  • Filebeat:ELK 协议栈的新成员,一个轻量级开源日志文件数据搜集器,基于 Logstash-Forwarder 源代码开发,是对它的替代。在需要采集日志数据的 server 上安装 Filebeat,并指定日志目录或日志文件后,Filebeat 就能读取数据,迅速发送到 Logstash 进行解析,亦或直接发送到 Elasticsearch 进行集中式存储和分析。

2、ELK 常用架构及使用场景介绍

2.1、架构一:基础ELK架构

我们先谈谈第一种ELK架构,如图1,这是最简单的一种ELK架构方式。优点是搭建简单,易于上手。缺点是Logstash耗资源较大,运行占用CPU和内存高。另外没有消息队列缓存,存在数据丢失隐患。建议供学习者和小规模集群使用。此架构首先由Logstash分布于各个节点上搜集相关日志、数据,并经过分析、过滤后发送给远端服务器上的Elasticsearch进行存储。Elasticsearch将数据以分片的形式压缩存储并提供多种API供用户查询,操作。用户亦可以更直观的通过配置Kibana Web Portal方便的对日志查询,并根据数据生成报表

2.1.1、图一:image001.png-19kB

2.2、架构二:基于消息队列的架构

位于各个节点上的Logstash Agent先将数据/日志传递给Kafka(或者Redis),并将队列中消息或数据间接传递给Logstash,Logstash过滤、分析后将数据传递给Elasticsearch存储。最后由Kibana将日志和数据呈现给用户。因为引入了Kafka(或者Redis),所以即使远端Logstash,server因故障停止运行,数据将会先被存储下来,从而避免数据丢失。这种架构适合于较大集群的解决方案,但由于Logstash中心节点和Elasticsearch的负荷会比较重,可将他们配置为集群模式,以分担负荷,这种架构的优点在于引入了消息队列机制,均衡了网络传输,从而降低了网络闭塞尤其是丢失数据的可能性,但依然存在Logstash占用系统资源过多的问题。

2.2.2、图二:image004.png-30.6kB

2.3、架构三:基于Filebeat架构

前面提到 Filebeat 已经完全替代了 Logstash-Forwarder 成为新一代的日志采集器,同时鉴于它轻量、安全等特点,越来越多人开始使用它。这个章节将详细讲解如何部署基于 Filebeat 的 ELK 集中式日志解决方案,具体架构见图 三。因为免费的 ELK 没有任何安全机制,所以这里使用了 Nginx 作反向代理,避免用户直接访问 Kibana 服务器。加上配置 Nginx 实现简单的用户认证,一定程度上提高安全性。另外,Nginx 本身具有负载均衡的作用,能够提高系统访问性能。

2.3.3、图三:image005.png-37.4kB

2.4、总结:

  • 不管采用上面哪种ELK架构,都包含了其核心组件,即:Logstash、Elasticsearch 和Kibana。当然这三个组件并非不能被替换,只是就性能和功能性而言,这三个组件已经配合的很完美,是密不可分的。各系统运维中究竟该采用哪种架构,可根据现实情况和架构优劣而定

3、ELK主要解决了哪些生产问题?

ELK在大数据运维系统中的应用,ELK组件各个功能模块如图四所示,它运行于分布式系统之上,通过搜集、过滤、传输、储存,对海量系统和组件日志进行集中管理和准实时搜索、分析,使用搜索、监控、事件消息和报表等简单易用的功能,帮助运维人员进行线上业务的准实时监控、业务异常时及时定位原因、排除故障、程序研发时跟踪分析Bug、业务趋势分析、安全与合规审计,深度挖掘日志的大数据价值。同时Elasticsearch提供多种API(REST JAVA PYTHON等API)供用户扩展开发,以满足其不同需求

3.1、图四:20160205205702084.png-72kB

3.2、总结:

  • 日志查询,问题排查,上线检查
  • 服务器监控,应用监控,错误报警,Bug管理
  • 性能分析,用户行为分析,安全漏洞分析,时间管理
  • 综上,ELK组件在大数据运维中的应用是一套必不可少的且方便、易用的开源解决方案。

4、EFK实战之监控自定义NGINX日志

4.1、实验环境:

平台 IP 用途 E版本 F版本 K版本
CentOS 6.7 64Bit 192.168.2.245 Elasticsearch+Head+Kibana 6.3.1 6.3.0
CentOS 6.7 64Bit 192.168.2.246 Elasticsearch+Head 6.3.1
CentOS 6.7 64Bit 192.168.2.11 Filebeat 6.3.0

4.1.1、配置EPEL源+ELK源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@localhost ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/Packages/e/epel-release-6-8.noarch.rpm
[root@localhost ~]# yum install vim telnet wget nethogs htop glances dstat traceroute lrzsz goaccess ntpdate dos2unix openssl-devel tcpdump lrzsz fio nss curl -y
[root@localhost ~]# yum groupinstall "Development Tools" -y
[root@localhost ~]# vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-6.x]
name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
[root@localhost ~]# vim /etc/yum.repos.d/kibana.repo
[kibana-6.x]
name=Kibana repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
[root@localhost ~]# vim /etc/yum.repos.d/logstash.repo
[logstash-6.x]
name=Elastic repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

4.1.2、安装及配置系统环境(all_es)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@test1 ~]# echo "* - nofile 65536" >> /etc/security/limits.conf
[root@test1 ~]# sed -i "s/1024/65536/g" /etc/security/limits.d/90-nproc.conf
[root@test1 ~]# echo "fs.file-max = 65536" >> /etc/sysctl.conf
[root@test1 ~]# wget http://192.168.1.231/soft/jdk-8u131-linux-x64.tar.gz
[root@test1 ~]# mkdir -pv /usr/java/
[root@test1 ~]# tar xzvf jdk-8u131-linux-x64.tar.gz -C /usr/java/
[root@test1 ~]# ln -s /usr/java/jdk1.8.0_131/bin/java /usr/sbin/
[root@test1 ~]# vim /etc/profile
JAVA_HOME=/usr/java/jdk1.8.0_131
export JAVA_HOME
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export CLASSPATH
PATH=$JAVA_HOME/bin:$PATH:$HOME/bin:$JAVA_HOME/bin
export PATH
export LANG=zh_CN.UTF-8
[root@test1 ~]# reboot

4.1.3、安装Elasticsearch(all_es)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@test1 ~]# yum install nss curl ca-certificates -y
[root@test1 ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
[root@test1 ~]# yum install elasticsearch -y
#主配置文件
/etc/elasticsearch/elasticsearch.yml
#JAVA启动配置文件
/etc/elasticsearch/jvm.options
#ES日志配置文件
/etc/elasticsearch/log4j2.properties
#ES启动脚本
/etc/init.d/elasticsearch
#ES主程序安装目录
/usr/share/elasticsearch/
/usr/share/elasticsearch/plugins
#ES日志输出目录
/var/log/elasticsearch
#ES运行PID文件目录
/var/run/elasticsearch
[root@test1 ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin install ingest-user-agent
[root@test1 ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin install ingest-geoip
[root@test1 ~]# mkdir -p /data/es-data
[root@test1 ~]# chown -R elasticsearch:elasticsearch /data/es-data
[root@test1 ~]# chown -R elasticsearch:elasticsearch /var/log/elasticsearch/
[root@test1 ~]# chkconfig elasticsearch on

4.1.4、核心配置文件详解

  • /etc/elasticsearch/elasticsearch.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    # ======================== Elasticsearch Configuration =========================
    #
    # NOTE: Elasticsearch comes with reasonable defaults for most settings.
    # Before you set out to tweak and tune the configuration, make sure you
    # understand what are you trying to accomplish and the consequences.
    #
    # The primary way of configuring a node is via this file. This template lists
    # the most important settings you may want to configure for a production cluster.
    #
    # Please consult the documentation for further information on configuration options:
    # https://www.elastic.co/guide/en/elasticsearch/reference/index.html
    #
    # ---------------------------------- Cluster -----------------------------------
    #集群名称,所有节点的集群名称需要一致
    cluster.name: lisir
    #本机节点名称
    node.name: efk-one
    # ----------------------------------- Paths ------------------------------------
    #数据存放目录
    path.data: /data/es-data
    #日志存放目录
    path.logs: /var/log/elasticsearch
    # ----------------------------------- Memory -----------------------------------
    #配置内存使用用交换分区
    bootstrap.memory_lock: false
    # ---------------------------------- Network -----------------------------------
    #监听的网络地址
    network.host: 0.0.0.0
    #开启监听的端口
    http.port: 9200
    # --------------------------------- Discovery ----------------------------------
    #集群内其他节点IP地址,(9300端口是节点内选举所用端口)
    discovery.zen.ping.unicast.hosts: ["192.168.2.245:9300", "192.168.2.246:9300"]
    #为了防止数据丢失,配置discovery.zen.minimum_master_nodes非常重要,使每个适合主的节点都知道要形成集群必须可见的适合主的节点的最小数目:(master_eligible_nodes / 2) + 1,换句话说,如果有三个主合格节点,那么最小主节点应该设置为(3/2)+1或2:
    discovery.zen.minimum_master_nodes: 2
    # ---------------------------------- outher -----------------------------------
    #如果要使用head,那么需要增加新的参数,使head插件可以访问es
    #开启跨域访问支持,默认为false
    http.cors.enabled: true
    #跨域访问允许的域名地址,(允许所有域名)以上使用正则
    http.cors.allow-origin: "*"
    #此配置解决报错“system call filters failed”
    bootstrap.system_call_filter: false
  • /etc/elasticsearch/jvm.options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
## JVM configuration

################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################
#在默认情况下,Elasticsearch告诉JVM使用最小和最大大小为1GB的堆,在迁移到生产时,重要的是配置堆大小以确保Elasticsearch有足够的可用堆。
#Elasticsearch将在jvm.options中通过Xms(最小堆大小)和Xmx(最大堆大小)的设置分配指定的整个堆。
#这些设置的值取决于服务器上可用RAM的数量,好的经验法则是:
#设置最小堆大小(Xms)和最大堆大小(Xmx)彼此相等
#对Elasticsearch可用的堆越多,它用于缓存的内存就越多,但是请注意,过多的堆可能会使您陷入长时间的垃圾收集停顿
#将Xmx设置为不超过物理RAM的50%,以确保有足够的物理RAM留给内核文件系统缓存
-Xms2g
-Xmx2g
  • /etc/elasticsearch/log4j2.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#配置RollingFile输出源
appender.rolling.type = RollingFile
appender.rolling.name = rolling
#日志到/var/log/elasticsearch/production.log
appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%.-10000m%n
#滚动日志到/var/log/elasticsearch/production-yyyy-MM-dd-i.log,日志将被压缩在每个滚动上,并且i将被递增。
appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.log.gz
appender.rolling.policies.type = Policies
#使用基于时间的滚动策略
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
#每天滚动日志
appender.rolling.policies.time.interval = 1
#在一天的边界上对齐滚动条(而不是每24小时滚动一次)
appender.rolling.policies.time.modulate = true
#使用基于大小的滚动策略
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
#在128MB后滚动日志
appender.rolling.policies.size.size = 128MB
#配置DefaultRolloverStrategy
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.fileIndex = nomax
#在滚动日志时使用删除操作
appender.rolling.strategy.action.type = Delete
#Elasticsearch日志的基本路径
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
#只删除匹配文件模式的日志
appender.rolling.strategy.action.condition.type = IfFileName
#模式是只删除主日志
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-*
#只有当我们积累了太多的压缩日志时才删除
appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize
#压缩日志的大小条件是2GB
appender.rolling.strategy.action.condition.nested_condition.exceeds = 2GB

4.1.5、安装elasticsearch-head

1
2
3
4
5
6
[root@test1 ~]# yum install -y npm
[root@test1 ~]# git clone git://github.com/mobz/elasticsearch-head.git
[root@test1 ~]# cd elasticsearch-head
[root@test1 ~]# npm config set strict-ssl false
[root@test1 ~]# npm install
[root@test1 ~]# npm run start &

4.1.6、安装kibana

1
2
3
4
5
6
7
8
9
[root@test1 ~]# yum install kibana
[root@test1 ~]# chkconfig kibana on
[root@test1 ~]# rpm -ql kibana
#kibana启动脚本
/etc/init.d/kibana
#配置文件
/etc/kibana/kibana.yml
#主程序安装路径
/usr/share/kibana/

4.1.7、kibana核心配置文件详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 默认值 : 5601 Kibana 由后端服务器提供服务。此设置指定要使用的端口
server.port: 5601
# 默认值 : “localhost” 此设置指定后端服务器的主机默认值 : “localhost” 此设置指定后端服务器的主机
server.host: "0.0.0.0"
# 使您能够指定如果您在代理后运行的 Kibana 的路径。这只影响 Kibana 生成的 URL,您的代理应该在转发请求到 Kibana 之前删除 basePath 值。此设置不能以斜杠(/)结尾。
#server.basePath: ""
# 默认值 : 1048576 传入服务器请求的最大有效负载大小(以字节为单位)。
#server.maxPayloadBytes: 1048576f you are running behind a proxy. This only affects
# 默认值 : “your-hostname” 用于标识此 Kibana 实例的可读的显示名称。
#server.name: "your-hostname"
# 默认值 : “http://localhost:9200” 要用于所有查询的 Elasticsearch 实例的 URL。
elasticsearch.url: "http://localhost:9200"
# 默认值 : true 当此设置的值为 true 时,Kibana 使用 server.host 设置中指定的主机名。当此设置的值为 false 时,Kibana 使用连接到此 Kibana 实例的主机的主机名。
#elasticsearch.preserveHost: true
# 默认值 : “.kibana”Kibana 使用 Elasticsearch 中的索引来存储保存的搜索,可视化和仪表板。如果索引不存在,Kibana 将创建一个新索引。
kibana.index: ".kibana"
# 默认值 : “discover” 要加载的默认应用程序。
#kibana.defaultAppId: "home"
# 如果您的 Elasticsearch 受基本认证保护,这些设置提供 Kibana 服务器用于在启动时对 Kibana 索引执行维护的用户名和密码。您的 Kibana 用户仍需要使用通过 Kibana 服务器代理的 Elasticsearch 进行身份验证。
#elasticsearch.username: "user"
#elasticsearch.password: "pass"
# 分别指向 PEM 格式 SSL 证书和 SSL 密钥文件的路径。这些文件为从 Kibana 服务器到浏览器的传出请求启用 SSL。
#server.ssl.enabled: false
#server.ssl.certificate: /path/to/your/server.crt
#server.ssl.key: /path/to/your/server.key
# 提供 PEM 格式 SSL 证书和密钥文件路径的可选设置。这些文件验证您的 Elasticsearch 后端使用相同的密钥文件。
#elasticsearch.ssl.certificate: /path/to/your/client.crt
#elasticsearch.ssl.key: /path/to/your/client.key
# 可选设置,使您能够为 Elasticsearch 实例指定证书颁发机构的 PEM 文件的路径。
#elasticsearch.ssl.certificateAuthorities: [ "/path/to/your/CA.pem" ]
# 要忽略SSL证书的有效性,请将此设置值更改为“NONE”。
#elasticsearch.ssl.verificationMode: full
# 默认值 : elasticsearch.requestTimeout 设置以毫秒为单位的时间等待 Elasticsearch 对 PING 作出响应。
#elasticsearch.pingTimeout: 1500
# 默认值 : 30000 等待来自后端或 Elasticsearch 的响应的时间(以毫秒为单位)。此值必须为正整数。
#elasticsearch.requestTimeout: 30000
# 默认值 : [ 'authorization' ] 要发送到 Elasticsearch 的 Kibana 客户端头标列表。要发送任何客户端头,请将此值设置为 [](一个空列表)。
#elasticsearch.requestHeadersWhitelist: [ authorization ]
# 默认值 : {} 要发送到 Elasticsearch 的 header name 和 value。不管如何配置 elasticsearch.tribe.requestHeadersWhitelist,任何自定义的 header 都不能被客户端的 header 覆盖。
#elasticsearch.customHeaders: {}
# 默认值 : 0 Elasticsearch 等待来自分片的响应的时间(以毫秒为单位)。设置为 0 可禁用。
#elasticsearch.shardTimeout: 0
# 默认值 : 5000 重试前在 Kibana 启动时等待 Elasticsearch 的时间(以毫秒为单位)。
#elasticsearch.startupTimeout: 5000
# 指定 Kibana 创建进程标识文件的路径。
#pid.file: /var/run/kibana.pid
# 默认值 : stdout 允许您指定 Kibana 存储日志输出的文件
#logging.dest: stdout
# 默认值 : false 将此设置的值设置 true 为禁止所有日志记录输出。
#logging.silent: false
# 默认值 : false 将此设置的值设置 true 为禁止除错误消息之外的所有日志记录输出。
#logging.quiet: false
# 默认值 : false 将此设置的值设置为 true 记录所有事件,包括系统使用信息和所有请求。
#logging.verbose: false
# 默认值 : 5000 设置示例系统和过程性能指标的间隔(以毫秒为单位)。最小值为 100。默认值 : 5000 设置示例系统和过程性能指标的间隔(以毫秒为单位)。最小值为 100。
#ops.interval: 5000
# 默认字符集设置
#i18n.defaultLocale: "en"

4.1.8、Filebeat概述:

Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并且转发这些信息到elasticsearch或者logstarsh中存放。以下是filebeat的工作流程:当你开启filebeat程序的时候,它会启动一个或多个探测器(prospectors)去检测你指定的日志目录或文件,对于探测器找出的每一个日志文件,filebeat启动收割进程(harvester),每一个收割进程读取一个日志文件的新内容,并发送这些新的日志数据到处理程序(spooler),处理程序会集合这些事件,最后filebeat会发送集合的数据到你指定的地点。如图五:

4.1.9、图五:filebeat.png-79.4kB

4.1.10、安装Filebeat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@test1 ~]# curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.3.0-x86_64.rpm
[root@test1 ~]# rpm -vi filebeat-6.3.0-x86_64.rpm
[root@test1 ~]# chkconfig filebeat on
[root@test1 ~]# rpm -ql filebeat
#filebeat启动脚本
/usr/bin/filebeat
/etc/init.d/filebeat
#主程序路径
/usr/share/filebeat/
#filebeat模块配置文件路径
/usr/share/filebeat/module
#主程序安装路径
/usr/share/kibana/
#主配置文件路径
/etc/filebeat
#模块配置文件夹引用路径
/etc/filebeat/modules.d/

4.1.11、主配置文件详解:/etc/filebeat/filebeat.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#======= Filebeat modules =======
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
reload.period: 10s

#======= Elasticsearch template setting =======
#如修改了默认索引名,此处必须配置
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.fields: "/etc/filebeat/fields.yml"
setup.template.overwrite: "true"
#kiabna展示默认索引名
setup.dashboards.index: "nginx-*"
setup.template.settings:
index.number_of_shards: 5

#======= Kibana =======
setup.kibana:
host: "192.168.2.245:5601"

#======= Elasticsearch output =======
#when.contains配置判断输入字段是否包含特定内容,并创建特定的索引,主要用来区分日志
output.elasticsearch:
hosts: ["192.168.2.245:9200","192.168.2.246:9200"]
index: "defaults-logs-%{+yyyy.MM.dd}"
indices:
- index: "nginx-error-logs-%{+yyyy.MM.dd}"
when.contains:
fileset.name: "error"
- index: "nginx-access-logs-%{+yyyy.MM.dd}"
when.contains:
fileset.name: "access"
# Optional protocol and basic auth credentials.
#protocol: "https"
#username: "elastic"
#password: "changeme"
#多个工作线程开启负载模式
worker: "2"

4.1.12、开启filebeat nginx 模块,支持过滤日志

1
[root@test1 ~]# filebeat modules enable nginx

4.1.13、载入filebeat自带的仪表盘和可视化代码,通过kibana展示

1
[root@test1 ~]# filebeat setup --dashboards

4.1.14、修改filebeat nginx模块配置文件,input数据

1
2
3
4
5
6
7
8
9
10
11
12
[root@test1 ~]# vim /etc/filebeat/modules.d/nginx.yml
- module: nginx
# Access logs
access:
enabled: true
#新建access目录用来区分日志
var.paths: ["/usr/local/nginx/logs/access/*"]

error:
enabled: true
#新建error目录用来区分日志
var.paths: ["/usr/local/nginx/logs/error/*"]

4.1.15、修改filebeat nginx模块access默认配置文件用来匹配自定义日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#获取自定义NGINX日志格式
log_format access '$remote_addr - $remote_user [$time_local] "$server_name" "$request" '
'$status $body_bytes_sent "$request_body" "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$connection $upstream_addr '
'$upstream_response_time $request_time ';

#对模块源码进行调整,匹配自定义NGINX格式,注意数据类型
[root@test1 ~]# vim /usr/share/filebeat/module/nginx/access/ingest/default.json
"patterns":[
"\"?%{IP_LIST:nginx.access.remote_ip_list} - %{DATA:nginx.access.user_name} \\[%{HTTPDATE:nginx.access.time}\\] \"%{DATA:nginx.access.servername}\" \"%{GREEDYDATA:nginx.access.info}\" %{NUMBER:nginx.access.response_code} %{NUMBER:nginx.access.body_sent.bytes} \"%{DATA:nginx.access.request_body}\" \"%{DATA:nginx.access.referrer}\" \"%{DATA:nginx.access.agent}\" \"%{DATA:nginx.access.http_x_forwarded_for}\" %{NUMBER:nginx.access.connection} %{DATA:nginx.access.upstream_addr} %{DATA:nginx.access.upstream_response_time} %{DATA:nginx.access.request_time}"]

#默认的nginx模块只支持默认日志格式,如有变动需要在此处新增字段名,文章过长,只展示新增部分
[root@test1 ~]# vim /etc/filebeat/fields.yml
- key: nginx
title: "Nginx"
description: >
Module for parsing the Nginx log files.
short_config: true
fields:
......
- name: servername
type: keyword
description: >
The http servername
- name: request_body
type: text
format: bytes
description: >
The http request_body
- name: referrer
type: keyword
description: >
The HTTP referrer.
- name: http_x_forwarded_for
type: text
description: >
The http http_x_forwarded_for
- name: connection
type: keyword
example: GET
description: >
The http connection
- name: upstream_addr
type: keyword
example: GET
description: >
The http upstream_addr
- name: upstream_response_time
type: keyword
example: GET
description: >
The http upstream_response_time
- name: request_time
type: keyword
example: GET
description: >
The http request_time
......

4.1.16、修改filebeat nginx模块error默认配置文件用来解决时区问题

1
2
3
4
5
6
7
[root@test1 ~]#vim /usr/share/filebeat/module/nginx/error/ingest
"date": {
"field": "nginx.error.time",
"target_field": "@timestamp",
"formats": ["YYYY/MM/dd H:m:s"],
"timezone": "Asia/Shanghai"
}

4.1.17、启动filebeat,观察日志

1
2
[root@test1 ~]# service filebeat start
[root@test1 ~]# filebeat -e #开启debug模式

5、相关手册整理

官方文档:Elastic Stack and Product Documentation

官方文档:Elasticsearch Reference

官方文档:Logstash Reference

官方文档:Filebeat Reference

官方文档:Kibana User Guide

官方文档:关于Load the index template in Elasticsearch

Grok Debugger

Grok 官方自带Patterns

Kibana汉化地址

ELK+Filebeat 集中式日志解决方案详解

ELK-filebeat收集日志到Kafka,并转存ES

Logstash收集nginx日志之使用grok过滤插件解析日志

ELK日志服务使用-filebeat多文件发送

ElasticSearch Settings全部参数和默认值

ElasticSearch 常用命令

ElasticSearch 常用工具清单

-------------本文结束感谢您的阅读-------------
LiGuanCheng wechat
如有问题,请与我微信交流或通过右下角“daovoice”与我联系~。
请我喝一杯咖啡~