본문 바로가기

NHN Cloud

NHN Cloud 에서 Log Pipeline 만들기 (1)

반응형

1. 개요

이 문서에서는 다수의 서버에서 발생하는 실시간 로그를 수집하여 다양한 서비스 및 사용자에게 제공하기 위한 Log pipeline을 구축하는 방법을 안내합니다. 이를 위해 NHN Cloud Native 서비스를 활용하여 Log pipeline을 구성하고, 정상적으로 로그를 조회할 수 있는지 확인합니다.

 

2. Architecture

 

 

아래는 Log pipeline의 플로우에 대해 설명합니다.

  1. 로그 수집: 각 서버에 설치된 Logstash를 사용하여 로그를 수집합니다. Logstash는 다양한 소스에서 로그를 수집하고 가공하여 Log & Crash Search에 전송합니다.
  2. 로그 저장: Log & Crash Search는 수집된 로그를 저장하는 중앙화된 저장소입니다. Logstash에서 전송된 로그는 Log & Crash Search에서 색인되어 검색이 가능하도록 관리됩니다.
  3. 장기 보관: 로그를 장기간 보관하기 위해 DataFlow 서비스를 사용합니다. DataFlow는 로그를 수집하여 Object Storage에 안정적으로 저장합니다. 이를 통해 오랜 기간 동안 로그를 보관하고 필요할 때에 접근할 수 있습니다.
  4. 로그 조회: 저장된 로그를 조회하기 위해 DataQuery 서비스를 활용합니다. DataQuery는 Object Storage에 저장된 로그 데이터를 검색하고 원하는 시간대나 조건에 맞는 로그를 조회할 수 있습니다.

 

위의 아키텍처를 통해 Logstash를 통해 로그를 수집하고 Log & Crash Search에 저장한 뒤, DataFlow와 Object Storage를 사용하여 장기 보관하고 DataQuery를 통해 로그를 효율적으로 조회할 수 있습니다.

 

3. 필요한 서비스 정보

Log pipeline을 구축하기 위해 필요한 NHN Cloud Native 서비스를 확인합니다. 다음은 필수적인 서비스 목록입니다.

 

  • Log & Crash Search: 클라이언트와 서버의 로그를 수집하여 사용자가 원하는 로그를 검색하고 조회할 수 있습니다.
  • DataFlow: ETL(extract-transform-load) 데이터 플로우를 생성할 수 있습니다.
  • DataQuery: 표준 SQL 쿼리를 통해 Object Storage에 저장된 데이터를 조회할 수 있습니다.

 

4. 구성

4.1 Log & Crash Search

Logstash SDK를 설치하고 실행하여 LNCS(Log & Crash Search)서비스로 로그를 전송하는 방법을 가이드합니다.

 

4.1.1. Logstash 다운로드

로그를 수집할 서버에 Logstash를 다운로드하고 설치를 진행합니다.

가이드에 제공된 버전은 최신버전(8.8)보다 현저히 낮은 1.5버전으로 Logstash 공식 홈페이지를 통해 최신버전으로 다운로드합니다.

 

# OS: Ubuntu20.04LTS

wget -qO - <https://artifacts.elastic.co/GPG-KEY-elasticsearch> | sudo gpg --dearmor -o /usr/share/keyrings/elastic-keyring.gpg
sudo apt-get install apt-transport-https
echo "deb [signed-by=/usr/share/keyrings/elastic-keyring.gpg] <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 logstash

 

4.1.2 Logstash 설정

Logstash를 이용하여 로그를 수집하고 Log & Crash search 서비스의 Collector로 전송합니다.

Logstash가 설치 된 폴더에서 config 파일을 생성합니다.

 

# 최신버전의 logstash 설치시 “/usr/share/logstash” 경로에 설치됩니다.

cd /usr/share/logstash
vim apache-logs.conf

 

더보기

vim apache-logs.conf

 

