본문 바로가기
Infra2024년 7월 3일5분 읽기

Docker 로그 관리 전략 — 로테이션과 중앙 수집

YS
김영삼
조회 641

Docker 로그의 기본 동작

Docker는 기본적으로 json-file 로깅 드라이버를 사용하며, 컨테이너의 stdout/stderr를 JSON 형식으로 호스트 디스크에 저장합니다. 별도 설정 없이 운영하면 로그 파일이 무한히 커져 디스크를 가득 채울 수 있습니다.

docker inspect --format='{{.LogPath}}' my-container
du -sh /var/lib/docker/containers/*/
docker system df -v

로그 로테이션 설정

전역 설정 (daemon.json)

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "50m",
    "max-file": "3",
    "compress": "true",
    "labels": "service,environment"
  }
}

컨테이너별 설정

services:
  app:
    image: my-app:latest
    logging:
      driver: json-file
      options:
        max-size: "100m"
        max-file: "5"
        tag: "{{.Name}}/{{.ID}}"

  nginx:
    image: nginx:alpine
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "3"

기존 로그 정리

truncate -s 0 $(docker inspect --format='{{.LogPath}}' my-container)

for f in /var/lib/docker/containers/*/*-json.log; do
  truncate -s 0 "$f"
done

docker system prune -f

Fluentd로 중앙 로그 수집


  @type forward
  port 24224
  bind 0.0.0.0



  @type elasticsearch
  host elasticsearch
  port 9200
  logstash_format true
  logstash_prefix docker-logs

  
    @type file
    path /fluentd/buffer
    flush_interval 5s
    retry_max_interval 30s
    chunk_limit_size 8m
  

Docker에서 Fluentd 드라이버 사용

services:
  app:
    image: my-app:latest
    logging:
      driver: fluentd
      options:
        fluentd-address: "localhost:24224"
        fluentd-async: "true"
        tag: "docker.{{.Name}}"
        fluentd-retry-wait: "1s"
        fluentd-max-retries: "30"

  fluentd:
    image: fluent/fluentd:v1.16-1
    ports:
      - "24224:24224"
    volumes:
      - ./fluent.conf:/fluentd/etc/fluent.conf
      - fluentd-buffer:/fluentd/buffer

로깅 드라이버 비교

드라이버용도docker logs 지원특징
json-file기본값O로컬 파일 저장
local기본 대안O더 빠르고 효율적
fluentd중앙 수집X유연한 라우팅
gelfGraylogXUDP 전송
awslogsCloudWatchXAWS 네이티브
gcplogsCloud LoggingXGCP 네이티브

구조화된 로그 출력

const pino = require('pino');
const logger = pino({
  level: process.env.LOG_LEVEL || 'info',
  transport: process.env.NODE_ENV !== 'production'
    ? { target: 'pino-pretty' }
    : undefined
});

logger.info({
  requestId: 'abc-123',
  method: 'GET',
  path: '/api/users',
  duration: 45,
  statusCode: 200
}, 'Request completed');

운영 체크리스트

  • 모든 컨테이너에 max-size와 max-file 로그 로테이션이 설정되어 있는지 확인하세요.
  • 프로덕션 환경에서는 fluentd 또는 클라우드 네이티브 로깅 드라이버로 중앙 수집하세요.
  • 앱 레벨에서 JSON 형식의 구조화된 로그를 출력하면 검색과 분석이 훨씬 수월합니다.
  • docker system df를 주기적으로 모니터링하여 디스크 사용량을 관리하세요.

댓글 0

아직 댓글이 없습니다.
Ctrl+Enter로 등록