본문 바로가기

모니터링

[Elastic Stack #2] Docker로 Elastic Stack 구축해서 Filebeat로 로그 수집하기 - 2

728x90

 

안녕하세요 바쁘다 바뻐 현대사회, 알쏭달쏭 모바일 세상에서 열심히 살려고 노력하는 글쓴이입니다.

 

오늘은 지난번에 구축한 Elastic Stack을 통해 Filebeat로 수집한 log를 모니터링하는 법에 대해 글을 쓰려합니다.

 

우선 Filebeat로 log를 수집하고 말고 전에 일단 진짜 로그파일이 필요합니다.

 

어떤 작업을 수행하고나서 success log, error log 등 어떤 프로그램을 돌리고 나서 나오는 log가 우선 필요한데

 

제가 돌리는 프로그램은 python으로 작성이 되어있어서 파이썬 log를 사용하여 우선 log를 수집했습니다. 처음에는 아무생각 없이 aa.log 이런식으로 저장을 했었는데 python에서 ecs 입맛에 맞게 log를 만들어주는 라이브러리가 있어서 수정했습니다.

 

요거를 일단 좀 참고하시면 될거같습니다. ecs_logging 라이브러리 사용에 관한 글입니다.

https://www.elastic.co/guide/en/cloud/current/ec-getting-started-search-use-cases-python-logs.html

 

Ingest logs from a Python application using Filebeat | Elasticsearch Service Documentation | Elastic

Depending on variables including the installation location, environment, and local permissions, you might need to change the ownership of filebeat.yml. You can also try running the command as root: sudo ./filebeat setup -e or you can disable strict permiss

www.elastic.co

 

아무튼 이런식으로 로그를 만들게되면 json파일이 저장되게 되는데 그 json파일을 filebeat를 통해 elastic search로 보내야합니다. 

 

자 filebeat를 설치를 해봅시다. 참고로 filebeat는 로그를 수집하는 서버에 하셔야합니다. 마치 자빅스 agent같은 느낌으로 말이죠

 

curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.2.0-amd64.deb

sudo dpkg -i filebeat-7.2.0-amd64.deb

설치에는 여러방법이있겠지만 저는 이방법으로 했던거 같습니다. 

 

설치가 끝나면 이제 filebeat를 elastic search나logstash, kibana에 연결해주기위해서 yml 파일을 수정해줘야합니다.

 

/etc/filebeat/ 에 들어가보시면 filebeat.yml 파일이있습니다.

 

#=========================== 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.
# Below are the input specific configurations.

- type: log

  # Change to true to enable this input configuration.
  enabled: true

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /home/user/test/log/*.json

  json.keys_under_root: true
  json.overwrite_keys: true
  json.add_error_key: true
  json.expand_keys: true

일단 여기서 json 파일 설정부터 해줘야하는데 이건 뭐냐 filebeat로 수집할 로그에 대한 설정입니다. 보시면 paths가 있습니다. path에  *.json의 의미는 .json 으로 끝나는 파일명을 수집하겠다는 얘기고 밑에 json 관련된 내용을 적어줍시다. 

 

그리고 밑에 내리시다보면 kibana와 output.elasticsearch 가 있는데 여기에 연결할 elasticsearch에 대한 정보를 적어줍시다. 

logstash는 안쓸거니까 제껴둡시다. 

#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["localhost:9200"]
  # Optional protocol and basic auth credentials.
  #protocol: "https"
  username: "elastic"
  password: "비밀번호"
  
  
  
  
#============================== Kibana =====================================

# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
setup.kibana:

  # Kibana Host
  # Scheme and port can be left out and will be set to the default (http and 5601)
  # In case you specify and additional path, the scheme is required: http://localhost:5601/path
  # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
  host: "localhost:5601"

 

이제 filebeat를 setup하고 실행시킵시다.

filebeat setup -e

service filebeat start

 

자 이제 kibana로 들어가서 한 번 확인해봅시다.

 

이야 이런식으로 로그가 수집되고 있군요~~

 

 

 

대시보드를 활용하면 이렇게 모니터링할 수도 있습니다.

 

좋네요 다음시간에는 metricbeat를 사용해서 시스템 모니터링에 대해 적어보겠습니다.

반응형