#需求:
做一个收集Nginx的access log和error log并绘制图表
#解决方案
采用filebeat6.6.2
在Nginx应用服务器上收集日志,经过kafka2.1.1
(zookeeper
集群)消息队列中间件传入到logstash
进行过滤解析,然后存储到elasticsearch
,最终由kibana
进行查询和制图。并且elk
、filebeat
和kafka
都采用docker
的方式进行部署,采用docker-compose
进行编排方便线上的维护
使用filebeat的原因:对比logstash,filebeat更加轻量,且消耗资源更少
采用kafka作为中间件原因:避免直接传入logstash引起的io瓶颈,同时具有较高吞吐量,而且比较稳定,处理消息的效率很高
PS:当看到kibana正常load数据的时候,有几点感触
1.一定要好好看elk和filebeat的官方文档,知道filebeat的output和logstash的input的各个参数使用
2.使用docker部署kafka & zookeeper会有个坑,可能也是楼主对docker网络和kafka不熟悉的原因造成的
#实际场景
应用程序 | 虚拟机IP |
---|---|
Nginx | 10.150.33.123 |
#部署情况
应用程序 | 虚拟机IP | 备注 | 端口 |
---|---|---|---|
Nginx | 10.150.33.123 | 日志文件位于/var/log/nginx | |
filebeat | 10.150.33.123 | ||
kafka & zookeeper | 10.150.33.126 | zookeeper集群,单机kafka | 9092、2181 |
elk | 10.150.33.126 | 需与kafka处于同一network | 9200,5601,9300,5000,9600 |
#Nginx
/var/log/nginx下有文件,Nginx中的access和error日志格式是不同的,需要通过logstash进行处理
- www.test.com.access.log
- www.test.com.error.log
- api.test.com.access.log
- api.test.com.error.log
- openapi.test.com.access.log
- openapi.test.com.error.log
#filebeat部署
先看filebeat的目录结构如图
#####第一步:编辑Dockerfile
FROM docker.elastic.co/beats/filebeat:6.6.2
##enable Nginx modules,注意这里需要开启filebeat的Nginx模块
RUN /usr/share/filebeat/filebeat modules enable nginx
COPY filebeat.yml /usr/share/filebeat/filebeat.yml
USER root
RUN chown root:filebeat /usr/share/filebeat/filebeat.yml
USER filebeat
具体参考官方文档
#####第二步:编辑docker-compose.yml
version: '2.3'
services:
beat:
build:
context: ${PWD}/.
user: root
environment:
- BEAT_STRICT_PERMS=false
restart: always
volumes:
#filebeat.yml作为配置文件,ro表示只读
- ${PWD}/filebeat.yml:/usr/share/filebeat/filebeat.yml:ro
#为了filebeat的module.d配置nginx.yml
- ${PWD}/modules.d:/usr/share/filebeat/modules.d
- /var/lib/docker/containers:/var/lib/docker/containers:ro
# We launch docker containers to test docker autodiscover:
- /var/run/docker.sock:/var/run/docker.sock
#把logs和data共享出来
- ${PWD}/logs:/usr/share/filebeat/logs
- ${PWD}/data:/usr/share/filebeat/data
##为了读取外部的日志文件,开启共享,下面举例为读取外部Nginx的/var/logs/nginx里的日志
- /var/log/nginx:/var/log/nginx
extra_hosts:
## 这里是为了解决kafka的一个坑,需要在filebeat上作hosts的映射
- "kafka:10.150.33.126"
#####第三步:编辑module.d的nginx.yml文件
可以通过下载filebeat的安装包然后把里面的module.d文件夹复制过来
然后把nginx.yml.disabled
修改名为nginx.yml
,然后编辑如下:
# Module: nginx
# Docs: https://www.elastic.co/guide/en/beats/filebeat/master/filebeat-module-nginx.html
- module: nginx
# Access logs
access:
enabled: true
# Set custom paths for the log files. If left empty,
# Filebeat will choose the paths depending on your OS.
#var.paths:
var.paths: ["/var/log/nginx/*.test.com.access.log"]
# Convert the timestamp to UTC. Requires Elasticsearch >= 6.1.
var.convert_timezone: true
# Error logs
error:
enabled: true
# Set custom paths for the log files. If left empty,
# Filebeat will choose the paths depending on your OS.
var.paths: ["/var/log/nginx/*.test.com.error.log"]
# Convert the timestamp to UTC. Requires Elasticsearch >= 6.1.
var.convert_timezone: true
具体参考官方文档 Nginx Filebeat module
#####第四步:编辑filebeat.yml
###################### Filebeat Configuration Example #########################
# This file is an example configuration file highlighting only the most common
# options. The filebeat.reference.yml file from the same directory contains all the
# supported options with more comments. You can use it as a reference.
#
# You can find the full configuration reference here:
# https://www.elastic.co/guide/en/beats/filebeat/index.html
# For more available modules and options, please see the filebeat.reference.yml sample
# configuration file.
#=========================== Filebeat inputs =============================
filebeat.inputs:
# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Belo