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
| Component | Use |
| Fluentd / Fluent Bit | Collect & forward node logs |
| Vector | Modern alternative (Rust) |
2. Configuring Elasticsearch
helm install es elastic/elasticsearch -n logging
helm install kibana elastic/kibana -n logging
3. Configuring Kibana
| Setting | Detail |
| Index pattern | logstash-* |
| 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 ELK | Loki |
| Storage | Object storage (S3, GCS); cheap |
| Indexing | Labels 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
| System | Setting |
| Loki | limits_config.retention_period: 720h |
| Elasticsearch | ILM policy (hot/warm/cold/delete) |
| Object storage | S3 lifecycle rules |
9. Configuring Structured Logging
| Format | Benefit |
| JSON | Field-level querying |
| logfmt | Human + machine readable |
| Klog v2 | K8s native structured logger |
10. Troubleshooting Logging Stack
| Symptom | Cause |
| Missing logs | Promtail/Fluentd position file stuck; check pod restarts |
| Disk pressure | Retention too long; expand storage |
| Slow queries | Loki: increase parallelism, ES: too many indices |