input { # input plugin
  file {
    path => "/var/log/apache2/access.log" # log가 적재되는 경로 입력(반드시 절대경로로 입력)
    type => "apache-access" # 사용자 임의설정 가능
  }
}

filter {
   if [type] == "apache-access" {
     grok {
       match => { "message" => "%{COMBINEDAPACHELOG}" }
     }

    mutate {
      remove_field => [ "@version", "@timestamp", "path", "tags", "timestamp"] # remove할 필드를 설정
      rename => {
        "message" => "body"
        "host" => "host"
      }
      add_field => {
        "projectName" => "ditKA4OZ1aHKc2og" #필수. Log & Crash Search서비스의 Appkey입력
        "projectVersion" => "0.0.1" #필수. 프로젝트 버전 입력(사용자 임의설정 가능)
        "logVersion" => "v2" #필수. 로그 포맷 버전(사용자 임의설정 가능)
        "logType" => "logstash" #옵션
        "logSource" => "collector" #옵션
        "logLevel" => "INFO" #옵션
      }
    }
  }
}

output {
  http {
    url => "https://api-logncrash.cloud.toast.com/v2/log" # NHN Cloud LNCS API주소
    http_method => "post"
    format => "json"
    ssl_verification_mode => none
    headers => {
      "content-type" => "application/json"
    }
  }
}

 

4.1.3 Logstash 실행

-f옵션을 통해 위에서 생성한 conf 파일을 지정하여 실행합니다.

# 현재경로에 apache-logs.conf파일이 없다면 절대경로로 입력합니다.

bin/logstash -f apache-logs.conf

 

더보기

실행결과

 

$ /usr/share/logstash/bin/logstash -f apache-logs.conf
Using bundled JDK: /usr/share/logstash/jdk
WARNING: Could not find logstash.yml which is typically located in $LS_HOME/config or /etc/logstash. You can specify the path using --path.settings. Continuing using the defaults
Could not find log4j2 configuration at path /usr/share/logstash/config/log4j2.properties. Using default config which logs errors to the console
[WARN ] 2023-06-02 13:27:25.626 [main] runner - NOTICE: Running Logstash as superuser is not recommended and won't be allowed in the future. Set 'allow_superuser' to 'false' to avoid startup errors in future releases.
[INFO ] 2023-06-02 13:27:25.636 [main] runner - Starting Logstash {"logstash.version"=>"8.8.0", "jruby.version"=>"jruby 9.3.10.0 (2.6.8) 2023-02-01 107b2e6697 OpenJDK 64-Bit Server VM 17.0.7+7 on 17.0.7+7 +indy +jit [x86_64-linux]"}
[INFO ] 2023-06-02 13:27:25.638 [main] runner - JVM bootstrap flags: [-Xms1g, -Xmx1g, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djruby.compile.invokedynamic=true, -XX:+HeapDumpOnOutOfMemoryError, -Djava.security.egd=file:/dev/urandom, -Dlog4j2.isThreadContextMapInheritable=true, -Djruby.regexp.interruptible=true, -Djdk.io.File.enableADS=true, --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED, --add-opens=java.base/java.security=ALL-UNNAMED, --add-opens=java.base/java.io=ALL-UNNAMED, --add-opens=java.base/java.nio.channels=ALL-UNNAMED, --add-opens=java.base/sun.nio.ch=ALL-UNNAMED, --add-opens=java.management/sun.management=ALL-UNNAMED]
[WARN ] 2023-06-02 13:27:25.906 [LogStash::Runner] multilocal - Ignoring the 'pipelines.yml' file because modules or command line options are specified
[INFO ] 2023-06-02 13:27:26.968 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600, :ssl_enabled=>false}
[INFO ] 2023-06-02 13:27:28.320 [Converge PipelineAction::Create<main>] Reflections - Reflections took 327 ms to scan 1 urls, producing 132 keys and 464 values
[INFO ] 2023-06-02 13:27:28.926 [Converge PipelineAction::Create<main>] javapipeline - Pipeline `main` is configured with `pipeline.ecs_compatibility: v8` setting. All plugins in this pipeline will default to `ecs_compatibility => v8` unless explicitly configured otherwise.
[WARN ] 2023-06-02 13:27:28.973 [[main]-pipeline-manager] grok - ECS v8 support is a preview of the unreleased ECS v8, and uses the v1 patterns. When Version 8 of the Elastic Common Schema becomes available, this plugin will need to be updated
[INFO ] 2023-06-02 13:27:29.139 [[main]-pipeline-manager] javapipeline - Starting pipeline {:pipeline_id=>"main", "pipeline.workers"=>2, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>50, "pipeline.max_inflight"=>250, "pipeline.sources"=>["/usr/share/logstash/apache-logs.conf"], :thread=>"#<Thread:0x765edf76@/usr/share/logstash/logstash-core/lib/logstash/java_pipeline.rb:134 run>"}
[INFO ] 2023-06-02 13:27:30.182 [[main]-pipeline-manager] javapipeline - Pipeline Java execution initialization time {"seconds"=>1.04}
[INFO ] 2023-06-02 13:27:30.199 [[main]-pipeline-manager] file - No sincedb_path set, generating one based on the "path" setting {:sincedb_path=>"/usr/share/logstash/data/plugins/inputs/file/.sincedb_8636a19711465cc96926000984eb4005", :path=>["/var/log/apache2/access.log"]}
[INFO ] 2023-06-02 13:27:30.207 [[main]-pipeline-manager] javapipeline - Pipeline started {"pipeline.id"=>"main"}
[INFO ] 2023-06-02 13:27:30.215 [[main]<file] observingtail - START, creating Discoverer, Watch with file and sincedb collections
[INFO ] 2023-06-02 13:27:30.223 [Agent thread] agent - Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}

 

