Monitoring & Logging in K8s
Observe cluster and workload health with metrics, logs, and the standard tooling.
Theory
Observing Kubernetes spans the cluster and the apps inside it. The common stack:
- Metrics: Prometheus scrapes metrics from Pods and components;
Grafana visualizes them. The metrics-server powers
kubectl topand autoscaling. - Logs: containers log to stdout/stderr; a node agent (Fluent Bit/Fluentd)
ships them to a backend (Loki, Elasticsearch).
kubectl logsreads them live. - Events & health:
kubectl get events,describe, and probes surface Pod problems like CrashLoopBackOff or failing readiness.
Autoscaling closes the loop: the Horizontal Pod Autoscaler (HPA) scales replicas on CPU/custom metrics.
Real-World Example
kubectl top pods # live CPU/memory (needs metrics-server)
kubectl logs deploy/web --tail=100 -f # stream app logs
kubectl get events --sort-by=.lastTimestamp
kubectl describe pod web # events: scheduling, probes, OOMKills
# Autoscale on CPU
kubectl autoscale deploy/web --min=2 --max=10 --cpu-percent=70 Hands-On Exercise
- Use
kubectl topandkubectl logsto inspect a workload’s health. - Explain why apps should log to stdout/stderr in Kubernetes.
- Describe the role of Prometheus and Grafana in a cluster.
- Create an HPA that scales a Deployment on CPU.
Cheat Sheet▾
| Tool / cmd | Purpose |
|---|---|
| Prometheus | Scrape + store metrics |
| Grafana | Dashboards |
| metrics-server | kubectl top, HPA |
| Fluent Bit/Fluentd | Ship logs |
kubectl logs -f | Stream logs |
kubectl get events | Cluster events |
| HPA | Autoscale on metrics |
Common Interview Questions▾
How does logging work in Kubernetes?
Containers write to stdout/stderr; a node-level agent (Fluent Bit/Fluentd) collects and ships those streams to a backend like Loki or Elasticsearch. kubectl logs reads them directly.
What does the Horizontal Pod Autoscaler do?
It automatically adjusts a Deployment’s replica count based on observed metrics (CPU, memory, or custom), scaling out under load and back in when idle.
Official Documentation
📝 My notes on this topic
Auto-saves as you type