Leader Election via Lease
When you run several copies of a controller for high availability, how do they avoid all acting at once? They race to claim a single Lease object, and only the winner runs the control loops while the rest stand by and poll it. The holder keeps renewing the Lease to prove it is alive, and once renewals stop for a full lease duration a standby takes it over, so leadership moves after a crash on its own. The Lease records the holder and counts every handover.
Play the animation 4 steps, animated, best on a wide screen
Leader Election via Lease, step by step
-
All three replicas race for the Lease. The first write creates it and its sender becomes holder. Every write after that is a compare-and-swap on resourceVersion.
Play from step 1 -
Only the leader runs control loops. It PUTs a fresh renewTime well inside leaseDurationSeconds (15s), and the standbys only GET the Lease to check it.
Play from step 2 -
Controller-mgr-1 crashes or is partitioned. A partitioned leader stands down at its renew deadline (10s), and a standby may acquire after leaseDurationSeconds.
Play from step 3 -
With the Lease expired, both survivors race again. Controller-mgr-2 wins the CAS and becomes holder, so the control loops resume there after about a lease duration.
Play from step 4