diff --git a/infrastructure/home/kustomization.yaml b/infrastructure/home/kustomization.yaml index 3f64703..a91ca5f 100644 --- a/infrastructure/home/kustomization.yaml +++ b/infrastructure/home/kustomization.yaml @@ -5,3 +5,4 @@ resources: - ../networking/gateway - ../networking/internal - ../networking/cilium + - ../node-io diff --git a/infrastructure/node-io/README.md b/infrastructure/node-io/README.md new file mode 100644 index 0000000..e9c3378 --- /dev/null +++ b/infrastructure/node-io/README.md @@ -0,0 +1,81 @@ +# node-io — protecting etcd's WAL fsync from the dind runner + +`etcd-io-guard` is a one-node DaemonSet (siriussec) that keeps the kernel's +dirty-page writeback backlog bounded. It exists because the amd64 Forgejo runner +(`forgejo-runner-amd64`, privileged `docker:dind`) and the etcd WAL share +`/dev/nvme0n1p3`, and heavy dind writeback turns etcd's `fdatasync` into +multi-second stalls — which in turn bounces `kube-apiserver`'s liveness probe and +every leader-election client on the control plane. + +## What the fault actually is (measured 2026-10-26) + +| observation | evidence | +|---|---| +| etcd fsync tail is extreme but rare | `etcd_disk_wal_fsync_duration_seconds`: 14.8 M samples over 45 d, 99.9 % ≤ 32 ms, **158 samples > 8.192 s**, worst logged `slow fdatasync` 10.39 s | +| the tail is not a per-cgroup queueing problem | under a 150 s synthetic dind build load with **no** mitigation: 24 etcd fsyncs, **11 of them > 1 s, 5 > 8.192 s**, while the dind cgroup averaged ~48 MB/s and ~35 IOPS — the device queue was nearly empty (`ios in progress` 0) | +| bulk dirty data is necessary for the tail | same load with only the fsync/metadata writers (no 512 MB bulk streams): 422 etcd fsyncs, **0 above 1 s**, p99 512 ms | +| per-cgroup `io.latency` cannot fix it | cgroup-v2 applies latency targets **only between peers of the same parent**. kubelet puts every pod in its own slice, so etcd's leaf and the dind pod's leaf never influence each other. Set `259:0 target=50000` on etcd's leaf and it is accepted, but during load the dind cgroup's `io.stat` still reported `depth=max` — never throttled — and the stall count did not move (17 → 27 samples > 1 s, i.e. noise). Verified against `block/blk-iolatency.c` and the cgroup-v2 docs, not assumed | +| shrinking the backlog helps | `vm.dirty_bytes=64 MiB`, `dirty_background_bytes=16 MiB`: median fsync **512 ms → ~10 ms**, **0 samples above 8.192 s** (control: 5), dind throughput 48 → 39 MB/s (−17 %) | + +Mechanism in one sentence: an `fdatasync` completes only after the device has +made everything already submitted durable, so an etcd WAL commit queues behind +whatever the runner has pushed into the page cache and writeback; on the default +`vm.dirty_ratio=20` (≈3 GB of a 15 GB box) that backlog is seconds of I/O. + +## What this DaemonSet does + +Writes `vm.dirty_bytes` / `vm.dirty_background_bytes` from the +`etcd-io-guard-config` ConfigMap into the **host's** `/proc/sys/vm` every 30 s, +re-applying on drift. It mounts the host `/proc/sys/vm` (privileged + hostPID) and +sleeps otherwise. + +It deliberately does **not** set `io.latency` or `io.max` on the runner pod: the +latency controller is peer-scoped (above), and an `io.max` cap throttles drain +rate rather than the queued backlog, so it lengthens the flush instead of +shortening the commit. + +## Verify + +```bash +# the guard is running and has applied the limits +kubectl -n node-io get ds,po +kubectl -n node-io logs ds/etcd-io-guard --tail=5 + +# the host agrees (0 ratio => the byte limits are in force) +ssh -i ~/.ssh/lancelot_priv Lancelot@192.168.1.179 \ + 'sysctl vm.dirty_bytes vm.dirty_background_bytes vm.dirty_ratio vm.dirty_background_ratio' +# expect: 67108864 / 16777216 / 0 / 0 + +# etcd tail under load +ssh -i ~/.ssh/lancelot_priv Lancelot@192.168.1.179 \ + 'curl -s http://127.0.0.1:2381/metrics | grep -E "^etcd_disk_wal_fsync_duration_seconds_(bucket|count)"' +kubectl -n kube-system logs etcd-siriussec --since=1h | grep -c 'slow fdatasync' +``` + +## Tuning and known trade-offs + +- Both knobs are in `configmap.yaml`; change, push, Flux applies, the guard + rewrites within 30 s. Keep `dirty-background-bytes <= dirty-bytes`. +- 64 MiB is aggressive (≈0.4 % of RAM). It cost ~17 % of dind write bandwidth in + the pressure test and made the 1–4 s band more frequent because there are many + more, much shorter commits. Raise to 256 MiB/64 MiB if CI wall time regresses; + that still bounds the backlog far below the 3 GB default. +- The limits are host-global: they apply to every writer on siriussec, not just + the runner. There is no per-cgroup dirty limit in this kernel that binds a + different pod. +- Not touched here (out of scope for this card): the dind data-root is still on + the etcd device. Moving it to its own disk removes the contention entirely and + is the only fix that lets the dirty limits go back to default. + +## Node-level step that is not GitOps + +Periodic TRIM is **not** covered by this directory. `fstrim.timer` was disabled +on siriussec while `/dev/nvme0n1` reports `DISC-MAX 2T`; enable it once (it +survives reboots): + +```bash +sudo systemctl enable --now fstrim.timer +``` + +That is hygiene against write amplification/GC on a heavily churned SSD, not a +measured tail-latency fix. diff --git a/infrastructure/node-io/configmap.yaml b/infrastructure/node-io/configmap.yaml new file mode 100644 index 0000000..494ba32 --- /dev/null +++ b/infrastructure/node-io/configmap.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: etcd-io-guard-config + namespace: node-io +data: + # Bounded writeback backlog. Measured on 2026-10-26 (see README.md): + # default vm.dirty_ratio=20 (3 GB on this 15 GB box) lets the amd64 dind + # runner queue 1-3 GB of dirty pages; an etcd WAL fdatasync has to wait for + # the device to flush everything already submitted, which is where the + # multi-second slow fdatasync comes from. 64 MiB / 16 MiB removed every + # sample above 8 s and cut the median fsync from 512 ms to ~10 ms at a ~17 % + # dind write-throughput cost. Raise both together if CI throughput matters + # more than the etcd tail; keep background <= bytes. + dirty-bytes: "67108864" + dirty-background-bytes: "16777216" diff --git a/infrastructure/node-io/etcd-io-guard.yaml b/infrastructure/node-io/etcd-io-guard.yaml new file mode 100644 index 0000000..dc913c9 --- /dev/null +++ b/infrastructure/node-io/etcd-io-guard.yaml @@ -0,0 +1,66 @@ +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: etcd-io-guard + namespace: node-io + labels: + app: etcd-io-guard +spec: + selector: + matchLabels: + app: etcd-io-guard + updateStrategy: + type: RollingUpdate + template: + metadata: + labels: + app: etcd-io-guard + spec: + # siriussec is the only node that hosts the etcd data dir and the dind + # runner data-root on the same NVMe. + nodeSelector: + kubernetes.io/hostname: siriussec + tolerations: + - operator: Exists + hostPID: true + priorityClassName: system-node-critical + containers: + - name: guard + image: busybox:1.36 + imagePullPolicy: IfNotPresent + command: ["/bin/sh", "/opt/guard/guard.sh"] + env: + - name: DIRTY_BYTES + valueFrom: + configMapKeyRef: + name: etcd-io-guard-config + key: dirty-bytes + - name: DIRTY_BACKGROUND_BYTES + valueFrom: + configMapKeyRef: + name: etcd-io-guard-config + key: dirty-background-bytes + securityContext: + privileged: true + resources: + requests: + cpu: 5m + memory: 8Mi + limits: + cpu: 50m + memory: 32Mi + volumeMounts: + - name: host-proc-sys + mountPath: /host/proc/sys/vm + - name: script + mountPath: /opt/guard + readOnly: true + volumes: + - name: host-proc-sys + hostPath: + path: /proc/sys/vm + type: Directory + - name: script + configMap: + name: etcd-io-guard-script + defaultMode: 0755 diff --git a/infrastructure/node-io/guard-script.sh b/infrastructure/node-io/guard-script.sh new file mode 100644 index 0000000..f1b864e --- /dev/null +++ b/infrastructure/node-io/guard-script.sh @@ -0,0 +1,39 @@ +#!/bin/sh +# Keep the host's writeback backlog bounded so an etcd WAL fdatasync does not +# wait for gigabytes of dind writeback. Idempotent: rewrites only on drift. +# +# Deliberately NOT done here: per-cgroup io.latency / io.max on the dind pod. +# io.latency only throttles *peers of the same parent* (cgroup-v2 doc, and +# block/blk-iolatency.c: a miss is recorded in the missing group's parent +# child_lat), and kubelet puts every pod in its own slice, so an etcd target +# can never throttle the dind pod - measured on 2026-10-26: dind depth stayed +# `max` and the stall count did not move. See README.md in the repo. +set -u +DB="${DIRTY_BYTES:-67108864}" +DBG="${DIRTY_BACKGROUND_BYTES:-16777216}" + +log() { echo "$(date -u +%FT%TZ) $*"; } + +log "start dirty_bytes=${DB} dirty_background_bytes=${DBG}" +while true; do + cur_bg="$(cat /host/proc/sys/vm/dirty_background_bytes 2>/dev/null || echo read-error)" + # dirty_background_bytes must be written before dirty_bytes: writing + # dirty_bytes zeroes dirty_ratio, and a nonzero dirty_background_bytes keeps + # dirty_background_ratio out of play. + if [ "${cur_bg}" != "${DBG}" ]; then + if echo "${DBG}" > /host/proc/sys/vm/dirty_background_bytes 2>/dev/null; then + log "applied vm.dirty_background_bytes=${DBG} (was ${cur_bg})" + else + log "FAILED to write vm.dirty_background_bytes" + fi + fi + cur_db="$(cat /host/proc/sys/vm/dirty_bytes 2>/dev/null || echo read-error)" + if [ "${cur_db}" != "${DB}" ]; then + if echo "${DB}" > /host/proc/sys/vm/dirty_bytes 2>/dev/null; then + log "applied vm.dirty_bytes=${DB} (was ${cur_db})" + else + log "FAILED to write vm.dirty_bytes" + fi + fi + sleep 30 +done diff --git a/infrastructure/node-io/kustomization.yaml b/infrastructure/node-io/kustomization.yaml new file mode 100644 index 0000000..4e4a7c2 --- /dev/null +++ b/infrastructure/node-io/kustomization.yaml @@ -0,0 +1,13 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: + - namespace.yaml + - configmap.yaml + - etcd-io-guard.yaml +configMapGenerator: + - name: etcd-io-guard-script + namespace: node-io + files: + - guard-script.sh +generatorOptions: + disableNameSuffixHash: false diff --git a/infrastructure/node-io/namespace.yaml b/infrastructure/node-io/namespace.yaml new file mode 100644 index 0000000..f678859 --- /dev/null +++ b/infrastructure/node-io/namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: node-io