Skip to content

Kubernetes Log Pipeline: From OpenTelemetry Collector to Loki

Interactive Diagram

새 탭 ↗

By default, Kubernetes container logs go to stdout/stderr, and kubelet writes them to files on the node under /var/log/pods/<namespace>_<pod>_<uid>/<container>/*.log. This environment collects directly from that path.

Because logs are generated independently per node, collection has to happen per node too. Regardless of which node an application pod lands on, a log agent must already be running there to tail without gaps, which is why otel-loki is deployed as a 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 targets every pod log, but workloads that could create a collection loop, such as the Collector’s own logs, are excluded at a category level. The container operator parses the CRI log format (logtag, timestamp, stream) into normalized fields.

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 attaches pod metadata via the kubelet API, and the resource processor selects a subset of it as Loki label candidates. Which attributes get promoted to labels is the key decision covered next.

Loki label design: why high-cardinality labels are avoided

Section titled ā€œLoki label design: why high-cardinality labels are avoidedā€

Loki creates a separate stream for every label combination. Putting a field with unbounded value variety (request IDs, timestamps) into a label triggers cardinality explosion, blowing up ingester memory and index size together.

So this pipeline only promotes namespace, pod, and container to loki.resource.labels, leaving everything else (request path, trace ID, etc.) in the log line body to be queried with LogQL line filters like |= and | json. It’s a trade-off: keep label count low, pay the filtering cost at query time instead.

  • Promoted to labels: namespace, pod, container, node
  • Not promoted: request ID, user ID, arbitrary dynamic values

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

PlantUML Diagram
  • distributor, ingester, querier, query-frontend, query-scheduler, index-gateway, and gateway are each deployed as separate workloads, mostly at 1 replica.
  • The schema is TSDB v13, backed by Azure Blob object storage.
  • In this snapshot, authentication is disabled, and the ruler/canary/extra cache layers are not used, a simplification choice for the development environment.
# Loki storage/schema configuration (generalized)
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

The 24h retention reflects a judgment call for this development environment: ā€œbeing able to quickly debug a recent issue is enough.ā€ Workloads that need long-term retention or auditing would require a separate retention policy (e.g., per-namespace overrides), and this pipeline deliberately kept the label scheme simple to leave room for that extension later.

  • Node collector failure: if a node’s otel-loki pod dies, only that node’s logs are lost. Other nodes are unaffected, the benefit of the DaemonSet layout.
  • Backend transient failure: the batch processor’s buffer is finite, so if the Loki gateway stops responding beyond some threshold, the oldest batches can be dropped. This environment doesn’t configure separate disk buffering.
  • Label explosion: carelessly promoting a dynamic attribute to a label when adding a new workload can spike ingester memory almost instantly. Adding a new label should always go through review.
  • Log arrival: confirm the target namespace/pod’s logs are queryable via the Loki datasource in Grafana Explore.
  • Labels: confirm streams split as expected in the form {namespace="...", pod="..."}.
  • Query: confirm LogQL line filters (|= "error") work correctly after the label-based query.
  • Pod metadata: confirm fields like k8s.pod.name attached by k8sattributes actually appear in the log line.

The metric pipeline continues in Kubernetes Metrics Pipeline: From Exporters and OTel Collector to Mimir.