419 of 419: a question about latency that ended in a full conformance pass

This one started with a short question: how slow is it between my nodes, and can I make it faster? I expected an afternoon of tuning. It turned into a small adventure. Along the way I learned how WireGuard keeps quiet, why a relay should be picked by a stopwatch and not a coin, and that a filesystem can give itself away with a number. It ended with litek8s passing all 419 specs of the official Kubernetes conformance suite, which it had never done.
Here is the trip, in order.
Step 1: you cannot speed up what you cannot see
The first finding was a little embarrassing: nothing measured it. The overlay between nodes (WireGuard, see home lab across NATs) knew a lot about each peer:
- bytes sent and received;
- the age of the last handshake;
- whether the path was direct or went through a relay.
It knew nothing about time.
There was already a probe: every 10 seconds each node sends a small UDP packet to each peer. But it goes to port 9, the discard port. Nobody answers on purpose, because the probe only has to arrive, so that WireGuard sees traffic. The only reply is a WireGuard keepalive, and that can take up to 10 seconds to come back. You cannot time anything with that.
So each node got a tiny echo service on its overlay address (UDP port 7, the classic echo port):
- once a round, every node pings every other node from that same address;
- the reply comes back the way pod traffic goes, through the relay when there is one;
- a reply that has not arrived by the next round counts as lost.
To keep it from being abused, it only answers addresses inside the cluster, with a packet exactly the size of the question.
The first test ran on a Linux workstation, with two nodes on the same machine:
| what | measured round trip |
|---|---|
| as is | 0.15 ms |
20 ms of delay added to one node (tc netem) |
20.18 ms |
| pings dropped with iptables | no value, pings lost go up |
It was measuring the right thing. Now there are two new metrics:
litek8s query 'avg_over_time(litek8s_overlay_peer_rtt_seconds[5m])'litek8s query 'rate(litek8s_overlay_peer_pings_lost_total[5m]) * 10' # share lostStep 2: a relay picked by coin toss
Measuring was only the first half; the question was also “make it faster”. When two nodes cannot reach each other directly (two homes behind different routers, say), their traffic goes through a relay: a node both can reach. Until now that relay was chosen with a hash: stable and fair, but blind. Picture a server in the next room and one on another continent. To the hash, both were equally good.
Now every agent reports its round trip to each peer it reaches directly.
The server picks the relay with the lowest total a → relay → b. Two
things took care:
- Both sides must agree. If the traffic goes through one relay and the reply through another, WireGuard drops the reply. The calculation is symmetric and uses the same data from both ends.
- It must not flap. Two relays at 31 and 32 ms would trade places on every measurement. A pair keeps its relay unless another is clearly faster: 25 % plus 2 ms. And the reports go in coarse steps (1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25 ms…), so they only change when the latency really does.
The test, bench/relay-rtt.sh, runs on a single Linux machine without
root:
- Five nodes in a dev cluster: two that cannot see each other and two relays.
- I cut the direct path, and the pair started going through a relay.
- I slowed that relay down by 30 ms.
Twenty seconds later the pair had moved to the other relay, both sides agreed, and pods kept talking to each other.
Step 3: “while I’m on Linux…”
To test all of that, I had been running real clusters on Linux, with the crun runtime, which is the configuration litek8s actually ships. That raised an uncomfortable question. The conformance runs I had published (413/419) were on macOS, with Docker underneath. I had never run the whole suite on Linux.
So I ran it. Result: 411/419. Worse than on the Mac, on the platform that matters most. That was the best part of the trip.
The volume that lied
Seven of the failures said the same thing. A pod writes into an
emptyDir with medium: Memory (a volume that lives in RAM) and asks
what kind of filesystem it is on. It expects tmpfs. It got
2435016766.
That number is 0x9123683E, the signature of btrfs: the workstation’s
disk. The “memory” volume was a plain directory on disk. The Docker
runtime did it right. The crun runtime ignored the label that said
“memory”. Every pod that thought it kept secrets or caches in RAM was
writing them to disk.
The fix is to mount a tmpfs on that volume, shared by all the pod’s containers as upstream does. It is unmounted before it is deleted, and mounted again if the node restarts.
The test was reading with a newer kubectl
Another spec failed on kubectl describe node: it looks for a line
called Kube-Proxy Version. The workstation had kubectl v1.37 installed,
against a v1.33 suite, and newer kubectl no longer prints that line. It
was not a bug in litek8s but in how I ran the suite. Now the script
downloads the kubectl of the suite’s own version.
Second run: 418/419.
The test that only fails on a cold node
The last one was the most interesting, because it had passed the first
time. A pod has two init containers: the first succeeds, the second
fails. The test watches every status change and demands that, while an
init container waits, its reason is PodInitializing.
litek8s reported ContainerCreating — Pulling image. It is honest, but it
is not what the kubelet says, because the kubelet publishes no status in
the middle of a pull. Why did it pass the first time? Because the image
was already on the node. This time the test happened to run first, on a
fresh node that had to download it. It was a bug that only shows up when
the cache is cold.
With that fixed, I ran the full suite once more, from scratch:
419/419. Every spec, in 21 minutes. The reference k3s scores 416/419 on the same suite.
Things that came along for the ride
With the hood open, two holes in docs/limitations.md were easy to close:
- The kubelet’s events. Until now, a failing probe or a restart loop
left no trace in
kubectl describe pod. Now you getUnhealthy(saying what the probe checks),Killing,BackOffandFailedfor pulls. A probe failing every second becomes one event with a counter, not thousands. imagePullSecrets. litek8s ignored them: credentials came only from the node’s configuration. Now a pod can pull from a private registry with its own Secret, or its ServiceAccount’s. I tested it against a registry that requires a login, with and without credentials.
What I take away
- Measure before you tune. The first piece of work was not to speed anything up, just to be able to see. Everything else followed from that.
- Test on the configuration you ship. The usual runs on the Mac hid a bug that wrote “memory” volumes to disk on every Linux node. One full run on the real platform found it in its first minutes.
- Watch out for tests that depend on the cache. A test that passes with a warm cache and fails with a cold one is not flaky. It is pointing at a bug.
- Use the client of the right version. The same API can look broken just because whoever reads it is newer.
Try it
From 0.2.22 (the full changelog is in CHANGELOG.md):
# Round trip between nodes, per peerlitek8s query 'litek8s_overlay_peer_rtt_seconds'
# Which relay each pair useskubectl get node <node> -o jsonpath='{.metadata.annotations.litek8s\.dev/wireguard-routes}'
# The relay test, on a Linux machine without rootLITEK8S_HOME=/tmp/lk-relay bench/relay-rtt.shThe conformance results, with the three runs (411, 418, 419), are in compat/CONFORMANCE.md.