summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-12-06 22:50:01 +0200
committerPaul Buetow <paul@buetow.org>2025-12-06 22:50:01 +0200
commit9c2fe98a60ed6100fd6a07c36cf504d3d919bf18 (patch)
treeb23e6b30d52666509032aa9ce709503c49b4388a
parent11b545f8184a0d716194eb196170ec6f344228e2 (diff)
more on this
-rw-r--r--gemfeed/DRAFT-f3s-kubernetes-with-freebsd-part-8.gmi.tpl224
1 files changed, 185 insertions, 39 deletions
diff --git a/gemfeed/DRAFT-f3s-kubernetes-with-freebsd-part-8.gmi.tpl b/gemfeed/DRAFT-f3s-kubernetes-with-freebsd-part-8.gmi.tpl
index e88cdf07..6f26d2cb 100644
--- a/gemfeed/DRAFT-f3s-kubernetes-with-freebsd-part-8.gmi.tpl
+++ b/gemfeed/DRAFT-f3s-kubernetes-with-freebsd-part-8.gmi.tpl
@@ -12,10 +12,10 @@ This is the 8th blog post about the f3s series for my self-hosting demands in a
In this blog post, I set up a complete observability stack for the k3s cluster. Observability is crucial for understanding what's happening inside the cluster—whether it's tracking resource usage, debugging issues, or analysing application behaviour. The stack consists of four main components, all deployed into the `monitoring` namespace:
-* **Prometheus** — time-series database for metrics collection and alerting
-* **Grafana** — visualisation and dashboarding frontend
-* **Loki** — log aggregation system (like Prometheus, but for logs)
-* **Alloy** — telemetry collector that ships logs from all pods to Loki
+* `Prometheus`: time-series database for metrics collection and alerting
+* `Grafana`: visualisation and dashboarding frontend
+* `Loki`: log aggregation system (like Prometheus, but for logs)
+* `Alloy`: telemetry collector that ships logs from all pods to Loki
Together, these form the "PLG" stack (Prometheus, Loki, Grafana), which is a popular open-source alternative to commercial observability platforms.
@@ -50,15 +50,7 @@ namespace/monitoring created
## Installing Prometheus and Grafana
-Prometheus and Grafana are deployed together using the `kube-prometheus-stack` Helm chart from the Prometheus community. This chart bundles Prometheus, Grafana, Alertmanager, and various exporters into a single deployment.
-
-### What each component does
-
-* **Prometheus** — scrapes metrics from all pods and services, stores them in a time-series database, and evaluates alerting rules
-* **Grafana** — provides dashboards and visualisation for metrics (and later, logs from Loki)
-* **Alertmanager** — handles alerts from Prometheus, deduplicates them, and routes notifications
-* **Node Exporter** — runs as a DaemonSet on each node to expose hardware and OS metrics
-* **Kube State Metrics** — exposes Kubernetes object metrics (deployments, pods, etc.)
+Prometheus and Grafana are deployed together using the `kube-prometheus-stack` Helm chart from the Prometheus community. This chart bundles Prometheus, Grafana, Alertmanager, and various exporters (Node Exporter, Kube State Metrics) into a single deployment. I'll explain what each component does in detail later when we look at the running pods.
### Prerequisites
@@ -109,7 +101,15 @@ The `persistence-values.yaml` configures Prometheus and Grafana to use the NFS-b
The chart also deploys an ingress for Grafana, making it accessible at `grafana.f3s.foo.zone`. The ingress configuration follows the same pattern as other services in the cluster—Traefik handles the routing internally, while the OpenBSD edge relays terminate TLS and forward traffic through WireGuard.
-Once deployed, Grafana is accessible and comes pre-configured with Prometheus as a data source. The default credentials are `admin`/`prom-operator`, which should be changed immediately after first login.
+Once deployed, Grafana is accessible and comes pre-configured with Prometheus as a data source. You can verify the Prometheus service is running:
+
+```sh
+$ kubectl get svc -n monitoring prometheus-kube-prometheus-prometheus
+NAME TYPE CLUSTER-IP PORT(S)
+prometheus-kube-prometheus-prometheus ClusterIP 10.43.152.163 9090/TCP,8080/TCP
+```
+
+Grafana connects to Prometheus using the internal service URL `http://prometheus-kube-prometheus-prometheus.monitoring.svc.cluster.local:9090`. The default Grafana credentials are `admin`/`prom-operator`, which should be changed immediately after first login.
=> ./f3s-kubernetes-with-freebsd-part-8/grafana-dashboard.png Grafana dashboard showing cluster metrics
@@ -188,6 +188,11 @@ discovery.relabel "pods" {
source_labels = ["__meta_kubernetes_pod_container_name"]
target_label = "container"
}
+
+ rule {
+ source_labels = ["__meta_kubernetes_pod_label_app"]
+ target_label = "app"
+ }
}
loki.source.kubernetes "pods" {
@@ -202,11 +207,19 @@ loki.write "default" {
}
```
-This configuration automatically labels each log line with the namespace, pod name, and container name, making it easy to filter logs in Grafana.
+This configuration automatically labels each log line with the namespace, pod name, container name, and app label, making it easy to filter logs in Grafana.
### Adding Loki as a Grafana data source
-Loki doesn't have its own web UI—you query it through Grafana. To add Loki as a data source in Grafana:
+Loki doesn't have its own web UI—you query it through Grafana. First, verify the Loki service is running:
+
+```sh
+$ kubectl get svc -n monitoring loki
+NAME TYPE CLUSTER-IP PORT(S)
+loki ClusterIP 10.43.64.60 3100/TCP,9095/TCP
+```
+
+To add Loki as a data source in Grafana:
* Navigate to Configuration → Data Sources
* Click "Add data source"
@@ -214,13 +227,7 @@ Loki doesn't have its own web UI—you query it through Grafana. To add Loki as
* Set the URL to: `http://loki.monitoring.svc.cluster.local:3100`
* Click "Save & Test"
-Once configured, you can explore logs in Grafana's "Explore" view using LogQL queries like:
-
-```
-{namespace="services"}
-{pod=~"miniflux.*"}
-{namespace="kube-system", container="traefik"}
-```
+Once configured, you can explore logs in Grafana's "Explore" view. I'll show some example queries in the "Using the observability stack" section below.
=> ./f3s-kubernetes-with-freebsd-part-8/loki-explore.png Exploring logs in Grafana with Loki
@@ -230,6 +237,8 @@ Once configured, you can explore logs in Grafana's "Explore" view using LogQL qu
After deploying everything, here's what's running in the monitoring namespace:
+=> ./f3s-kubernetes-with-freebsd-part-8/k9s-monitoring-namespace.png k9s showing all pods in the monitoring namespace
+
```sh
$ kubectl get pods -n monitoring
NAME READY STATUS RESTARTS AGE
@@ -266,14 +275,21 @@ prometheus-prometheus-node-exporter ClusterIP 10.43.127.242 9100/TCP
Let me break down what each pod does:
-* **alertmanager-...** — handles alerting rules and notifications
-* **alloy-*** — three pods (one per node) collecting logs and shipping to Loki
-* **loki-0** — the log aggregation backend
-* **prometheus-grafana-...** — the Grafana frontend
-* **prometheus-kube-prometheus-operator-...** — manages Prometheus configuration via CRDs
-* **prometheus-kube-state-metrics-...** — exposes Kubernetes object metrics
-* **prometheus-prometheus-...** — the Prometheus server itself
-* **prometheus-prometheus-node-exporter-*** — three pods (one per node) exposing host metrics
+* `alertmanager-prometheus-kube-prometheus-alertmanager-0`: the Alertmanager instance that receives alerts from Prometheus, deduplicates them, groups related alerts together, and routes notifications to the appropriate receivers (email, Slack, PagerDuty, etc.). It runs as a StatefulSet with persistent storage for silences and notification state.
+
+* `alloy-g5fgj, alloy-nfw8w, alloy-tg9vj`: three Alloy pods running as a DaemonSet, one on each k3s node. Each pod tails the container logs from its local node via the Kubernetes API and forwards them to Loki. This ensures log collection continues even if a node becomes isolated from the others.
+
+* `loki-0`: the single Loki instance running in single-binary mode. It receives log streams from Alloy, stores them in chunks on the NFS-backed persistent volume, and serves queries from Grafana. The `-0` suffix indicates it's a StatefulSet pod.
+
+* `prometheus-grafana-...`: the Grafana web interface for visualising metrics and logs. It comes pre-configured with Prometheus as a data source and includes dozens of dashboards for Kubernetes monitoring. Dashboards, users, and settings are persisted to the NFS share.
+
+* `prometheus-kube-prometheus-operator-...`: the Prometheus Operator that watches for custom resources (ServiceMonitor, PodMonitor, PrometheusRule) and automatically configures Prometheus to scrape new targets. This allows applications to declare their own monitoring requirements.
+
+* `prometheus-kube-state-metrics-...`: generates metrics about the state of Kubernetes objects themselves: how many pods are running, pending, or failed; deployment replica counts; node conditions; PVC status; and more. Essential for cluster-level dashboards.
+
+* `prometheus-prometheus-kube-prometheus-prometheus-0`: the Prometheus server that scrapes metrics from all configured targets (pods, services, nodes), stores them in a time-series database, evaluates alerting rules, and serves queries to Grafana.
+
+* `prometheus-prometheus-node-exporter-...`: three Node Exporter pods running as a DaemonSet, one on each node. They expose hardware and OS-level metrics: CPU usage, memory, disk I/O, filesystem usage, network statistics, and more. These feed the "Node Exporter" dashboards in Grafana.
## Using the observability stack
@@ -281,9 +297,9 @@ Let me break down what each pod does:
The kube-prometheus-stack comes with many pre-built dashboards. Some useful ones include:
-* **Kubernetes / Compute Resources / Cluster** — overview of CPU and memory usage across the cluster
-* **Kubernetes / Compute Resources / Namespace (Pods)** — resource usage by namespace
-* **Node Exporter / Nodes** — detailed host metrics like disk I/O, network, and CPU
+* `Kubernetes / Compute Resources / Cluster`: overview of CPU and memory usage across the cluster
+* `Kubernetes / Compute Resources / Namespace (Pods)`: resource usage by namespace
+* `Node Exporter / Nodes`: detailed host metrics like disk I/O, network, and CPU
### Querying logs with LogQL
@@ -307,14 +323,144 @@ In Grafana's Explore view, select Loki as the data source and try queries like:
Prometheus supports alerting rules that can notify you when something goes wrong. The kube-prometheus-stack includes many default alerts for common issues like high CPU usage, pod crashes, and node problems. These can be customised via PrometheusRule CRDs.
+## Monitoring external FreeBSD hosts
+
+The observability stack can also monitor servers outside the Kubernetes cluster. The FreeBSD hosts (`f0`, `f1`, `f2`) that serve NFS storage can be added to Prometheus using the Node Exporter.
+
+### Installing Node Exporter on FreeBSD
+
+On each FreeBSD host, install the node_exporter package:
+
+```sh
+paul@f0:~ % doas pkg install -y node_exporter
+```
+
+Enable the service to start at boot:
+
+```sh
+paul@f0:~ % doas sysrc node_exporter_enable=YES
+node_exporter_enable: -> YES
+```
+
+Configure node_exporter to listen on the WireGuard interface. This ensures metrics are only accessible through the secure tunnel, not the public network. Replace the IP with the host's WireGuard address:
+
+```sh
+paul@f0:~ % doas sysrc node_exporter_args='--web.listen-address=192.168.2.130:9100'
+node_exporter_args: -> --web.listen-address=192.168.2.130:9100
+```
+
+Start the service:
+
+```sh
+paul@f0:~ % doas service node_exporter start
+Starting node_exporter.
+```
+
+Verify it's running:
+
+```sh
+paul@f0:~ % curl -s http://192.168.2.130:9100/metrics | head -3
+# HELP go_gc_duration_seconds A summary of the wall-time pause...
+# TYPE go_gc_duration_seconds summary
+go_gc_duration_seconds{quantile="0"} 0
+```
+
+Repeat for the other FreeBSD hosts (`f1`, `f2`) with their respective WireGuard IPs.
+
+### Adding FreeBSD hosts to Prometheus
+
+Create a file `additional-scrape-configs.yaml` in the prometheus configuration directory:
+
+```yaml
+- job_name: 'node-exporter'
+ static_configs:
+ - targets:
+ - '192.168.2.130:9100' # f0 via WireGuard
+ - '192.168.2.131:9100' # f1 via WireGuard
+ - '192.168.2.132:9100' # f2 via WireGuard
+ labels:
+ os: freebsd
+```
+
+The `job_name` must be `node-exporter` to match the existing dashboards. The `os: freebsd` label allows filtering these hosts separately if needed.
+
+Create a Kubernetes secret from this file:
+
+```sh
+$ kubectl create secret generic additional-scrape-configs \
+ --from-file=additional-scrape-configs.yaml \
+ -n monitoring
+```
+
+Update `persistence-values.yaml` to reference the secret:
+
+```yaml
+prometheus:
+ prometheusSpec:
+ additionalScrapeConfigsSecret:
+ enabled: true
+ name: additional-scrape-configs
+ key: additional-scrape-configs.yaml
+```
+
+Upgrade the Prometheus deployment:
+
+```sh
+$ just upgrade
+```
+
+After a minute or so, the FreeBSD hosts appear in the Prometheus targets and in the Node Exporter dashboards in Grafana.
+
+=> ./f3s-kubernetes-with-freebsd-part-8/grafana-freebsd-nodes.png FreeBSD hosts in the Node Exporter dashboard
+
+### FreeBSD memory metrics compatibility
+
+The default Node Exporter dashboards are designed for Linux and expect metrics like `node_memory_MemAvailable_bytes`. FreeBSD uses different metric names (`node_memory_size_bytes`, `node_memory_free_bytes`, etc.), so memory panels will show "No data" out of the box.
+
+To fix this, I created a PrometheusRule that generates synthetic Linux-compatible metrics from the FreeBSD equivalents:
+
+```yaml
+apiVersion: monitoring.coreos.com/v1
+kind: PrometheusRule
+metadata:
+ name: freebsd-memory-rules
+ namespace: monitoring
+ labels:
+ release: prometheus
+spec:
+ groups:
+ - name: freebsd-memory
+ rules:
+ - record: node_memory_MemTotal_bytes
+ expr: node_memory_size_bytes{os="freebsd"}
+ - record: node_memory_MemAvailable_bytes
+ expr: node_memory_free_bytes{os="freebsd"} + node_memory_inactive_bytes{os="freebsd"} + node_memory_cache_bytes{os="freebsd"}
+ - record: node_memory_MemFree_bytes
+ expr: node_memory_free_bytes{os="freebsd"}
+ - record: node_memory_Buffers_bytes
+ expr: node_memory_buffer_bytes{os="freebsd"}
+ - record: node_memory_Cached_bytes
+ expr: node_memory_cache_bytes{os="freebsd"}
+```
+
+This file is saved as `freebsd-recording-rules.yaml` and applied as part of the Prometheus installation. The `os="freebsd"` label (set in the scrape config) ensures these rules only apply to FreeBSD hosts. After applying, the memory panels in the Node Exporter dashboards populate correctly for FreeBSD.
+
+=> https://codeberg.org/snonux/conf/src/branch/master/f3s/prometheus/freebsd-recording-rules.yaml freebsd-recording-rules.yaml on Codeberg
+
+### Disk I/O metrics limitation
+
+Unlike memory metrics, disk I/O metrics (`node_disk_read_bytes_total`, `node_disk_written_bytes_total`, etc.) are not available on FreeBSD. The Linux diskstats collector that provides these metrics doesn't have a FreeBSD equivalent in the node_exporter.
+
+The disk I/O panels in the Node Exporter dashboards will show "No data" for FreeBSD hosts. FreeBSD does expose ZFS-specific metrics (`node_zfs_arcstats_*`) for ARC cache performance, and per-dataset I/O stats are available via `sysctl kstat.zfs`, but mapping these to the Linux-style metrics the dashboards expect is non-trivial. Creating custom ZFS-specific dashboards is left as an exercise for another day.
+
## Summary
-With Prometheus, Grafana, Loki, and Alloy deployed, I now have complete visibility into the k3s cluster:
+With Prometheus, Grafana, Loki, and Alloy deployed, I now have complete visibility into the k3s cluster and the FreeBSD storage servers:
-* **Metrics** — Prometheus collects and stores time-series data from all components
-* **Logs** — Loki aggregates logs from all containers, searchable via Grafana
-* **Visualisation** — Grafana provides dashboards and exploration tools
-* **Alerting** — Alertmanager can notify on conditions defined in Prometheus rules
+* `Metrics`: Prometheus collects and stores time-series data from all components
+* `Logs`: Loki aggregates logs from all containers, searchable via Grafana
+* `Visualisation`: Grafana provides dashboards and exploration tools
+* `Alerting`: Alertmanager can notify on conditions defined in Prometheus rules
This observability stack runs entirely on the home lab infrastructure, with data persisted to the NFS share. It's lightweight enough for a three-node cluster but provides the same capabilities as production-grade setups.