본문 바로가기
카테고리 없음

모니터링 시스템 구축 및 서버 로그 분석

by 백수A 2025. 3. 30. 15:47

모니터링 시스템 구축 및 서버 로그 분석

1. 문제 상황: 서버 장애 감지 및 대응의 어려움

운영 중인 서비스에서 다음과 같은 문제가 발생했다.

  • 서버가 갑자기 응답하지 않거나 속도가 느려지는 현상이 발생함
  • 문제 발생 시 원인을 즉시 파악하기 어려움
  • 사용자들이 불편함을 겪기 전 장애를 미리 감지할 방법이 없음
  • 서버 리소스 사용량을 실시간으로 파악할 수 없음

이러한 문제를 해결하기 위해 서버와 애플리케이션의 상태를 실시간으로 모니터링하는 시스템을 구축하고, 로그 데이터를 분석하여 장애 원인을 빠르게 파악하는 것이 필요하다.

2. 해결 과정: 모니터링 시스템 구축

2.1 모니터링 시스템 개요

서버 및 애플리케이션 모니터링 시스템을 구축하면 다음과 같은 이점이 있다.

  • CPU, 메모리, 네트워크 사용량 등을 실시간으로 확인 가능
  • 응답 시간, 오류율 등을 측정하여 서비스 품질 유지
  • 이상 징후 발생 시 자동으로 알람을 받을 수 있음
  • 장애 발생 시 원인을 빠르게 파악하여 대응할 수 있음

이를 위해 Prometheus, Grafana, ELK Stack(Elasticsearch, Logstash, Kibana) 등의 도구를 활용할 수 있다.

2.2 Prometheus & Grafana를 활용한 서버 모니터링

1) Prometheus 설치

Prometheus는 시계열(time-series) 데이터를 수집하여 모니터링하는 오픈소스 도구이다.

wget https://github.com/prometheus/prometheus/releases/latest/download/prometheus-*.linux-amd64.tar.gz
tar xvf prometheus-*.linux-amd64.tar.gz
cd prometheus-*
./prometheus --config.file=prometheus.yml

2) Prometheus 설정

Prometheus의 설정 파일(prometheus.yml)을 수정하여 모니터링할 대상 서버를 지정한다.

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9090']

3) Grafana 설치 및 연동

Grafana는 Prometheus에서 수집한 데이터를 시각화하는 도구이다.

sudo apt update
sudo apt install -y grafana
sudo systemctl start grafana-server
sudo systemctl enable grafana-server

설치 후 웹 브라우저에서 http://localhost:3000에 접속하여 Prometheus 데이터를 시각화할 수 있다.

2.3 ELK Stack을 활용한 로그 분석

로그 데이터를 효과적으로 분석하기 위해 Elasticsearch, Logstash, Kibana(ELK Stack)를 사용할 수 있다.

1) Elasticsearch 설치

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.0.0-linux-x86_64.tar.gz
tar -xvzf elasticsearch-8.0.0-linux-x86_64.tar.gz
cd elasticsearch-8.0.0
./bin/elasticsearch

2) Logstash 설정

Logstash는 로그 데이터를 수집하여 Elasticsearch로 전달하는 역할을 한다.

input {
  file {
    path => "/var/log/syslog"
    start_position => "beginning"
  }
}

output {
  elasticsearch {
    hosts => ["localhost:9200"]
    index => "server-logs"
  }
}

설정 후 Logstash를 실행한다.

bin/logstash -f logstash.conf

3) Kibana 실행

Kibana는 Elasticsearch에 저장된 로그 데이터를 시각화하는 도구이다.

./bin/kibana

웹 브라우저에서 http://localhost:5601에 접속하여 로그를 분석할 수 있다.

2.4 장애 발생 시 자동 알림 설정

장애 발생 시 빠르게 대응하기 위해 알람 시스템을 설정할 수 있다.

1) Prometheus Alertmanager 설정

wget https://github.com/prometheus/alertmanager/releases/latest/download/alertmanager-*.linux-amd64.tar.gz
tar xvf alertmanager-*.linux-amd64.tar.gz
cd alertmanager-*
./alertmanager --config.file=alertmanager.yml

설정 파일(alertmanager.yml)을 수정하여 이메일 또는 Slack으로 알람을 받을 수 있다.

route:
  receiver: 'email-alert'

receivers:
  - name: 'email-alert'
    email_configs:
      - to: 'admin@example.com'
        from: 'monitoring@example.com'
        smarthost: 'smtp.example.com:587'
        auth_username: 'user'
        auth_password: 'password'

3. 최종 정리

서버 모니터링과 로그 분석 시스템을 구축하면 서비스 운영 중 발생하는 문제를 사전에 감지하고 신속하게 대응할 수 있다.

핵심 요약:

  • Prometheus와 Grafana를 활용하면 실시간으로 서버 리소스 사용량을 모니터링할 수 있다.
  • ELK Stack을 사용하면 서버 및 애플리케이션 로그를 효과적으로 분석할 수 있다.
  • Alertmanager를 설정하면 장애 발생 시 자동으로 알람을 받을 수 있다.
  • 이러한 시스템을 구축하면 장애 대응 속도가 빨라지고, 서비스 안정성을 높일 수 있다.

모니터링 시스템을 구축하면 사전에 문제를 감지하고 대응할 수 있어, 서비스 운영의 효율성이 크게 향상된다.