개발/Kafka

[Kafka] - 20. Filebeats to Kafka(Log produce)

Dortmoot 2022. 8. 25. 19:38

Appicatoin의 Log 데이터를 ELK ( Elasticsearch,Kibana,logstash ) 로 구축하여 볼 예정입니다.

들어가기 앞서, Kafka는 Docker-compose로 이미 구성을 완료하였습니다.

 

Architecture


  • 구상한 Architecture = log 수집 -> filebeats -> Kafka -> logstash -> elasticsearch입니다.

 

1. Filebeats


  • 가상 로그 환경을 만들어주는 환경을 사용하여 구축할 예정입니다. ( Fake-Apache-Log-generator )
  • docker-compose로 아래와 같이 구성하여 줍니다.
  • centos8 version은 EOL되었기 때문에 mirroring error가 발생하여 값을 변경하여 줍니다.
#docker-compose-pipeline-application.yml
version: '3'
services:
  apache-app:
    image: centos:centos8
    hostname: apache-app-server1
    network_mode: host
    command:
      - bash
      - -c
      - >
        sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-Linux-*;
        sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-Linux-*;
        yum -y install git vim;
        yum -y install python2 python2-pip;
        curl -s https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.15.1-linux-x86_64.tar.gz -o filebeat.tar.gz && tar xvfz filebeat.tar.gz -C /;
        git clone https://github.com/kiritbasu/Fake-Apache-Log-Generator log_generator && cd log_generator && pip2 install -r requirements.txt;
        python2 apache-fake-log-gen.py -n 0 -o LOG
    tty: true

 

 

1-1) Filebeats setting init

  • Kafka와 연동하기 위한 값을 setting하여 줍니다.
  • filebeat.yml을 Kafka와 연동하여 설정하여 주어야 합니다.
$ docker exec -it elk-apache-app-1 /bin/bash
$ cd filebeat-7.15.1-linux-x86_64/
$ vim filebeat.yml

1-2) Input setting

  • enabled : false -> true
  • path : log가 쌓이는 파일명을 설정하여 줍니다.
# input
# Change to true to enable this input configuration.
  enabled: true

# Paths that should be crawled and fetched. Glob based paths.
paths:
    - /log_generator/access*.log

 

1-3) Output setting

  • hosts : kafka 연동 정보
  • topic : Produce할 Topic
  • compression : 압축 정보
# output
output.kafka:
  # Array of hosts to connect to.
  hosts: ["172.28.128.1:19092","172.28.128.1:29092","172.28.128.1:39092"]
  topic: "webapp-logs"
  compression: gzip

1-4) Producing

  • Kafka Topic에 log들을 Produce 하여 줍니다.
$ ./filebeat

1-5) Trace log

  • 현재 logfile이 제대로 동작하는지 확인하기 위하여 사용합니다.
# trace logfile
$ tail -f logfile

결과