homek8s/docs/hubble-event-loss.md

216 lines
11 KiB
Markdown
Raw Normal View History

# Hubble event loss: why the counter was climbing and the gate on it
Finding **F10** of the [2026-11-27 cluster network-security audit](2026-11-27-cluster-netsec-audit.md).
Written up here because the number looked like data loss and was not, and because the
detector contract that came out of it outlives this particular fix.
## What was observed
```
$ promq 'sum by (node,source) (rate(hubble_lost_events_total[30m]))'
0.0966 node=linode source=hubble_ring_buffer
0.6821 node=siriussec source=hubble_ring_buffer
0.0050 node=rpi source=hubble_ring_buffer
$ promq 'sum by (node) (hubble_lost_events_total)'
23787 linode / 72529 siriussec / 156 rpi (2026-11-27T05:16Z)
```
`source` was always `hubble_ring_buffer`; `perf_event_ring_buffer` and
`observer_events_queue` were 0 on all three nodes the whole time, i.e. the BPF
perf ring (the only place events can genuinely be lost on the way in) never
dropped anything, and the metrics exporter never fell behind.
## Root cause — a measurement artefact, not dropped flows
`Ring.read()` (`pkg/hubble/container/ring.go`) synthesises a
`LostEvent{source: HUBBLE_RING_BUFFER}` **and increments the metric** whenever a
read falls outside the valid window — including the "reader behind writer" case
at the ring's read boundary. `getFlowRate()` (`pkg/hubble/observer/local_observer.go`)
walks the ring *backwards* over a full minute on every `ServerStatus` call, and
`ServerStatus` is called by:
* the agent's own hubble health probe, every **5 s** (12/min, constantly),
* every `hubble status` (including the one in `cilium-dbg status` and the Hubble UI's status panel),
* every UI/relay status request.
When the ring holds **less than one minute of flows**, that walk hits the
boundary, so each probe adds exactly 1 to the loss counter. Cilium's own tracker
is cilium#46584 (`hubble_ring_buffer` lost metric increments even when no events
are actually lost); upstream fixed it by counting only on forward reads, which
is not in the 1.20.1 build deployed here.
Measured proof on this cluster (2026-11-27T05:13Z):
| node | ring | flows/s | 10 back-to-back `hubble status` | +21 s idle |
|---|---|---|---|---|
| siriussec | 4095 (100 %) | ~80 | **+11** | +12 |
| linode | 4095 (100 %) | ~20 | **0** (40 calls) | 0 |
siriussec's ring held ~51 s at 80 flows/s → every probe hit the boundary.
linode's held ~200 s → the walk stopped on the timestamp check and nothing was
counted. That is the whole of the "0.68/s" on siriussec and the bursty ~4/min on
linode (during linode's ingest bursts the ring briefly held <60 s).
The cumulative 72,529 was therefore the counter counting reads, not losses.
No flow was actually dropped: with no client attached the ring only has to hold
history, and `Ring.Write()` never discards — it overwrites, and loss is only
ever reported at read time.
## The change
`infrastructure/networking/cilium/values.yaml` (Flux → HelmRelease `kube-system/cilium`):
```yaml
hubble:
eventBufferCapacity: "16383" # was the chart default, 4095
```
The ring must hold **>= 60 s at peak** so the backward walk exits on the
timestamp check instead of the boundary. 24 h peak flow rates measured in
Prometheus (`max_over_time(sum by (node)(rate(hubble_flows_processed_total[1m]))[24h:1m])`):
| node | 24 h peak flows/s | 16383 events = | 4095 events = |
|---|---|---|---|
| linode | 195.8 | 84 s | 21 s |
| siriussec | 101.5 | 161 s | 40 s |
| rpi | 38.4 | 427 s | 107 s |
16383 is the smallest ring that covers the 60 s walk on **every** node: at
linode's peak 8191 would hold 41 s (still under a minute, still hitting the
boundary), 16383 holds 84 s. 32767 is the next allowed step and doubles the
memory for headroom the 24 h peak does not justify. The ring only accepts
`2^n - 1` (1, 3, 7 … 4095, 8191, 16383, 32767, 65535).
### Memory cost per node (measured, not assumed)
The ring stores `*v1.Event` pointers and keeps the events themselves alive; the
delta is bounded by `(16383 - 4095) × retained bytes per event`. Measured with
`container_memory_working_set_bytes{pod=~"cilium-.*",container="cilium-agent"}`,
averaged over the 30 min before the roll (old 4095 agents) against a 34 min
uptime sample after all three agents restarted onto the new config:
| node | before (4095) | after (16383) | delta |
|---|---|---|---|
| siriussec | 330.2 MiB | 333.8 MiB | +3.6 MiB |
| linode | 309.1 MiB | 297.1 MiB | -12.0 MiB |
| rpi | 194.5 MiB | 213.1 MiB | +18.6 MiB |
The deltas straddle zero, i.e. the 12288 extra retained events are inside the
process's own allocation noise at this event size (they are pointers plus the
payloads that were already being allocated; the ring only delays their free).
The honest statement is: **no measurable growth; the Pi, the node with the
least headroom after linode, is +18.6 MiB.** Rejected alternative: 65535 "to be
safe" is 4x this for headroom the 24 h peak does not justify.
### Rollout
Config-only change: the chart does not put a configmap checksum on the
DaemonSet pod template, so the Helm upgrade updated `cilium-config` without
rolling the agents. The agents were restarted **one node at a time** (rpi →
linode → siriussec), each with the datapath checked before moving on:
* `hubble status` → `Current/Max Flows: … /16,383`
* pod↔pod across the mesh: linode → `forgejo.forgejo.svc:3000`, siriussec →
`osint-nats.osint.svc:8222`, rpi host → `10.10.0.2:8080` (gateway origin)
* linode pod → internet (masquerade) `https://data.gdeltproject.org/...`
* all three nodes `Ready`, `cilium` DaemonSet 3/3
Rollback: set `eventBufferCapacity` back to the default (delete the key) and
restart the agents, the same way. Nothing else changed — L7 is still on
(`enable-l7-proxy=true`), `hubble-metrics` still carries `flow`, no retention
change.
## The gate (this is the durable part)
A deeper ring removes *this* artefact. It does not make the stream
unconditionally complete — a genuinely overloaded agent, or a Hubble client that
cannot keep up with a follow stream, still loses events, and that failure is
silent. So the sensor contract is:
> **A 30-minute window with any lost event may not be reported as clean.**
2026-11-27 18:46:35 -05:00
The counter carries two different signals and the gate must not treat them
alike: `perf_event_ring_buffer` / `observer_events_queue` are real loss on the
way in, while `hubble_ring_buffer` also counts *reads* that fall outside the
ring's retained window (the artefact above). A read is not a lost event, so the
gate value is built from the two real-loss sources only, and read churn gets its
own rate-based regression guard.
Threshold and enforcement:
| rule | expression | meaning |
|---|---|---|
2026-11-27 18:46:35 -05:00
| `hubble:lost_events_increase_30m` (recorder) | `sum by (node, source) (increase(hubble_lost_events_total{source=~"perf_event_ring_buffer\|observer_events_queue"}[30m]))` | the gate value: any non-zero **real** loss → window incomplete; `source` is kept so the alert can name it |
| `hubble:ring_buffer_read_rate_15m` (recorder) | `sum by (node) (rate(hubble_lost_events_total{source="hubble_ring_buffer"}[15m]))` | read churn on the ring, events/s |
| `hubble:flows_processed_increase_30m` (recorder) | `sum by (node) (increase(hubble_flows_processed_total{type="Trace"}[30m]))` | denominator for the ratio |
2026-11-27 18:46:35 -05:00
| `HubbleFlowWindowIncomplete` (warning) | `hubble:lost_events_increase_30m > 0` for 10 m | a flow-shaped detector may not call this window clean — real loss only |
| `HubbleRingBufferReadChurn` (warning) | `hubble:ring_buffer_read_rate_15m > 0.05` for 10 m | status-call storm / ring under a minute of flows; **reads, not drops** |
| `HubbleFlowLossMaterial` (critical) | `sum by (node) (hubble:lost_events_increase_30m) / flows > 0.001` for 15 m | real loss that is material, not a boundary artefact |
| `HubbleFlowSensorMissing` (critical) | `absent(hubble:flows_processed_increase_30m)` for 15 m | no input at all; the gate cannot even be evaluated |
2026-11-27 18:46:35 -05:00
All five live in `monitoring/controllers/home/hubble-loss-gate.yaml`
(Flux → `monitoring` Kustomization), so they evaluate against the same
Prometheus the detectors read and route to Alertmanager.
**How a detector must use it:** evaluate the traffic window and the gate value
over the *same* range; if `hubble:lost_events_increase_30m > 0` for the node(s)
in scope, the result is `incomplete` — never "no anomalous traffic". Alerting on
silence (absence of an expected event) is the case that matters most: a lossy
2026-11-27 18:46:35 -05:00
window produces false silence, not false noise. `hubble_ring_buffer` alone is
never grounds for `incomplete`: it is a read counter.
### Follow-up 2026-11-27: the gate as first written paged on reads
The first version of these rules recorded
`increase(hubble_lost_events_total{source="hubble_ring_buffer"}[30m])` and alerted
at `> 0`. That reproduced the artefact the deeper ring was meant to remove and
paged on it: measured at 18:39Z, `HubbleFlowWindowIncomplete` was firing on all
three nodes with `hubble:lost_events_increase_30m` ≈ 2.01, while
`perf_event_ring_buffer` and `observer_events_queue` were 0 everywhere and the
ring-buffer counter stepped +1 twice in an hour (18:27, 18:35 — identical
timestamps on all three nodes) with the Hubble UI open from the admin laptop. A
`for: 10m` cannot fix that: two status reads an hour keep `increase() > 0` true
for the whole hour, and the same recorder fed `HubbleFlowLossMaterial`, where a
couple of extra counts out of ~7 k flows on the quiet Pi node is 0.03 %.
Split applied: the gate recorder now covers `perf_event_ring_buffer` +
`observer_events_queue` only, `HubbleFlowWindowIncomplete` and
`HubbleFlowLossMaterial` read that (unchanged thresholds), and
`hubble_ring_buffer` gets rate-based `HubbleRingBufferReadChurn` at **0.05
events/s over 15 m** — ~45 reads per window, versus the F10 probe storm's
~0.2/s (12/min) and the current ~0.001/s (2 reads/hour). Both firing conditions
name the source in the summary so a read is never described as a dropped flow.
`hubble.eventBufferCapacity` was not touched and the agents were not rolled.
## Verification (live, 2026-11-27)
```
# acceptance 1: 30 min window, after the last agent restart (05:19:48Z), read 05:53Z
$ promq 'sum by (node) (rate(hubble_lost_events_total{source="hubble_ring_buffer"}[30m]))'
0 node=rpi / 0 node=linode / 0 node=siriussec
$ promq 'sum by (node) (increase(hubble_lost_events_total{source="hubble_ring_buffer"}[30m]))'
0 node=rpi / 0 node=linode / 0 node=siriussec
# counter stops climbing — flat for 30 min, with both big rings 100 % full
$ promq 'sum by (node) (hubble_lost_events_total{source="hubble_ring_buffer"})'
31 rpi / 31 linode / 30 siriussec (05:23:47Z)
31 rpi / 31 linode / 30 siriussec (05:53:47Z)
# ...and the depth is what holds it there, not luck:
# siriussec Current/Max Flows 16,383/16,383, linode 16,383/16,383, rpi 10,685/16,383
# the artefact is gone: 20 status calls + 20 s idle move nothing
siriussec before: 30 after 20 status: 30 after +20s idle: 30
# flows still flow, L7 still on
$ hubble observe --last 3 # each of the three agents returns flows, incl. http-request
# scrape targets
hubble-metrics 10.10.0.1:9965 up / 10.10.0.2:9965 up / 10.10.0.5:9965 up
```
The ~30 events on the counter after each restart are the ring's own cold-start:
while the ring is still filling, a backward walk passes nil slots and each one
counts. It is a one-off per agent start, not a rate, and it ages out of the
30-minute window.