Intro

Through most of my career, I worked with high performance schedulers. PBS, Torque, Slurm, and then Mesos before Kubernetes took over the world.

I remember the first time I evaluated Kubernetes for replacing Mesos, and I was struck by the absurdly low 60 pods per second launch performance.

Really, 60 pods/s?
The 60 pods/s is a super early performance number. It traces back to CoreOS’s 2016 post “Improving Kubernetes Scheduler Performance”, which measured ~60 pods/s average scheduling throughput on a 100-node cluster (Kubernetes ~1.1/1.2 era). Granted, it is a super super old number, but was correct for the time.

I thought for sure nobody could possibly be using this for anything serious.

I learned that was sort of true. Nobody was using it for high performance batch scheduling, for sure. But for web apps that was fine (service), and anything that wasn’t batch-centric (run to completion).

That’s okay. High performance scheduling wasn’t a key design decision in Kubernetes’ model.

And for sure, premature optimization is the root of all evil, right?

Why Kubernetes is slow

Kubernetes was never designed to replace high performance computing batch schedulers. HPC batch schedulers are just a completely different breed, with different concerns.

I already knew why Kubernetes is slow for high performance scheduling. Just take a look at its architecture.

graph LR kubectl([kubectl]) --> API CM[controller-manager] <-->|watch / write| API Sched[kube-scheduler] <-->|watch pods / write binding| API API[kube-apiserver] <-->|gRPC over TCP| etcd[(etcd)] API <-->|watch / status| K1[kubelet] API <-->|watch / status| K2[kubelet] API <-->|watch / status| K3[kubelet]

Kubernetes isn’t really a scheduler as much as a generic API in front of etcd, a generic CRUD object store for any kind of object.

It just happens to be able to run pods as a side effect (/s).

Karavel design

I wondered if I could “just” rewrite the Kubernetes “microservices” to make it a high performance scheduler monolith.

But also, could I still retain the flexibility of being able to handle arbitrary objects through an API server? (making it compatible with all existing k8s yaml)

Karavel is the result of this research.

graph LR kubectl([kubectl]) --> API CM[controller-manager] <-->|watch / write| API subgraph CN["Control node"] API[stock kube-apiserver] subgraph Core["Karavel core (Rust)"] Arena[(arena: protobuf objects)] Sched[scheduler] GW[gateway] end API <-->|etcd API over a Unix socket| Core end GW <-->|kubelet API subset| K1[kubelet] GW <-->|kubelet API subset| K2[kubelet] GW <-->|kubelet API subset| K3[kubelet]

In Karavel, what used to be etcd, the scheduler, the kubelet-facing API and the node lifecycle controller are collapsed into a single Rust process.

The API server itself stays a stock process on the same machine. It talks over a local Unix socket that speaks the etcd protocol.

Even though I’m not using etcd, there is still a raft-based replication protocol between control nodes for HA.

In Karavel, because the pod is a first-class object, kubelets and the scheduler are on the fast path.

They don’t talk through the Kubernetes API server.

They talk to the control nodes through a thin gateway. A stock, unmodified kubelet speaks to karavel-gateway, which serves the kubelet-facing API straight from memory.

Control nodes

The control nodes are built with high performance in mind.

There’s one large arena of memory storing all of the objects (pods included) in their native protobuf format.

Here are the major departures from what a “normal” etcd storage backend would do:

  1. Heartbeats aren’t writes. Kubelets PUT a Lease every 10 s. At 1M nodes that’s 100k writes/s, let’s not write this down, it’s just ephemeral data. Only “node unreachable” / “node recovered” transitions are logged.
  2. One disk sync per batch. The applier batches up to 4096 writes, one synced RocksDB write.
  3. The Scheduler runs in-process over a columnar index. Packed arrays of allocatable resources, taints and labels, instead of re-decoding EVERY pod and node through watch streams. This is 50 to 300 times upstream’s scheduler_perf thresholds.
  4. Watches are log cursors over already-encoded bytes. No per-watcher re-serialization. Events are routed only to the node that holds them instead of filtered per watcher. I just hate all the serialization and deserialization that happens in k8s.
  5. Rust, single writer thread. No GC at all! The k8s-1m write-up named Go’s GC and the apiserver watch-cache lock as upstream’s real ceiling, and that doesn’t exist in Karavel.

Fast-path kinds are Pod, Node and Lease. Everything else, including CRDs, is opaque bytes in the store.

Wait, aren’t we supposed to escape monoliths?

If you’ve read Google’s “Borg, Omega, and Kubernetes” paper, this might feel like a step backwards.1

The lesson it is most insistent about is “don’t expose raw state.” Borg was a monolith that knew the semantics of every operation. Omega made the store passive and pushed all the logic out into its clients. Kubernetes picked the middle ground: a general-purpose API server in front of the store, enforcing validation, versioning and policy.

