콘텐츠로 이동

Kubernetes Log Pipeline: OpenTelemetry Collector에서 Loki까지

Interactive Diagram

새 탭 ↗

Kubernetes에서 container log는 기본적으로 stdout/stderr로 나가고, kubelet이 이를 node의 /var/log/pods/<namespace>_<pod>_<uid>/<container>/*.log 경로에 파일로 쌓는다. 이 환경은 이 경로를 그대로 수집 대상으로 삼는다.

log는 node마다 독립적으로 발생하기 때문에 수집도 node 단위여야 한다. application pod가 어느 node에 스케줄되는지와 무관하게 log agent가 항상 그 node에 존재해야 누락 없이 tailing할 수 있다. 그래서 otel-loki는 DaemonSet으로 배포된다.

receivers:
filelog:
include:
- /var/log/pods/*/*/*.log
exclude:
- /var/log/pods/*/otel-collector*/*.log
start_at: end
include_file_path: true
include_file_name: false
operators:
- type: container
id: container-parser

include는 모든 pod log를 대상으로 하되, Collector 자기 자신의 log 등 순환 수집이 될 수 있는 Workload는 category 단위로 제외한다. container operator가 CRI log format(logtag, timestamp, stream)을 파싱해 표준화된 필드로 변환한다.

processors:
k8sattributes:
auth_type: serviceAccount
extract:
metadata:
- k8s.namespace.name
- k8s.pod.name
- k8s.container.name
- k8s.node.name
resource:
attributes:
- key: loki.resource.labels
value: "k8s.namespace.name,k8s.pod.name,k8s.container.name"
action: insert
memory_limiter:
check_interval: 5s
limit_percentage: 80
batch:
send_batch_size: 1024
timeout: 5s

k8sattributes가 kubelet API를 통해 pod metadata를 붙이고, resource processor가 그 중 일부만 Loki label 후보로 지정한다. 여기서 어떤 attribute를 label로 승격시킬지가 다음 절의 핵심이다.

Loki label 설계: high-cardinality를 피해야 하는 이유

섹션 제목: “Loki label 설계: high-cardinality를 피해야 하는 이유”

Loki는 label 조합마다 별도 stream을 만든다. label에 request ID나 timestamp처럼 값의 종류가 무한히 늘어나는 필드를 넣으면 stream 수가 폭발(cardinality explosion)하고, ingester 메모리와 index 크기가 함께 터진다.

그래서 이 pipeline은 loki.resource.labels에 namespace, pod, container 정도만 label로 승격시키고, 나머지(요청 경로, trace id 등)는 log line 본문에 남겨 LogQL의 |=, | json 같은 line filter로 조회하게 한다. label 수를 낮게 유지하는 대신 조회 시 필터링 비용을 지불하는 trade-off다.

  • label로 승격: namespace, pod, container, node
  • label로 승격하지 않음: request id, user id, 임의의 동적 값

filelog → k8sattributes → resource → memory_limiter → batch → OTLP HTTP exporter → Loki

PlantUML Diagram
  • distributor, ingester, querier, query-frontend, query-scheduler, index-gateway, gateway가 각각 별도 Workload로 배포되며 대부분 1 replica다.
  • schema는 TSDB v13, object storage는 Azure Blob을 사용한다.
  • 이 snapshot에서는 authentication이 비활성화돼 있고, ruler/canary/추가 cache 계층은 사용하지 않는다. 개발 환경 단순화를 위한 선택이다.
# Loki storage/schema 설정 (일반화)
storage_config:
azure:
account_name: <redacted>
container_name: <redacted>
tsdb_shipper:
active_index_directory: /var/loki/tsdb-index
cache_location: /var/loki/tsdb-cache
schema_config:
configs:
- from: "2024-01-01"
store: tsdb
object_store: azure
schema: v13
index:
prefix: loki_index_
period: 24h
limits_config:
retention_period: 24h

retention 24h는 이 개발 환경에서 “최근 문제를 빠르게 디버깅할 수 있으면 충분하다”는 판단에 따른 값이다. 장기 보존이나 감사 요구가 있는 workload라면 별도 retention 정책(예: namespace별 override)이 필요하며, 이 pipeline은 그 확장을 고려해 label 체계를 미리 단순하게 유지했다.

  • node collector 장애: 특정 node의 otel-loki pod가 죽으면 그 node의 log만 유실된다. 다른 node는 영향받지 않는다. DaemonSet 구조의 이점이다.
  • backend 일시 장애: batch processor의 buffer는 유한하므로, Loki gateway가 일정 시간 이상 응답하지 않으면 오래된 batch부터 drop될 수 있다. 이 환경은 별도 disk buffering을 구성하지 않았다.
  • label explosion: 신규 Workload 추가 시 무심코 dynamic attribute를 label로 승격시키면 순식간에 ingester 메모리가 증가한다. 새 label 추가는 리뷰 대상이다.
  • log arrival: 대상 namespace/pod의 log가 Grafana Explore에서 Loki datasource로 조회되는지 확인
  • label: {namespace="...", pod="..."} 형태로 stream이 예상대로 분리되는지 확인
  • query: LogQL line filter(|= "error")가 label 조회 이후 정상 동작하는지 확인
  • pod metadata: k8sattributes가 붙인 k8s.pod.name 등 field가 실제 log line에 존재하는지 확인

Metric pipeline은 Kubernetes Metric Pipeline: Exporter와 OTel Collector에서 Mimir까지에서 이어진다.