3 min read
Reboot gates: trust the boot ID, not the SSH session
A rolling reboot script that waits for SSH can pass its health gate before the node has even gone down. Gate on a fact that can only change after the reboot.
Rolling reboots across a cluster follow a simple loop: reboot one node, wait until it is back and healthy, then move on to the next. Everything depends on the word back. If the gate says a node is back when it is not, the script reboots the next node while the first one is still going down, and a cluster that tolerates one missing member is suddenly missing two.
The gate that lied
My first version checked the obvious thing: can I open an SSH session and run a command?
ssh "$node" 'systemctl reboot'
until ssh -o ConnectTimeout=5 "$node" true; do sleep 5; done
# ...then check cluster health and move on
It passed almost immediately, which looked like a fast reboot. It was not. A host takes a while to shut down after it accepts the reboot command, and SSH keeps answering for part of that time. Connection reuse makes it worse: a multiplexed SSH connection can keep answering from the old session after a fresh connection would already have failed. On my hardware the false window was 40 to 80 seconds, long enough for the follow-up health checks to run against a node that had not rebooted yet, pass, and wave the script on to the next one.
Nothing was wrong with SSH. The gate was asking a question whose answer is the same before and after the event.
Ask a question only the new boot can answer
Linux gives every boot a random identifier in /proc/sys/kernel/random/boot_id. It is generated fresh at each boot and never repeats, which makes it a fact that can only change after a reboot. Read it before, read it again after, and the gate passes only when the value is different.
read_boot_id() {
ssh -o ControlMaster=no -o ControlPath=none -o ConnectTimeout=5 \
"$1" cat /proc/sys/kernel/random/boot_id 2>/dev/null
}
before=$(read_boot_id "$node")
ssh "$node" 'systemctl reboot' || true # the session may drop mid-command
deadline=$((SECONDS + 900))
until after=$(read_boot_id "$node") && [ -n "$after" ] && [ "$after" != "$before" ]; do
[ "$SECONDS" -lt "$deadline" ] || { echo "no new boot after 15 min" >&2; exit 1; }
sleep 5
done
Three details matter:
- Disable connection reuse for the check (
ControlMaster=no,ControlPath=none), so every probe is a brand-new connection and cannot be answered by the old session. - Fail closed on a deadline. A gate that waits forever turns a stuck node into a stuck pipeline nobody notices.
- A new boot is necessary, not sufficient. Once the boot ID has changed, the real health checks still have to run: cluster membership, storage state, the services that node hosts. They are now checking the right boot.
The general rule
This is one instance of a pattern I keep meeting: a check that is green for the wrong reason. A running process, an open port, a container marked healthy and an exit code of 0 all have the same weakness. They are true in states you did not mean to accept.
When I write a gate now, I ask one question first: could this check already pass before the thing I am waiting for has happened? If it could, the gate is measuring the wrong fact. Find the value that can only exist after the event (a new boot ID, a new version string, a request that returns real data) and gate on that instead.