I kept that middle ground. The stock kube-apiserver is still the front door. etcd was never the client-facing API anyway. Swapping etcd for a Rust arena is still a private API.

Where Karavel does look like the old world is the fast path (pods). The store, the scheduler and the node lifecycle collapse back into one monolithic process. kubelets and the scheduler read state directly instead of going through the API server. Isn’t that…. bad???

Bad for who? Because it is great for performance! The leak is scoped to three kinds: Pod, Node and Lease. Everything else is bytes in the store behind the API server like normal.

To me, the more interesting architecture question is: what is the relationship to the backing store?

Kubernetes put a general-purpose API in front of a general-purpose store (etcd). Karavel keeps the general-purpose API and puts a special-purpose store behind it for the three kinds on the hot path (pods).

If you can stomach this, then you can get insane scaling and performance!

Compatibility

Thanks to Kubernetes’ exhaustive test suite and benchmark suite, I can confidently tell you that Karavel is compatible with (almost) everything else Kubernetes!

Remember, it’s running stock kubelet on the node. All extra controllers talk to the stock Kubernetes API server, exactly as they do on any cluster.

The front door (the thing kubectl and every controller talk to) is the stock, unmodified kube-apiserver binary. The Rust core impersonates etcd over a local Unix socket, so the API server stores into Karavel through the normal etcd v3 API. I’m not the first person to use this trick.2

In a sense, it is “easy” to be compatible with normal k8s because of all the standard interfaces.

At the same time, it is “easy” to outperform normal k8s by centralizing logic and building for speed.

Benchmarks

I never got to run the full million. :(

I couldn’t convince GCP to up my quota on a new account.

The largest run was 100,000 nodes and 100,000 pods, on a single 32-vCPU machine, with a fleet of kwok fake nodes on two other servers.

Here is Karavel against stock Kubernetes v1.37.1 on that same machine, and bchess’s super sharded k8s-1m attempt:

xychart-beta title "Time to 100,000 nodes Ready (seconds, lower is better)" x-axis ["Karavel", "Stock Kubernetes"] y-axis "Seconds" 0 --> 80 bar [20, 71]
xychart-beta title "Pods scheduled and Running (pods/s, higher is better)" x-axis ["Karavel", "k8s-1m", "Stock Kubernetes"] y-axis "Pods per second" 0 --> 17000 bar [16000, 14000, 156]
xychart-beta title "Control-plane CPU at 100k nodes (peak cores, lower is better)" x-axis ["Karavel", "Stock Kubernetes"] y-axis "Peak cores" 0 --> 28 bar [2.8, 25.5]
xychart-beta title "Scheduling efficiency (pods/s per core, higher is better)" x-axis ["Karavel", "Stock Kubernetes", "k8s-1m"] y-axis "Pods/s per core" 0 --> 6000 bar [5714, 6, 1.5]

The k8s-1m bars are bchess’s original run. It reached about the same pod throughput as Karavel, but its control plane was an insane 9,646 cores! (five apiservers, 289 scheduler replicas, and an etcd box)

I’ll keep trying to get my quota up’d to run it on at least 1 million kwok nodes.

This is a personal project, so I will never have the cash to burn it on 1 million real nodes.

Let me know if you have access to 1 million servers to try!

Trade-offs

You might think that I’m going to say you can either pick the battle-hardened slow normal k8s distribution, or you can pick my fast vibe-coded super slop.

But I won’t. The point of Karavel was never to convince you to run it in production tomorrow.

The biggest tradeoff here for picking a high performance k8s rewrite is losing the ability to sleep at night knowing you are using a battle-tested industry-vetted piece of software.

Conclusion

The 60 pods per second launch rate from the early k8s days stuck with me. I’m glad I could push k8s to a new upper bound of 16k pods per second.

When I read bchess’s original k8s-1m attempt, I was also a little sad, imagining all of the wasted CPU cores dedicated to just shipping protobufs around and the 289 copies of kube-scheduler.

Surely there must be a better way. That better way is to consolidate the microservices back to a monolith, like the good old days!

We already have multiple different Kubernetes distributions and implementations, like k3s, k0s, MicroK8s, Talos, and OpenShift, not to mention the hidden control planes behind EKS, GKE, and AKS.

Some of them already swap the core out: k3s runs the API server on kine instead of etcd.

Maybe someday there will be a hyperscale distribution of k8s that runs Karavel or something like it?


  1. “Borg, Omega, and Kubernetes”, Burns, Grant, Oppenheimer, Brewer and Wilkes, ACM Queue 14(1), 2016. ↩︎

  2. kine, from k3s, does the same thing to run the API server on SQLite, Postgres or MySQL instead of etcd. ↩︎


Comment via email