새소식

Data Engineering/Distributed System

EFK(ELK) 구축 해보기!! (서버 로그 수집) - 3. 로그 만들고 Fluentd로 로그 확인

  • -

이번에는 

실제로 EFK를 구축해보려고 한다.

 

아래는 계획한 기본적인 아키텍처이다.

* 미리 ec2 서버 2대를 생성하였다.

1. Fluentd 설치하고 로그 확인

가장 먼저 App에서 만든 로그파일을

Fluentd가 파싱하고 이를 다른 서버에 있는 Opensearch에게 전송하는 것이다.

 

*** 서버 1에서 진행

Fluentd 설치

이를 위해 Fluentd를 설치해야하는데

https://docs.fluentd.org/installation/before-install

 

Before Installation - Fluentd

Fluentd sometimes uses predictable paths for dumping, writing files, and so on. This default settings for the protections are in /etc/sysctl.d/10-link-restrictions.conf, or /usr/lib/sysctl.d/50-default.conf or elsewhere.

docs.fluentd.org

이 메뉴얼을 따라 시스템 설정을 변경하면 된다.

 

이후 Reboot해주는 것이 가장 안전하다.

 

Fluentd는 루비언어로 만들어졌기 때문에

Ruby gem을 설치한다.

 

sudo apt  install ruby-rubygems -y
sudo apt  install ruby-dev -y

 

이후 fluentd를 설치한다.

sudo gem install fluentd --no-doc

 

fluentd 디렉토리를 세팅하고

 

fluentd --setup ./fluent

 

테스트 해본다.

fluentd -c ./fluent/fluent.conf -vv &

echo '{"json":"message"}' | fluent-cat debug.test

 

Fluentd 프로세스를 종료시키고 싶다면 pkill 하면 된다.

pkill -f fluentd

 

추가적으로 수집할 로그를 만들기 위해 
'Log Generator'를 설치했다.

mkdir loggen && cd loggen
wget https://github.com/mingrammer/flog/releases/download/v0.4.3/flog_0.4.3_linux_amd64.tar.gz

tar -xvf flog_0.4.3_linux_amd64.tar.gz
./flog --help

 

json 버전과 apache 버전으로 날짜가 다른 파일 3개씩 만들었다.

 

# json
./flog -f json -t log -s 1m -n 1000 -o $filename -w &

# apache
./flog -f apache_common -t log -s 1m -n 1000 -o $filename -w &

예를 들어 vi에서 12/26일 파일을 27일로 만들고 싶으면

이렇게 vi에서 수정하면 된다.

:%s/26\/Dec/27\/Dec/g

 


 

이제 로그파일을 읽고 필터링 후 보내보려고 한다.

 

Fluentd는 여러 설정 명령들이 있으며

가장 확실한 방법은 공식 메뉴얼을 보는 것이다.

https://docs.fluentd.org/configuration/config-file

 

Config File Syntax - Fluentd

Fluentd accepts all non-period characters as a part of a tag. However, since the tag is sometimes used in a different context by output destinations (e.g. the table name, database name, key name, etc.), it is strongly recommended that you stick to the lowe

docs.fluentd.org

 

필요한 것만 보자면

  • source : input sources
  • match : output destinations
  • filter : event processing pipelines
  • system : system-wide configuration
  • label : directives group the output and filter for internal routing
  • worker : worker 수 설정
  • @include : include other files

* Fluentd는 대부분 기능을 Plugin으로 제공하며 이에 따라 작성이 조금씩 다르다.

그래서 공식문서를 참고하는 것이 가장 좋다.

 

fluent 폴더 안 fluent.conf 안의 내용들을 수정한다.

vi fluent.conf

 

json버전

<source>
  @type tail
  tag log.json.*
  path /home/ubuntu/loggen/json-*.log
  pos_file positions-json.pos
  read_from_head true
  follow_inodes true

  <parse>
    @type json
    time_key datetime
    time_type string
    time_format %d/%b/%Y:%H:%M:%S %z
  </parse>
</source>

 

regex 버전

<source>
  @type tail
  tag log.apache.*
  path /home/ubuntu/loggen/apache-*.log
  pos_file positions-apache.pos
  read_from_head true
  follow_inodes true
  <parse>
    @type regexp
    expression /^(?<client>\S+) \S+ (?<userid>\S+) \[(?<datetime>[^\]]+)\] "(?<method>[A-Z]+) (?<request>[^ "]+)? (?<protocol>HTTP\/[0-9.]+)" (?<status>[0-9]{3}) (?<size>[0-9]+|-)/
    time_key datetime
    time_format %d/%b/%Y:%H:%M:%S %z
  </parse>
</source>

 

기존 걸 모두 지우고 둘 다 붙여준다.

 

지금은 Opensearch에게 전송이 안되기 때문에 stdout으로만 확인한다.

아래도 붙여준다.

 

<match log.json.**>
  @type stdout
</match>
<match log.apache.**>
  @type stdout
</match>

 

이제 conf파일에서 나와

변경 사항 반영해준다.

 

fluentd -c ./fluent.conf -vv

 

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.