> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Centralized Logging for 100 Microservices

> Designing a resilient centralized logging pipeline for 100+ Kubernetes microservices using Fluent Bit, Kafka, stream processing, Elasticsearch for hot search and S3 for long term storage

All right — let's design a resilient centralized logging pipeline for a system running more than one hundred microservices. The goal: reliable collection, durable buffering, scalable processing, and cost-efficient long-term retention so a "noisy" service cannot take the logging stack down.

Example of a noisy service producing many errors:

```text theme={null}
[ERROR] payment-svc: OutOfMemoryError
[ERROR] payment-svc: connection timeout
[ERROR] payment-svc: db pool exhausted
[ERROR] payment-svc: connection refused
[ERROR] payment-svc: goroutine leak detected
[ERROR] payment-svc: heap fragmentation
[ERROR] payment-svc: panic: nil pointer
```

High-level goals

* Make ingestion resilient to spikes.
* Keep recent logs fast to search for on-call and alerts.
* Store all logs durably and cheaply for compliance and deep analysis.
* Maintain low per-node resource usage for log collectors.

Architecture overview — high-level flow

* Hundreds of microservices run on Kubernetes.
* Applications emit structured JSON logs to `stdout`.
* The container runtime / kubelet writes container stdout/stderr to node log files.
* Fluent Bit runs as a DaemonSet on each node, tails container logs, and forwards them in batches.
* Kafka is used as a durable buffer to decouple producers and consumers.
* A stream processor (Logstash, Vector, Kafka Connect, or Kafka Streams) consumes from Kafka, parses and enriches logs, tags errors, and writes to:
  * Elasticsearch (hot tier) for fast search and alerting.
  * S3 (cold tier) for long-term, cost-effective retention.
* Kibana (or equivalent) provides interactive search and dashboards; an alerting engine monitors error patterns and fires alerts.

Why structured logs?

* JSON logs make it straightforward to index and query fields such as `level`, `service`, `trace_id`, `request_id`, and `timestamp`.
* Structured logs enable precise queries, reduce costly regex parsing, and improve enrichment and correlation across traces and metrics.

Example: application emitting structured JSON to stdout

```javascript theme={null}
console.log(JSON.stringify({ level: "error", service: "payment-svc", msg: "db pool exhausted", trace_id: "abcd-1234", ts: "2026-01-01T12:00:00Z" }))
```

Collection on Kubernetes

* Deploy Fluent Bit as a DaemonSet so one lightweight collector runs per node. Fluent Bit has a small memory footprint and is designed for tailing files and forwarding logs efficiently.
* Fluent Bit tails CRI log files (commonly under `/var/log/containers` or CRI-managed paths), enriches with basic container metadata, batches messages, and forwards to Kafka.

Important design decision: use Kafka as a buffer

* Avoid sending logs directly from Fluent Bit to Elasticsearch. Forwarding straight to Elasticsearch couples ingestion spikes to the indexing tier and can overload it during noisy incidents.

<Callout icon="lightbulb" color="#1CB2FE">
  Kafka decouples producers and consumers — allowing Elasticsearch and processors to catch up without being overwhelmed.
</Callout>

<Callout icon="warning" color="#FF6B6B">
  Do not forward high-volume logs directly to Elasticsearch without an intermediary buffer. Indexing systems are not designed to absorb sudden, sustained ingestion spikes and can become unavailable.
</Callout>

Stream processing, enrichment, and tagging

* A stream processor reads from Kafka and performs:
  * Parsing (if logs are plain text) or validation of JSON.
  * Enrichment with Kubernetes pod metadata (via the Kubernetes API or metadata plugins).
  * Trace correlation: attach `trace_id` or join with trace/span data when available.
  * Classification: mark error/severity, extract HTTP status, request path, user id, etc.
  * Routing: send enriched logs to both hot and cold sinks.
* The stream processor should be horizontally scalable and able to checkpoint offsets so it can resume safely after restarts.

Hot vs cold storage — purpose and retention

* Hot tier (Elasticsearch / OpenSearch)
  * Purpose: low-latency search, dashboards, and alerting for recent incidents.
  * Typical retention: \~7 days (customizable based on SLOs and cost).
