Implementing Centralized Logging

1. Installing Fluentd DaemonSet

helm repo add fluent https://fluent.github.io/helm-charts
helm install fluentd fluent/fluentd -n logging --create-namespace
ComponentUse
Fluentd / Fluent BitCollect & forward node logs
VectorModern alternative (Rust)

2. Configuring Elasticsearch

helm install es elastic/elasticsearch -n logging
helm install kibana elastic/kibana -n logging

3. Configuring Kibana

SettingDetail
Index patternlogstash-*
Time field@timestamp

4. Setting Up Log Forwarding

<source>
  @type tail
  path /var/log/containers/*.log
  pos_file /var/log/fluentd-containers.pos
  tag kube.*
  <parse> @type json </parse>
</source>
<match kube.**>
  @type elasticsearch
  host es-master.logging
  port 9200
  logstash_format true
</match>

5. Using Loki for Logging

helm install loki grafana/loki-stack -n logging --create-namespace
vs ELKLoki
StorageObject storage (S3, GCS); cheap
IndexingLabels only; full-text via query

6. Installing Promtail

scrape_configs:
- job_name: kubernetes-pods
  kubernetes_sd_configs: [{ role: pod }]
  relabel_configs:
  - { source_labels: [__meta_kubernetes_namespace], target_label: namespace }
  - { source_labels: [__meta_kubernetes_pod_label_app], target_label: app }

7. Querying Logs with LogQL

{namespace="prod", app="web"} |= "error" | json | line_format "{{.msg}}"
rate({app="web"} |= "5xx" [5m])
sum by (status) (count_over_time({app="web"} | json [5m]))

8. Setting Log Retention Policies

SystemSetting
Lokilimits_config.retention_period: 720h
ElasticsearchILM policy (hot/warm/cold/delete)
Object storageS3 lifecycle rules

9. Configuring Structured Logging

FormatBenefit
JSONField-level querying
logfmtHuman + machine readable
Klog v2K8s native structured logger

10. Troubleshooting Logging Stack

SymptomCause
Missing logsPromtail/Fluentd position file stuck; check pod restarts
Disk pressureRetention too long; expand storage
Slow queriesLoki: increase parallelism, ES: too many indices