基于docker-compose编排elk6.6+filebeat6.6.2+kafka2.1.1部署日志收集平台

本文档介绍了如何利用filebeat收集Nginx日志,通过kafka消息队列传输到elk进行日志监控和分析。详细步骤包括filebeat的Dockerfile和配置文件编辑,kafka与zookeeper的docker-compose配置,以及elk的部署和logstash配置。在部署过程中,强调了官方文档的重要性以及在docker环境下可能遇到的问题和解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#需求:
做一个收集Nginx的access log和error log并绘制图表
#解决方案
采用filebeat6.6.2在Nginx应用服务器上收集日志,经过kafka2.1.1zookeeper集群)消息队列中间件传入到logstash进行过滤解析,然后存储到elasticsearch,最终由kibana进行查询和制图。并且elkfilebeatkafka都采用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的目录结构如图
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值