본문 바로가기
Server

Nginx 서버에 filebeat와 metricbeat 설치 후 설정파일 작성

by Ahngyuho 2023. 7. 13.

이제 ec2에 nginx 서버를 설치해보고 이 웹 서버의 로그와 메트릭 정보를 Elasticsearch로 넘겨서 Kibana로 시각화 해보겠습니다.

 

환경은 ec2의 Ubuntu 22.04버전 입니다.

Nginx 설치


sudo apt-get update
sudo apt-get install nginx
sudo systemctl enable nginx.service

순서대로 입력해주시면 됩니다.

이런식으로 active라고 나오면 성공입니다.

Filebeat, metricbeat 설치 및 설정


설치

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-8.x.list
sudo apt-get update && sudo apt-get install filebeat
sudo apt-get update && sudo apt-get install metricbeat

 

이렇게 차례대로 복사해서 입력해주시면 설치는 완료됩니다.

 

아래 명령어를 입력해주시면 서버 부팅시 서비스 자동 시작됩니다.

sudo systemctl enable filebeat.service
sudo systemctl enable metricbeat.service

 

설정

filebeat

sudo vi /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.

# filestream is an input for collecting log messages from files.
- type: filestream

  # Unique ID among all inputs, an ID is required.
  id: my-filestream-id

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

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /var/log/nginx/access.log
    #- c:\programdata\elasticsearch\logs\*

# ============================== Filebeat modules ==============================

filebeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml
  enabled: true
  # Set to true to enable config reloading
  reload.enabled: true

  # Period on which files under path should be checked for changes
  reload.period: 10s

# ================================= Dashboards =================================
# These settings control loading the sample dashboards to the Kibana index. Loading
# the dashboards is disabled by default and can be enabled either by setting the
# options here or by using the `setup` command.
setup.dashboards.enabled: true



# =================================== 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: "서버주소:5601"

# ---------------------------- Elasticsearch Output ----------------------------
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["서버주소:9200"]

#은 주석으로 서비스 실행에 영향을 미치지 않습니다. 그래서 적용되어야 하는 부분은 #을 지워주셔야 합니다.

아마 host 부분은 초기 설정이 localhost로 되어 있을 겁니다. ELK 가 Nginx와 동일한 서버에 설치되어 있다면 그대로 두시면 되고 그렇지 않다면 서버 주소를 적어주셔야 합니다. 

 

저는 ec2를 사용중인데 ec2를 사용하시는 분들이라면 퍼블릭 IPv4 주소를 넣어주시면 됩니다.

 

metricbeat

sudo vi /etc/metricbeat/metricbeat.yml

 

# =================================== 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: "3.36.169.149:5601"

# ================================== Outputs ===================================

# Configure what output to use when sending the data collected by the beat.

# ---------------------------- Elasticsearch Output ----------------------------
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["3.36.169.149:9200"]

 

참고로 filebeat,metricbeat는 Outputs의 Elasticsearch와 Logstash를 동시에 설정해 둘 수 없습니다. 둘 중 하나만 선택해서 설정해 주시면 됩니다. 저는 Elasticseach로 설정했습니다.

 

sudo systemctl start metricbeat.service 
sudo systemctl start filebeat.service

그리고 이제 서비스 시작해주시고 정상동작 하는지 확인해 주시면 됩니다.

systemctl status metricbeat.service 
systemctl status filebeat.service

 

정상동작 확인하시면 혹시 모르니 ELK 서버로 들어가서 연결이 잘 되었는지 확인해 봅시다.

//netstat 명령어를 사용하기 위한 패키지 설치
sudo apt-get install net-tools

netstat -antp | grep 9200

 

nginx filebeat/metricbeat 서비스: 포트번호      Elasticserach ip:9200

총 2개가 떠야 잘 연결된겁니다. 

혹시 filebeat 쪽이 연결이 안 되어 보일 수 있는데 그럴 경우 nginx 서버를 로컬 브라우저로 접근하셔서 새로고침(nginx 루트 페이지에 요청 날리기) 몇 번 눌러주시면 연결되는 걸 확인하실 수 있을 겁니다.

 

이제 다음 포스팅에서는 Elasticsearch와 Kibana 서버를 다뤄보면서 정말 연결이 잘 되고 데이터가 잘 전달 되는지 확인하고 filebeat와 metricbeat가 지원하는 모듈을 활성화해서 Kibana에서 대시보드로 확인해보겠습니다.