콘텐츠로 이동

2025 04 18

2025-04-18

ELK + Filebeat

  • Filebeat
    • 여러 종류의 로그 파일을 수집하자. input을 통해 로그 파일 경로 지정 가능
    • 이후 logstash 서버로 전송하자
      filebeat.inputs:
      
        - type: log
          paths:
      
            - /home/deploy/project-api*/current/log/access.log
          fields:
            tag: project-play-access
            type: play-access
            project: api1
      
        - type: log
          paths:
      
            - /home/deploy/project-api*/current/log/application.log
          multiline.pattern: ^20[1-9][0-9]
          multiline.negate: true
          multiline.match: after
          fields:
            tag: project-application
            type: application
            project: api1
      
        - type: log
          paths:
      
            - /usr/local/nginx/logs/ssl-access*.log
          fields:
            tag: project-access-ssl
            type: nginx-access
            project: api1
      output.logstash:
        host: logstash-server:5100
        loadbalance: true
      
  • logstash
    • 로그 데이터 필터링 + 처리
      • 로그 타입 별 다른 처리 로직 적용
      • 필드 추출/변환
      • 타임스탬프 변환
      • JSON 파싱
    • 로그 타입별로 다른 ES 인덱스에 저장: {환경}-{로그타입}-{날짜} 포맷
    • logstash.yaml 예시
  • input: 로그스태시 데이터 어디서 - 어떻게 받을지 정의
    input {
      beats {
        port => 7000  # 로그데이터 7000에서 수신
      }
      heartbeat {
        type => "heartbeat"
      }
    }
    
  • filter: 수신한 로그 가공
    • grok 파싱: 로그에서 타임스탬프/추적ID/HTTP/referer/domain/ip addr 등
    • 데이터 변환: elapsedTime -> Integer
    • 시간 처리: 추출한 timestamp 기반
    • 헤더 처리, URL 파싱, 불필요한 필드 제거 등
  • output: 가공한 로그를 ES의 특정 인덱스로 보내거나, 파일에 기록하거나
    output {
      if [type] == "nginx-access" {
        elasticsearch {
          hosts => ["domain.es.com:9200"]
          index => "%{[@metadata][index_prefix]}-nginx-access-%{[@metadata][index_date]}"
        }
      }
    
      else if [type] == "application" {
        elasticsearch {
          hosts => ["domain.es.com:9200"]
          index => "%{[@metadata][index_prefix]}-application-%{[@metadata][index_date]}"
        }
      }
    
      else {
        elasticsearch {
          hosts => ["domain.es.com:9200"]
          index => "%{[@metadata][index_prefix]}-log-%{[@metadata][index_date]}"
        }
      }
    }