Monitoring

ELK Stack 구성해보기

Joon0464 2021. 8. 3. 12:50

※ 사용 시스템 

AWS EC2

ubuntu 16.04

 

filebeat, apache2 => t2.micro

 

logstash => t2.small

 

※ 

Filebeat -> NLB -> Logstash

 

1. Filebeat과 apache서버 구성

1.1 테스트를 위한 Apache 설치

$ sudo apt-get install -y apache2

 apache2를 설치한다.

$ sudo vi /var/www/html/index.html

apache 서버의 메인 페이지를 원하는대로 수정한다.

$ sudo systemctl enable apache2
$ sudo systemctl start apache2
$ sudo systemctl stauts apache2

아파치 서버를 시작 프로그램으로 등록 및 실행시켜주고 상태를 확인한다.

접속되는지 확인한다.

1.2 Filebeat 설치 (ubuntu 16.04)

참고 사이트(Repositories for APT and YUM | Filebeat Reference [7.13] | Elastic)

1.2.1 Public Signing Key 다운로드 및 설치

$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

 

1.2.2 apt를 이용하여 filebeat를 설치하려면 apt-transport-https라는 패키지가 필요하다.

$ sudo apt-get install apt-transport-https

1.2.3 repository의 정보를 /etc/apt/sources.list.d/elastic-7.x.list 파일에 기록한다.

$ echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

1.2.4 apt-get update 및 filebeat 설치를 진행한다.

$ sudo apt-get update && sudo apt-get install filebeat

1.2.5 filebeat를 시작 프로그램으로 등록 및 실행한다.

$ sudo systemctl enable filebeat
$ sudo systemctl start filebeat

1.2.6 filebeat.yml 파일 수정

$ 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.

- type: log

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

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


                                 ```
                                 ```
                                 ```
                                 ```
                                 
# ================================== 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: ["localhost:9200"]

  # Protocol - either `http` (default) or `https`.
  #protocol: "https"

  # Authentication credentials - either API key or username/password.
  #api_key: "id:api_key"
  #username: "elastic"
  #password: "changeme"

# ------------------------------ Logstash Output -------------------------------
output.logstash:
  # The Logstash hosts
  hosts: ["LB-Logstash-4d348ebd65d46781.elb.ap-northeast-2.amazonaws.com:5044"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"

  # Client Certificate Key
  #ssl.key: "/etc/pki/client/cert.key"

filebeat을 통해 apache 서버의 access.log를 logstash로 전송하기 위해 filebeat.yml을 설정한다.

먼저 Filebeat inputs 설정에서 enabled를 반드시 true로 설정해야한다.

이후 path에 logstash로 전송을 원하는 로그 파일의 경로를 적어준다.

output.elasicsearch는 모두 주석처리한다.

output.logstash의 주석을 해제하고 hosts에 logstash와 연결된 ELB의 DNS주소와 포트를 입력한다.

 

2. Logstash 구축

 

2.1 java 설치

logstash는 반드시 java가 설치되어 있어야 동작 가능하다.

$ sudo apt-get install -y openjdk-8-jdk
$ java -version

2.1.1 java 환경변수 설정

$ sudo vi /etc/profile
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
export Class_PATH=$JAVA_HOME/lib:$CLASS_PATH

$ source /etc/profile

 

2.2 Logstash 설치

반드시 최소 메모리 2기가 이상의 가상머신에 설치해야한다.

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

2.2.1 filebeat에서 apache 로그를 받아오도록 설정

vi /etc/logstash/conf.d/logstash.conf

# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.

input {
  beats {
    port => 5044
  }
}

filter {
  mutate {
    remove_field => ["agent"]
  }
  grok {
    match => { "message" => [ "%{COMBINEDAPACHELOG}" ] }
  }
}

output {
  stdout {
    codec => rubydebug
  }
}

Logstash는 input -> filter -> output 의 총 3단계의 파이프라인을 통해 데이터를 가공한다.

input은 데이터를 받아올 곳을 지정하는데 filebeat을 통해 로그를 전송받기 위해 beats를 사용하여 5044번 포트로 설정한다.

filter는 데이터를 가공할 방법을 설정하는데 mutate는 삭제할 필드를 지정하는 설정이며 grok은 message 필드를 가공하여 각각의 위치에 맞게 필드로 펼쳐주기 위해 사용한다.

자세한 필터 설명은 아래 주소를 참고하면 된다.

https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html

 

 

https://cherrypick.co.kr/using-the-aws-elasticsearch-service-kibana-with-aws-cognito/

https://aws.amazon.com/ko/premiumsupport/knowledge-center/es-connect-filebeat-logstash-linux/