상세 컨텐츠

본문 제목

fluentd 설치 및 설정

Ops/Monitoring

by 크리두 2020. 4. 7. 11:45

본문

반응형

<fluentd 설치 및 도커화>

https://sketchofcreed.tistory.com/entry/fluentd-%EC%84%A4%EC%B9%98-%EB%B0%8F-%EB%8F%84%EC%BB%A4%ED%99%94-CentOS-7

 

fluentd 설치 (CentOS 7)

공식 사이트 https://www.fluentd.org/ Fluentd | Open Source Data Collector "Logs are streams, not files. I love that Fluentd puts this concept front-and-center, with a developer-friendly approach for..

sketchofcreed.tistory.com

위 링크 참조하여 설치는 간단히 진행할 수 있다.

 

이번 포스팅에서는 설치 후 필요한 설정을 하려고 한다.

configuration 부분에 대한 이해를 위해 최대한 설명하고 기록해야겠다.

 

 

<구성도>

 

 

  • Nginx Cache Server의 로그를 fluentd에서 Aggregator 서버로 전송
    • log 파싱 형태는 다음과 같이 설정 함
    • 아래는 nginx 서버 설정
log_format main '$host        $logdate        $logdate2       $scheme://$host$request_uri     $bytes_sent   $request_time    $upstream_cache_status  $status $remote_addr    $http_user_agent        $http_referer   $upstream_addr $upstream_status        $geoip_country_code   $geoip_org        $http_accept_encoding   $body_bytes_sent       $request_length $time_iso8601   $upstream_connect_time  $upstream_header_time   $upstream_response_time';

 

 

 

<conf 파일>

 

크게 6가지로 구분되어 있음.

 

 

Input Plugins

 

docs.fluentd.org

  • match : output 목적지 설정
    •  
  • filter : 프로세싱 pipeline 설정
  • system : system wide configuration
    • log_level
    • suppress_repeated_stacktrace
    • emit_error_log_interval
    • suppress_config_dump
    • without_source
    • process_name (only available in system directive. No fluentd option)
  • label : 내부 라우팅을 위한 output과 필터 그룹 설정
  • @include : 다른 파일 포함 설정
<system>
  root_dir /var/log/td-agent         # For handling unrecoverable chunks
</system>
 
