본문 바로가기

여러가지/구축 & 설치

[실습] ELK Stack

구성

(서버) 데이터 -> (Beat) Data Collection -> (Logstash) Data Processing -> ( ElasticSearch) Storage -> (Kibana) Visualize

  • ElasticSearch : LogStash를 통해서 전송받은 데이터 분석 및 저장 기능 담당
  • Logstash : 데이터 처리 파이프라인, 로그 수집하여 ElasticSearch에 전송
  • Kibana : ElasticSearch에 저장되어 있는 데이터 시각화 및 실시간 분석
  • Beat : 대상 서버에서 데이터 수집

구축

(선수 작업) Java 설치

# java -version

# yum install -y java-1.8.0

 

 

(ㄱ) ElasticSearch

Step 1. Elasticsearch GPG 키 가져오기

# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

 

Step 2. 저장소(repository) 정의 파일 생성 및 작성

(파일명) elasticsearch.repo

# vi /etc/yum.repos.d/elasticsearch.repo

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum 

gpgcheck=1 
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch 

enabled=1  
autorefresh=1 
type=rpm-md 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

 

[참고] https://uyijune15.tistory.com/190

 

[ELK Stack] .repo

# vi /etc/yum.repos.d/elasticsearch.repo-----------------------------------------------------------------------------------------------------------------------------------------------------------------[elasticsearch]  # 저장소 이름   ※ YUM 명

uyijune15.tistory.com

 

Step 3. elasticsearch 설치

# yum install --enablerepo=elasticsearch elasticsearch -y

 

Step 4. 파일 설정

(파일명) elasticsearch.yml

# vi /etc/elasticsearch/elasticsearch.yml

 

※ 아래는 하나의 서버에 하나의 노드만 실행시키는 경우입니다.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

cluster.name: test-cluster
node.name: test-node

node.data: true 

node.master: true

path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["test-node"]

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

 

[참고] https://uyijune15.tistory.com/191

 

[ELK Stack] elasticsearch.yml

● 전체 예시-----------------------------------------------------------------------------------------------------------------------------------------------------------------cluster.name: my-cluster  # 클러스터 이름※ 동일한 클러스터

uyijune15.tistory.com

 

Step 5. elasticsearch 실행

# systemctl daemon-reload
# systemctl enable elasticsearch
# systemctl start elasticsearch

 

Step 6. 확인

# curl localhost:9200

 

[참고] 공식 홈페이지

https://www.elastic.co/guide/en/elasticsearch/reference/current/rpm.html

 

Install Elasticsearch with RPM | Elasticsearch Guide [8.13] | Elastic

On systemd-based distributions, the installation scripts will attempt to set kernel parameters (e.g., vm.max_map_count); you can skip this by masking the systemd-sysctl.service unit.

www.elastic.co

 

 

(ㄴ) logstash

Step 1. 저장소(repository) 정의 파일 생성 및 작성

(파일명) logstash.repo

# vi /etc/yum.repos.d/logstash.repo

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

[logstash-7.x]  
name=Elastic repository for 7.x packages 
baseurl=https://artifacts.elastic.co/packages/7.x/yum

gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Step 2. logstash 설치

# yum install logstash -y

 

Step 3. logstash 실행

# systemctl enable logstash

# systemctl start logstash

 

Step 4. 파이프라인 작성

(파일명) logstash.conf

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

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

input {
  beats {
    port => 5044
  }
}

filter {
  date {
    match => [ "timestamp", "ISO8601" ]
    target => "@timestamp"
  }
  mutate {
    add_field => { "[@metadata][index]" => "logs-%{+YYYY.MM.dd}" }
  }
}

