Module 10 · Project 10 · Centralize Logs with Loki or ELK · Lesson 34 of 48
Structured logs in one place
Why logs must leave the servers they're written on, how to write logs a machine can search, and how a log pipeline collects, stores and queries them.
In this lesson · about 30 minutes
- Explain why logs are centralized
- Write structured logs with levels and request ids
- Describe the collector → store → query pipeline and compare Loki with ELK
Why logs can't stay on the server
In Project 01 you read logs with tail and grep on one machine. On Kubernetes that stops working:
- A Pod's logs disappear when the Pod is replaced, which is exactly when you need them.
- One request passes through web, api and worker, maybe on three different nodes.
- With 30 Pods, nobody is going to
kubectl logseach one during an outage.
Centralized logging ships every line from every container to one searchable store, and keeps it after the Pod is gone.
Write logs a machine can read
// plain text: easy for humans, hard to search
2026-11-15 10:02:11 export failed for job 5521
// structured (JSON): every field searchable
{"ts":"2026-11-15T10:02:11Z","level":"error","service":"worker",
"request_id":"req-7f3a","job_id":5521,"msg":"export failed","error":"S3 upload timeout"}
- Levels:
debug(off in production),info(normal events),warn(odd but handled),error(something failed). Set the level by environment variable. - Request id (or trace id): generated at the front door and passed along in a header, so every service logs it. One search then shows a request's whole story.
- Write to stdout, not to files. On containers, the platform collects stdout for you.
- Never log secrets or personal data: passwords, tokens, card numbers, full addresses. Mask them in the logging library.
The pipeline
- Collect: an agent on every node (a Kubernetes DaemonSet) reads every container's output and adds labels: namespace, Pod, app. Fluent Bit, Promtail/Grafana Alloy, Vector.
- Store: Loki indexes only the labels and keeps the text compressed in cheap object storage, so it's inexpensive. Elasticsearch/OpenSearch (the "ELK" or "EFK" stack) indexes every word, so it's fast for full-text search but needs more memory and disk.
- Query and view: Grafana for Loki (with LogQL), Kibana for Elasticsearch.
# LogQL: errors from the worker in the notes namespace
{namespace="notes", app="worker"} | json | level="error"
# everything for one request, across services
{namespace="notes"} | json | request_id="req-7f3a"
# errors per minute, by service (a metric from logs)
sum by (app) (count_over_time({namespace="notes"} | json | level="error" [1m]))Try it · watch it run
Centralized logging: ship, store, search
Ship the logs from three services into the store, then search them. Filter to errors, then click a request id to follow one request across web, api and worker. Finally, switch to plain-text logs and see what you lose.
3 services
write to stdout
12 lines
Collector
Fluent Bit / Promtail
on every node
Store
Loki / Elasticsearch
0 stored
Query
Grafana / Kibana
0 match
LogQL: {app="notes"} | json
Nothing stored yet. Ship the logs first.
Whiteboard · watch and try
Follow one request
Press Play. The tutor shows how a request id ties the logs together.
Tip: press Pause, then use the pen to work along on the board. Space plays and pauses; Ctrl+Z undoes your last mark.
Check your understanding0 of 3 correct
Key terms
Say the meaning to yourself, then tap a card to check.
Quiz score: 0 of 3
Written by Noviq Academy in its own words. The labs are simulations: no real servers are touched. When you build the projects yourself, use a practice virtual machine or a free-tier cloud account you control, watch your cloud spending, and never run cleanup or delete commands on a machine you care about. Tool names belong to their owners; versions and output are shown for learning and may differ slightly from yours. Read-aloud and the whiteboard voice use your device's built-in voices.