Phase 5 — Observability on Kubernetes with Prometheus and Grafana
What This Post Is
Section titled “What This Post Is”Phase 3 left the application running on a self-hosted K3s cluster on Raspberry Pi hardware. Everything was deployed and working — but there was no visibility into what was actually happening. No metrics, no dashboards, no way to know if a pod was struggling before it failed.
Phase 4 fixes that. The goal was a production-grade observability stack — Prometheus scraping real metrics, Grafana displaying them as dashboards, and the whole thing managed as infrastructure rather than a manual setup that disappears when a pod restarts.
The Approach — Prometheus Operator, Not Raw Manifests
Section titled “The Approach — Prometheus Operator, Not Raw Manifests”Phase 3 included a basic Prometheus and Grafana deployment using hand-written Kubernetes manifests. That approach works, but it does not scale well. Each configuration change means editing YAML directly. Scrape targets are hardcoded. Dashboard provisioning is manual.
Phase 4 replaces that with the kube-prometheus-stack Helm chart — the Prometheus Operator pattern. One Helm install deploys Prometheus, Grafana, Alertmanager, kube-state-metrics, and node-exporter as a single managed stack. The Operator pattern means scrape targets are declared as Kubernetes resources rather than hardcoded in Prometheus configuration files.
The difference matters. In the raw manifest approach, adding a new scrape target means editing the Prometheus ConfigMap and restarting the pod. With the Operator pattern, you create a ServiceMonitor resource and the Operator updates Prometheus automatically — no restart, no ConfigMap editing, no downtime.
The ServiceMonitor Pattern
Section titled “The ServiceMonitor Pattern”The central concept in the Prometheus Operator is the ServiceMonitor. Instead of telling Prometheus where to scrape, you declare what to scrape and let the Operator handle the rest.
A ServiceMonitor for the backend looks like this:
apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata: name: humor-game-monitor namespace: monitoring labels: release: monitoringspec: namespaceSelector: matchNames: - humor-memory-game selector: matchLabels: app: backend endpoints: - port: metrics path: /metrics interval: 15sThe Prometheus Operator reads this, finds the backend Service in the humor-memory-game namespace, and adds it to Prometheus’s scrape configuration. No Prometheus restart required. No ConfigMap editing. The scrape target is version-controlled alongside the application manifests.
The Grafana Sidecar Pattern
Section titled “The Grafana Sidecar Pattern”The other significant pattern in this phase is the Grafana sidecar for dashboard provisioning. The kube-prometheus-stack chart deploys a sidecar container alongside Grafana that watches for Kubernetes ConfigMaps labelled grafana_dashboard=1. When it finds one, it loads the JSON into Grafana automatically.
The workflow is clean. Export a dashboard as JSON, create a ConfigMap from the file, apply a label, and the dashboard appears in Grafana within 60 seconds — no manual import, no clicking through the UI, no state that lives only inside the Grafana pod.
kubectl create configmap comprehensive-game-dashboard \ --from-file=comprehensive-dashboard.json=monitoring/dashboards/comprehensive-dashboard.json \ --namespace monitoring
kubectl label configmap comprehensive-game-dashboard -n monitoring grafana_dashboard=1This makes dashboards version-controlled and redeployment-safe. When the monitoring stack is torn down and reinstalled, the dashboard reappears automatically.
What Actually Broke
Section titled “What Actually Broke”Eleven implementation issues were encountered and documented. None were immediately obvious. Each required methodical diagnosis.
The dashboard loaded but all panels showed “Datasource not found.” The dashboard JSON contained a hardcoded UID from a different Grafana environment. The kube-prometheus-stack chart assigns the Prometheus datasource the UID prometheus. Any other value — including the ${DS_PROMETHEUS} template variable that Grafana resolves during manual UI import — fails silently when loaded via ConfigMap provisioning. The fix was replacing every datasource UID in the JSON with the literal string prometheus before creating the ConfigMap.
The Grafana home page showed the Welcome screen instead of the dashboard after every login. The monitoring-values.yaml was configured with default_home_dashboard_path set to /var/lib/grafana/dashboards/default/comprehensive-dashboard.json. The sidecar does not write to that path — it writes to /tmp/dashboards/. The path was confirmed by inspecting the sidecar environment variable FOLDER: /tmp/dashboards via kubectl describe. Once corrected and the stack upgraded, the dashboard loaded as the home page immediately.
The ServiceMonitor showed 0/0 up in Prometheus — no active targets. Three separate issues compounded. The ServiceMonitor lacked a namespaceSelector, so the Prometheus Operator was only looking for matching Services within the monitoring namespace — not in humor-memory-game where the backend actually runs. The backend Service had no metadata.labels block at all — only a pod selector inside the spec — so the ServiceMonitor’s matchLabels selector found nothing. The Service port was unnamed (3001/TCP) but the ServiceMonitor references ports by name. All three required fixes before Prometheus began scraping.
The Prometheus RBAC ClusterRole was missing nonResourceURLs: ["/metrics"]. K3s requires this permission for node and cAdvisor metric endpoints. Without it, every node-level scrape request returned HTTP 403. Adding the two lines and reapplying the ClusterRole fixed all targets immediately.
After running helm upgrade to apply the corrected values file, port-forwards stopped responding. The upgrade restarted the Grafana pod. Port-forwards bind to a specific pod instance — when the pod restarts, the forward loses its connection silently. The fix is always to kill and restart port-forwards after any helm upgrade.
kubectl edit failed with error: unable to launch the editor "vi". The management VM does not have vi installed. The solution is kubectl patch for live resource changes and heredoc syntax for file-based edits. This is worth knowing early — it affects any workflow that assumes a text editor is available on the management machine.
What the Backend Actually Exposes
Section titled “What the Backend Actually Exposes”One finding worth documenting clearly. The dashboard was built expecting custom application metrics — http_requests_total, active_games_current, database_connections_current. None of these exist. The backend uses the default Node.js prom-client instrumentation, which exposes process and runtime metrics only.
This is not a gap in the monitoring stack — it is a gap in the application instrumentation. The monitoring infrastructure is correctly configured and scraping successfully. What Prometheus receives is process_cpu_seconds_total, process_resident_memory_bytes, nodejs_eventloop_lag_seconds, and similar Node.js runtime metrics. These are real and useful — they show CPU pressure, memory trends, and event loop health. But they are not the application-level business metrics the dashboard panels were designed for.
Adding custom metrics requires modifying the backend source code to register counters and histograms via prom-client. That is Phase 1 work, not Phase 4 work. It is documented as a future enhancement rather than a gap.
The Dashboard
Section titled “The Dashboard”The Comprehensive Game Monitoring Dashboard covers the available metrics across ten panels:
- Application health — pod status via kube-state-metrics
- HTTP request rate — derived from health probe and Prometheus scrape traffic
- Response time percentiles — p50, p95, p99
- Memory usage —
process_resident_memory_bytestrending over time - CPU usage —
rate(process_cpu_seconds_total[5m])as a percentage - Database connections — from kube-state-metrics
- Error rate by endpoint
- Node.js event loop lag
The dashboard JSON is version-controlled in monitoring/dashboards/ and loaded via ConfigMap on every deployment. No manual import step is required.
What Was Produced
Section titled “What Was Produced”Six documents cover the complete Phase 4 implementation, living in the docs/ folder of the Kubernetes repository:
| Document | Purpose |
|---|---|
MONITORING_SETUP.md |
Complete installation guide — single linear flow from nothing to a working monitoring stack |
MONITORING_DASHBOARDS.md |
Dashboard management — importing, ConfigMap loading, building manually |
MONITORING_METRICS.md |
PromQL reference, available metrics, scrape target verification |
MONITORING_TROUBLESHOOTING.md |
11 confirmed issues with root causes and fixes |
MONITORING_CUSTOM_DASHBOARDS.md |
Building custom dashboards from scratch — panels, variables, annotations, persistence |
monitoring/README.md |
Directory entry point — quick start, file index, environment reference |
What This Phase Demonstrates
Section titled “What This Phase Demonstrates”- Prometheus Operator pattern via
kube-prometheus-stackHelm chart - ServiceMonitor-based scrape target declaration — no Prometheus ConfigMap editing
- Grafana sidecar dashboard provisioning — version-controlled dashboards via ConfigMap
- Cross-namespace monitoring — ServiceMonitor targeting a different namespace from the monitoring stack
- Datasource UID management across environments — hardcoded UIDs vs provisioning-time resolution
- RBAC configuration for K3s node metrics —
nonResourceURLsrequirements - Systematic diagnosis of eleven real implementation issues — each documented with root cause and fix
- PromQL query construction against real available metrics
The Stack in Action
Section titled “The Stack in Action”

What Is Next
Section titled “What Is Next”Phase 6 covers CI/CD — automating the build, test, and deploy pipeline so that a push to the application repository triggers a multi-platform image build, pushes to Docker Hub, updates the Kubernetes manifest, and deploys to the cluster through ArgoCD with no manual steps.
← Phase 4 — Deploying to Kubernetes on Raspberry Pi
Repository: sjchristie/humor-memory-game-kubernetes Documentation: docs/ Monitoring directory: monitoring/