output {
  elasticsearch {
    hosts => ["http://192.168.112.202:9200"]
    index => "%{[@metadata][index]}"
  }
  stdout { codec => rubydebug }
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

 

[참고] https://uyijune15.tistory.com/192

 

[ELK Stack] logstash.conf, filebeat.yml

(파일명) logstash.conf# vi /etc/logstash/conf.d/logstash.conf (예시 1) 단일 인덱스-----------------------------------------------------------------------------------------------------------------------------------------------------------------in

uyijune15.tistory.com

 

Step 5. Logstash 시스템 서비스로 설치

# cd /usr/share/logstash/bin
# ./system-install

 

 

(ㄷ) kibana

Step 1. 저장소(repository) 정의 파일 생성 및 작성

(파일명) kibana.repo

# vi /etc/yum.repos.d/kibana.repo

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

[kibana-7.x]
name=Kibana repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Step 2. kibana 설치

# yum install kibana -y

 

Step 3. kibana 실행

# systemctl enable kibana

# systemctl start kibana

 

Step 4. 파일 설정

(파일명) kibana.yml

# vi /etc/kibana/kibana.yml

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

server.port: 5601 ※ 5601 연결 안되어서 5602로 진행했습니다.

server.host: "0.0.0.0"

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Step 5. 방화벽 설정

# firewall-cmd --permanent --add-port=5601/tcp

# firewall-cmd  --reload

# firewall-cmd --list-all

 

Step 6. kibana 대시보드 접속

(브라우저) http://<server ip>:5601

(콘솔)

# curl -v 192.168.112.202:5601

 

 

(ㄹ) filebeat

Step 1. filebeat 설치

# curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.14.0-x86_64.rpm
# rpm -vi filebeat-7.14.0-x86_64.rpm

 

Step 2. filebeat 실행

# systemctl enable filebeat

# systemctl start filebeat

 

Step 3. 파일 설정

(파일명) filebeat.yml

# vi /etc/filebeat/filebeat.yml

# cat /etc/filebeat/filebeat.yml | grep -v "#" | grep -v '^$'

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/*.log

output.logstash:
  hosts: ["localhost:5044"]

logging.level: info
logging.to_files: true
logging.files:
  path: /var/log/filebeat
  name: filebeat
  keepfiles: 7
  permissions: 0644

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

 

[참고] https://uyijune15.tistory.com/192

 

[ELK Stack] logstash.conf, filebeat.yml

(파일명) logstash.conf# vi /etc/logstash/conf.d/logstash.conf (예시 1) 단일 인덱스-----------------------------------------------------------------------------------------------------------------------------------------------------------------in

uyijune15.tistory.com

 

Step 4. filebeat 재실행

# systemctl restart filebeat

※ 수정된 파일을 적용시키기 위해서 재실행합니다.

 

테스트

  • 인덱스 (Index): Elasticsearch에서 데이터 저장하는 실제 컨테이너
  • 인덱스 패턴 (Index Pattern): Kibana에서 여러 인덱스 그룹화하여 관리하고 시각화하기 위한 패턴

Step 1. 인덱스 생성

※ 인덱스 생성 과정은 필요할 경우합니다. 위의 코드의 경우 자동으로 인덱스 생성됩니다.

(경로) (Management) Dev Tools

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

PUT /logs-2024.05.28
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "timestamp": {
        "type": "date"
      },
      "message": {
        "type": "text"
      },
      "level": {
        "type": "keyword"
      }
    }
  }
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

Step 2. 인덱스 패턴 생성

(경로) (Management) Stack Management > (Kibana) Index Patterns > Create index pattern

 

Step 2. 로그 확인

(경로) (Analytics) Discover

 

[참고] 감쟈의 엔지니어 일지

https://potato-yong.tistory.com/140

https://potato-yong.tistory.com/142

 

'여러가지 > 구축 & 설치' 카테고리의 다른 글

[ELK Stack] elasticsearch.yml  (0) 2024.05.29
[ELK Stack] .repo  (0) 2024.05.29
[Linux] 캐시 삭제  (0) 2024.04.25
[Linux] IP 변경 방법  (0) 2024.04.25
[설치] python3 - CentOS7  (0) 2024.02.13