Three replicas, but only one may reconcile An operator runs three replicas so it survives a node failure. If all three reconcile at once, they create the same child objects twice, overwrite each other and retry on conflicts forever.
So the replicas elect a leader. Only the leader runs the controllers. The others stay warm and wait to take over.
A Lease object records who holds the lock The lock is a Lease in the API group coordination.k8s.io/v1. spec.holderIdentity names the current leader, spec.leaseDurationSeconds says how long the claim is valid, spec.renewTime is when the leader last renewed it.
spec.acquireTime and spec.leaseTransitions record when and how often leadership changed hands. kube-controller-manager and kube-scheduler keep theirs in kube-system.
All replicas try to take it, and only one write wins Each replica tries to create or update the Lease with itself as holder. The API server uses optimistic concurrency: an update must carry the resourceVersion it read. The first write succeeds, the others get a conflict.
Replica A wins. Its client starts the controllers (in client-go, the OnStartedLeading callback runs). B and C read the Lease, see a valid holder and keep watching.
Key idea Leader election needs no extra system. It is built on the same compare-and-swap that protects every object in the API server.
The leader renews, the followers retry Three timings drive this. The leader updates renewTime every retryPeriod. If it cannot renew within renewDeadline, it gives up leadership. Followers try to acquire every retryPeriod, but only succeed after leaseDuration has passed without a renewal.
kube-controller-manager defaults to a 15 s lease duration, 10 s renew deadline and 2 s retry period. Operators built on controller-runtime use similar defaults.
Key idea leaseDuration must be longer than renewDeadline. The gap is the safety margin in which the old leader has already stopped before anyone else may start.
The leader dies, and a follower takes over The node running Replica A fails. renewTime stops moving. Followers do not compare their clocks with A. Each one notes on its own clock when it last saw the Lease change, and waits leaseDuration from there.
Then B updates the Lease to itself. leaseTransitions goes up by one, B starts its controllers and resumes from the current state in the API server. Reconciliation pauses for roughly one lease duration.
A clean shutdown should hand over the Lease On a rolling update, the old leader is stopped on purpose. If it just exits, the next leader waits a full lease duration. If it releases the Lease on shutdown (ReleaseOnCancel in client-go), a follower can take it at once.
Release only after the controllers have really stopped. Releasing first and stopping later leaves a moment with two active writers.
A paused leader wakes up and still thinks it leads Now B freezes for 30 s: a long garbage collection pause, a frozen VM, a stuck network. Its Lease expires and C takes over. When B wakes up, code that already checked I am leader keeps going and writes to the managed objects.
Clock problems make it worse. client-go tolerates clocks that disagree, but not clocks that run at different rates. If the leader clock runs slow, it believes its lease is still valid after the followers consider it expired.
Watch out The client-go documentation says it plainly: leader election does not guarantee that only one client acts as leader. For that you need fencing.
Fencing makes a stale leader harmless Fencing means the thing being written rejects a writer that is no longer current. Inside Kubernetes, updates that carry a resourceVersion already do part of this: a write based on an old read gets a conflict.
Keep reconcile idempotent and stop at once when renewal fails, as kube-controller-manager does by exiting. For an external system such as a database, pass a token that grows with each leadership change, for example leaseTransitions, and let the system refuse older tokens.
Node heartbeats are Leases too Every node has a Lease with the same name in the kube-node-lease namespace. The kubelet renews it every 10 seconds by default, separately from the much rarer full Node status updates.
The node lifecycle controller watches these renewTimes. When a node stops renewing for longer than the node monitor grace period (tens of seconds by default), its Ready condition becomes Unknown.
Key idea Lease renewals are tiny writes. That is why heartbeats moved from Node status to Leases: less load on the API server and etcd in big clusters.
Coordinated leader election lets the API server choose In plain leader election, whoever writes first wins. During an upgrade that can be a newer kube-controller-manager than the API servers support. Coordinated leader election changes that: candidates publish LeaseCandidate objects, and the API server picks the leader.
The built-in strategy OldestEmulationVersion prefers the oldest version, so mixed-version upgrades stay safe. It is beta since v1.33 but off by default, behind the CoordinatedLeaderElection feature gate and the coordination.k8s.io/v1beta1 API.
A Lease picks one leader, fencing keeps it honest Replicas compete to write a Lease. The holder renews it every retryPeriod and gives up after renewDeadline. Followers take over once leaseDuration passes with no renewal, and leaseTransitions counts each change.
Leases give you a leader most of the time, not a guarantee. Pauses and clock rate skew can make two replicas act at once, so writes should be conditional and idempotent.
Summary Check who leads with kubectl get lease -n kube-system. If reconciliation stalls after a node failure, expect roughly one lease duration of pause before the next leader starts.