Log & Crash Search에서 아래와 유사하게 로그가 쌓이는지 확인합니다.

Logstash가 실행 된 이후의 로그만 Log & Crash Search에 쌓이게 됩니다.

 

 

4.2 DataFlow

DataFlow를 통해 Log & Crash Search(이하 LNCS)에 적재된 Log를 Object Storage(이하 OBS)로 전송하는 방법을 가이드합니다.

 

필요한 정보

  • LNCS(Log & Crash Search)의 Appkey
  • OBS(Object Strage)의 Access Key, Secret Key, 버킷 네임

 

4.2.1 Data Flow생성

플로우 템플릿을 LNCS to OBS로 생성합니다.

 

 

4.2.2 LNCS, OBS 각 노드별 설정 진행

미리 준비한 LNCS의 Appkey를 입력합니다.

 

 

OBS도 마찬가지로 버킷네임, AccessKey, SecretKey를 입력합니다.

 

 

두 노드 모두 Codec 설정을 통해 codec: json과 charset: UTF-8로 설정합니다.

 

 

4.2.3 플로우 저장

우측 상단 “플로우저장” 버튼을 클릭해 플로우 정보를 저장합니다.

 

 

💡 설정 변경 시에는 플로우 저장 버튼을 필수로 해야합니다.

 

4.2.4 플로우시작

 

 

💡 설정이 변경 되었다면 플로우를 재시작해야 합니다. 재시작 동안에는 로그가 유실 될 수 있습니다.

 

4.2.5 DataFlow로그 확인

DataFlow의 실시간 로그를 확인하며 DataFlow서비스의 진행상태와 로그 전송 결과를 모니터링 할 수 있습니다.

 

 

4.2.6 OBS 적재로그 확인

OBS 에서 DataFlow서비스를 통해 LNCS에 적재된 로그가 OBS로 잘 전송된 것을 확인할 수 있습니다.

💡 참고로 객체 다운로드는 1개씩만 가능합니다.

 

 

반응형