<source>
  @type tail
  path /logdata/access/*.com.log,/logdata/access/*.net.log,/logdata/access/*.kr.log
  exclude_path ["/logdata/access/*.gz", "/logdata/access/*.zip" ]
  pos_file /var/log/td-agent/nginx-access.log.pos
  tag nginx.raw
  refresh_interval 1s
  <parse>
    @type tsv
     keys domain,date,time,url,sent,res,ret,status,cip,agent,referer,upip,up_status,geo,geoorg,encoding,length,reqlen,timestamp,up1,up2,up3
     time_key timestamp
     types sent:integer, res:float, reqlen:integer, up1:float, up2:float, up3:float
  </parse>
 
</source>
 
<filter nginx.raw>
  @type record_modifier
  remove_keys date, time, encoding, length
  <record>
    host "#{Socket.gethostname}"
  </record>
  <replace>
    key agent
    expression /^$/
    replace -
  </replace>
  <replace>
    key referer
    expression /^$/
    replace -
  </replace>
</filter>
 
<match nginx.*>
  @type copy
  <store>
    @type relabel
    @label @ERROR
  </store>
  <store>
    @type relabel
    @label @SAMPLING
  </store>
</match>
 
 
<label @ERROR>
 <match nginx.raw>
  @type rewrite_tag_filter
  capitalize_regex_backreference yes
  <rule>
    key status
    pattern /^(4|5)\d{2}$/
    tag nginx.error
  </rule>
 </match>
 
#  <match **>
#     @type stdout
#  </match>
 
  <match nginx.*>
    @type forward
     <server>
        host xxx.xxx.xxx.xxx
        port 24224
        weight 60
     </server>
     <server>
        host xxx.xxx.xxx.xxx
        port 24224
        weight 60
#        standby
     </server>
    heartbeat_type transport
    <buffer>
      flush_interval 1
      retry_wait 1                      # The wait interval for the first retry.
      retry_exponential_backoff_base 2  # Inclease the wait time by a factor of N.
      retry_type exponential_backoff    # Set 'periodic' for constant intervals.
      retry_randomize true              # Apply randomization. (see above)
      retry_timeout 72h                 # Maximum duration before giving up.
      retry_forever false               # Set 'true' for infinite retry loops.
      retry_secondary_threshold 0.8     # See the "Secondary Output" section in
    </buffer>
    <secondary>
      @type file
      path /var/log/td-agent/buffer/nginx.error
      compress gzip
    </secondary>
  </match>
</label>
 
<label @SAMPLING>
  <filter **>
    @type sampling
    interval 10
  </filter>
 
#  <match **>
#     @type stdout
#  </match>
 
  <match nginx.*>
    @type forward
     <server>
        host xxx.xxx.xxx.xxx
        port 24224
        weight 60
     </server>
     <server>
        host xxx.xxx.xxx.xxx
        port 24224
        weight 60
#        standby
     </server>
    heartbeat_type transport
    <buffer>
      flush_interval 1
      retry_wait 1                      # The wait interval for the first retry.
      retry_exponential_backoff_base 2  # Inclease the wait time by a factor of N.
      retry_type exponential_backoff    # Set 'periodic' for constant intervals.
      retry_randomize true              # Apply randomization. (see above)
      retry_timeout 72h                 # Maximum duration before giving up.
      retry_forever false               # Set 'true' for infinite retry loops.
      retry_secondary_threshold 0.8     # See the "Secondary Output" section in
    </buffer>
    <secondary>
      @type file
      path /var/log/td-agent/buffer/nginx.sampled
      compress gzip
    </secondary>
  </match>
</label>

 

<source> 부분

<source>
  @type tail
  path /logdata/access/*.com.log,/logdata/access/*.net.log,/logdata/access/*.kr.log
  exclude_path ["/logdata/access/*.gz", "/logdata/access/*.zip" ]
  pos_file /var/log/td-agent/nginx-access.log.pos
  tag nginx.raw
  refresh_interval 1s
  <parse>
    @type tsv
     keys domain,date,time,url,sent,res,ret,status,cip,agent,referer,upip,up_status,geo,geoorg,encoding,length,reqlen,timestamp,up1,up2,up3
     time_key timestamp
     types sent:integer, res:float, reqlen:integer, up1:float, up2:float, up3:float
  </parse>
 
</source>

 

- @type tail

  tail 사용하여 log를 읽어 들여 동작 시작

 

- path <logfile path>

  로그 파일 경로 설정

 

- exclude_path []

  제외할 파일들 지정, 예를 들어 압축파일인 경우

path /path/to/*
exclude_path ["/path/to/*.gz", "/path/to/*.zip"]

- pos_file

  fluentd가 여러 소스들을 pos_file 하나로 다루는 기능

  나중에 pos_file을 통해 기록을 하게 된다.

 

- tag

  tag 내용을 붙여서 파싱처리 함

 

- refresh_interval 60s (default)

  path로 지정된 리스트 파일 읽는 주기 설정

 

parse 부분

  • type : tsv 등 type 설정
    • 참고 : tab-separated values (tsv)
    • tsv는 keys <파라미터> 나열해서 설정
  • types (hash)
    • string-based
      • field1:type, field2:type, field3:type:option, field4:type:option
    • json-based
      • {"field1":"type", "field2":"type", "field3":"type:option", "field4":"type:option"}

 

< filter 부분 >

<filter nginx.raw>
  @type record_modifier
  remove_keys date, time, encoding, length
  <record>
    host "#{Socket.gethostname}"
  </record>
  <replace>
    key agent
    expression /^$/
    replace -
  </replace>
  <replace>
    key referer
    expression /^$/
    replace -
  </replace>
</filter>

- @type record_modifier 

 

 

 

  • config 파일 체크 ( --dry-run 옵션 추가)
fluentd --dry-run -c fluent.conf

 

<plugin 설치>

td-agent-gem install fluent-plugin-sampling-filter
td-agent-gem install fluent-plugin-rewrite-tag-filter

 

 

반응형

관련글 더보기

댓글 영역