ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • docker log fluentd 설정
    개발/log(로그)관리 2021. 6. 10. 12:53
    728x90

    1. fluentd란?

    각종 로그를 수집하고 저장할 수 있는 기능을 제공하는 오픈소스 도구로 도커 엔진 컨테이너 로그를 fluentd를 통해 저장할 수 있도록 플로그인을 제공합니다.

    fluentd는 수집된 데이터를 aws s3, HDFS, MongoDB 등 다양한 저장소에 저장할 수 있다는 장점이 있습니다.

    도커 로그를 fluentd를 통해 저장하려고 합니다. 여러 output 중에서 파일 output, mongodb output 설정을 실습으로 하겠습니다.


    2. fluentd 로깅 설정하기

    2-1. docker 설정

    1. fluentd docker 이미지 생성

    1. Dockerfile 생성
    # vi Dockerfile
    
    FROM fluent/fluentd:v1.12-debian-1
    
    # Use root account to use apt
    USER root
    
    # below RUN includes plugin as examples elasticsearch is not required
    # you may customize including plugins as you wish
    RUN buildDeps="sudo make gcc g++ libc-dev" \
     && apt-get update \
     && apt-get install -y --no-install-recommends $buildDeps \
     && sudo gem install fluent-plugin-elasticsearch \
     && sudo gem sources --clear-all \
     && SUDO_FORCE_REMOVE=yes \
        apt-get purge -y --auto-remove \
                      -o APT::AutoRemove::RecommendsImportant=false \
                      $buildDeps \
     && rm -rf /var/lib/apt/lists/* \
     && rm -rf /tmp/* /var/tmp/* /usr/lib/ruby/gems/*/cache/*.gem
    
    COPY fluent.conf /fluentd/etc/
    COPY entrypoint.sh /bin/
    

     

    1. fluentd 이미지 생성
    • custom-fluentd:lates 이미지 생성
    # docker build -t custom-fluentd:latest ./

     

    1. docker log 저장하고 싶은 컨테이너 실행 명령어에 --log로된 3줄을 추가해준다. 
    docker run -d \
    --name nginx \
    --log-driver=fluentd \
    --log-opt fluentd-address=127.0.0.1:24224 \
    --log-opt tag="{{.ImageName}}/{{.Name}}/{{.ID}}" \
    -p 80:80 \
    -v /opt/nginx/sites-enabled:/etc/nginx/sites-enabled \
    -v /opt/nginx/log:/var/log/nginx \
    [생략...]

     

    2-2. output 별 설정

    output 별 설정 방식을 알아봅니다. 여기서는 파일로 저장하는 방식과 mongodb로 저장하는 방식을 알아봅니다.

    1. 파일 output

    • 아래 예시는 path /var/log/fluent 위치에 로그 생성 설정을 하였습니다.
    # vi fluent.conf
    
    <source>
      @type forward
      port 24224
      bind 0.0.0.0
    </source>
    <match docker.**>
      @type file
      path /var/log/fluent
      time_slice_format %Y%m%d
      time_slice_wait 10m
      compress gzip
    </match>

    2. mongodb output

    • 몽고디비 output를 사용하는 경우 아래 설정으로 진행합니다.
    # vi fluent.conf
    
    <source>
      @type forward
    </source>
    
    <match docker.**>
      @type mongo
      database [database]
      collection [컬렉션 명]
      host [mnongdb host ip]
      port [mongodb port]
      flush_interval 10s
      user [mongodb user]
      password [mongodb password]
    </match>

     

    2-3. fluentd 컨테이너 실행

    • 이전에 만든 custom-fluentd 이미지를 실행시켜 줍니다.
    • 위에서 만든 fluent.conf를 볼륨으로 연결해줍니다.
    • file output를 사용하는 경우 로그 위치를 볼륨으로 연결해줍니다. 몽고 디비 output인 경우는 해당 볼륨은 지웁니다.
    docker run -d \
    --name fluentd \
    -p 24224:24224 \
    -v [파일 경로]/fluent.conf:/fluentd/etc/fluent.conf \
    -v /opt/fluentd/log:/var/log \
    -e FLUENTD_CONF=fluent.conf \
    custom-fluentd

    참고

    https://docs.fluentd.org/container-deployment/install-by-docker

    댓글

Designed by Tistory.