Docker & Kubernetes
Challenge - Advanced project
Set up a project combining the production app.
Challenge: the advanced project
In the last challenge you'll complete the observability of the shop in production: the API's metrics will be collected by Prometheus, an alert will go out when the error rate rises, and the HPA will work correctly under load.
ServiceMonitor -> api:http/metrics (30 s)
PrometheusRule -> error rate > 5%, 10 min -> severity: critical
HPA -> cpu 70%, 2-10 replicas
load test -> watch with hpa -w
Each part is a separate YAML or command; together they are the "eyes and reflexes" of a production service.
Exercise The ServiceMonitor
Write a ServiceMonitor YAML: monitoring.coreos.com/v1; name api, namespace magaza, label release: izleme; selector.matchLabels app: api; a single endpoint: port: http, path: /metrics, interval: 30s.
Show example solution
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: api
namespace: magaza
labels:
release: izleme
spec:
selector:
matchLabels:
app: api
endpoints:
- port: http
path: /metrics
interval: 30s
Exercise The alert rule
Write a PrometheusRule's spec section (starting with spec:): name: api under groups; a single rule: alert: YuksekHataOrani, expr: sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) > 0.05, for: 10m, labels.severity: critical.
Show example solution
spec:
groups:
- name: api
rules:
- alert: YuksekHataOrani
expr: sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) > 0.05
for: 10m
labels:
severity: critical
Test yourself
-
What happens if for: 0m is chosen instead of for: 10m for the error-rate alert?
- The alert never goes out
- Prometheus crashes
- The HPA turns off
- Even a single short spike sends an alert; alert fatigue increases
Show answer
Even a single short spike sends an alert; alert fatigue increases — A waiting period is given to separate lasting problems from brief noise.
-
Write the metric scrape interval.
endpoints:
- port: http
path: /metrics
____: 30s
Show answer
endpoints:
- port: http
path: /metrics
interval: 30s
-
Match.
- ServiceMonitor
- Metric collection
- PrometheusRule
- Alerting
- HPA
- Autoscaling
- hpa -w
- Observation
Next lesson: Advanced section exam