homek8s/infrastructure/node-io/guard-script.sh

95 lines
3.5 KiB
Shell
Raw Permalink Normal View History

#!/bin/sh
# etcd-io-guard: keep etcd's WAL fdatasync tail short while the amd64 dind
# runner hammers the same PM991 NVMe (/dev/nvme0n1p3).
#
# Two layers, both re-applied on drift every 30 s:
#
# 1. Bound the host writeback backlog (vm.dirty_bytes / dirty_background_bytes).
# On the default vm.dirty_ratio=20 a 15 GB box allows ~3 GB of dirty pages,
# so an etcd WAL commit queues behind seconds of dind writeback. 64/16 MiB
# removes the multi-second (>8 s) stalls but, measured 2026-10-26, still
# leaves 1-4 s fdatasyncs and lets kube-controller-manager / kube-scheduler
# lose their leases, because 64 MiB at this device's throttled drain rate is
# still ~1.4 s of queue.
#
# 2. Cap the dind container's dispatch rate with blk-throttle io.max. This is
# NOT the peer-scoped io.latency controller (which cannot reach across
# kubelet's per-pod slices and was measured to do nothing): io.max is an
# absolute per-cgroup cap, so the dind cgroup can only keep N requests in
# the device queue and etcd's small WAL writes find free slots.
#
# The dind container's cgroup path changes on every pod restart, so layer 2
# re-discovers the scope by looking for the container whose main process is
# dockerd. Nothing here targets kubepods-burstable.slice (etcd lives in it).
set -u
DB="${DIRTY_BYTES:-67108864}"
DBG="${DIRTY_BACKGROUND_BYTES:-16777216}"
WMAX="${DIND_WBPS:-41943040}" # 40 MiB/s write bandwidth cap for dind
CGROOT="${CGROUP_ROOT:-/host/sys/fs/cgroup}"
PROCROOT="${PROC_ROOT:-/host/proc}"
log() { echo "$(date -u +%FT%TZ) $*"; }
apply_writeback_limits() {
cur_bg="$(cat /host/proc/sys/vm/dirty_background_bytes 2>/dev/null || echo read-error)"
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
}
# Echo the cgroup directory of the dind container, or nothing.
find_dind_cgroup() {
for procs in "${CGROOT}"/kubepods.slice/kubepods-*.slice/kubepods-*-pod*.slice/cri-containerd-*.scope/cgroup.procs; do
[ -f "${procs}" ] || continue
dir="${procs%/cgroup.procs}"
while read -r pid; do
[ -n "${pid}" ] || continue
comm="$(cat "${PROCROOT}/${pid}/comm" 2>/dev/null || true)"
if [ "${comm}" = "dockerd" ]; then
echo "${dir}"
return 0
fi
done < "${procs}"
done
return 1
}
apply_io_max() {
dir="$(find_dind_cgroup)" || return 1
want="259:0 wbps=${WMAX}"
cur="$(cat "${dir}/io.max" 2>/dev/null || true)"
# blk-throttle reads back all four fields ("259:0 rbps=max wbps=40M ..."),
# so match on the wbps field alone, not on the exact written string.
case "${cur}" in
*"wbps=${WMAX}"*) : ;;
*)
if echo "${want}" > "${dir}/io.max" 2>/dev/null; then
log "applied io.max '${want}' to ${dir#"${CGROOT}"} (was '${cur}')"
else
log "FAILED to write io.max to ${dir#"${CGROOT}"}"
fi
;;
esac
}
log "start dirty_bytes=${DB} dirty_background_bytes=${DBG} dind_wbps=${WMAX}"
while true; do
apply_writeback_limits
if ! apply_io_max; then
log "no dockerd cgroup found yet (runner pod down?)"
fi
sleep 30
done