* Cold tier (S3 or object storage)
  * Purpose: cost-effective long-term retention for compliance, audits, and historical analysis.
  * Typical retention: \~1 year or more depending on compliance requirements.

Recommended component mapping

| Component                 | Role                                            | Example technologies                             |
| ------------------------- | ----------------------------------------------- | ------------------------------------------------ |
| Log collector (DaemonSet) | Lightweight per-node tailing and forwarding     | Fluent Bit                                       |
| Durable buffer            | Decouple producers and consumers; absorb spikes | Apache Kafka                                     |
| Stream processor          | Parse, enrich, tag, and route logs              | Logstash, Vector, Kafka Connect, Kafka Streams   |
| Hot storage               | Fast indexed search for recent logs             | Elasticsearch / OpenSearch                       |
| Cold storage              | Cheap, durable long-term archive                | S3, Google Cloud Storage, Azure Blob             |
| UI / Alerting             | Interactive search and alerting                 | Kibana, Grafana, ElastAlert, OpenSearch Alerting |

Retention and lifecycle (example policy)

| Tier | Storage                | Retention        | Use case                        |
| ---- | ---------------------- | ---------------- | ------------------------------- |
| Hot  | Elasticsearch          | `7 days`         | On-call investigation, alerting |
| Warm | Elasticsearch / frozen | `7–30 days`      | Less frequent searches          |
| Cold | S3 / object store      | `1 year` or more | Audits, deep analysis           |

Putting the pieces together — why this pipeline survives a "bad day"

* Fluent Bit keeps collection efficient and low-overhead on each node.
* Kafka buffers ingestion spikes and provides durable storage so downstream consumers can process at their own pace.
* Stream processors enrich and route logs to both a low-latency hot tier (for alerting) and a durable cold tier (for retention).
* Separation of concerns and buffering ensure that a single noisy service cannot overwhelm Elasticsearch or the UI.

Operational recommendations

* Monitor key metrics for each tier:
  * Fluent Bit: per-node memory, error rates, backoff/retries.
  * Kafka: broker CPU, disk usage, partition lag, consumer lag.
  * Stream processors: processing rate, error queues, retry behavior.
  * Elasticsearch: indexing rate, JVM memory, search latency, node availability.
* Implement backpressure and retry strategies in Fluent Bit and processors.
* Use topic partitioning and meaningful keys in Kafka to keep related logs grouped (e.g., by `service`).
* Keep explicit schema or mappings for hot indices to avoid costly dynamic mapping updates.
* Implement retention lifecycle policies to automate rollover from hot to cold.

Quick troubleshooting checklist for a noisy-service incident

* Check Kafka topic lag: are consumers falling behind?
* Inspect Fluent Bit errors and local buffer usage on nodes.
* Verify stream processor health and retry queues.
* Confirm Elasticsearch cluster health and threadpool rejections.
* If indexing is overwhelmed, stop indexing to hot tier temporarily and rely on Kafka + S3 for durability until capacity is restored.

References and further reading

* Fluent Bit: [https://fluentbit.io/](https://fluentbit.io/)
* Apache Kafka: [https://kafka.apache.org/](https://kafka.apache.org/)
* Elasticsearch: [https://www.elastic.co/elasticsearch/](https://www.elastic.co/elasticsearch/)
* Kibana: [https://www.elastic.co/kibana/](https://www.elastic.co/kibana/)
* Kubernetes logging concepts: [https://kubernetes.io/docs/concepts/cluster-administration/logging/](https://kubernetes.io/docs/concepts/cluster-administration/logging/)
* Cloud storage (S3): [https://aws.amazon.com/s3/](https://aws.amazon.com/s3/)

Summary
This pipeline — Fluent Bit (DaemonSet) → Kafka → stream processor → Elasticsearch (hot) + S3 (cold) — provides resiliency, scalability, and cost-effectiveness for centralized logging across 100+ microservices. It keeps on-call workflows fast, preserves logs durably, and prevents a single noisy service from taking down the entire logging stack.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/devops-interview-prep/module/ef53ec43-96e9-4d1b-8a6e-e6eb97b0d0dc/lesson/b3aaa66b-1938-41d8-affe-d91fee40f6af" />
</CardGroup>
