Monitoring & Logging in K8s

💤0
Lv 10 XP
← 📦 Containers & Kubernetes · Helm & Operations

Monitoring & Logging in K8s

Advanced ⭐ 120 XP ⏱ 18 min #kubernetes#monitoring#logging

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 top and autoscaling.
  • Logs: containers log to stdout/stderr; a node agent (Fluent Bit/Fluentd) ships them to a backend (Loki, Elasticsearch). kubectl logs reads 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
  1. Use kubectl top and kubectl logs to inspect a workload’s health.
  2. Explain why apps should log to stdout/stderr in Kubernetes.
  3. Describe the role of Prometheus and Grafana in a cluster.
  4. Create an HPA that scales a Deployment on CPU.
🧾Cheat Sheet
Tool / cmdPurpose
PrometheusScrape + store metrics
GrafanaDashboards
metrics-serverkubectl top, HPA
Fluent Bit/FluentdShip logs
kubectl logs -fStream logs
kubectl get eventsCluster events
HPAAutoscale 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