Leader Election via Lease

You run three copies of a controller for safety, yet only one acts at a time, and when it crashes another takes over on its own. How do they agree on who leads, and how do the others notice that it is gone?

Play the animation 4 steps, animated, plays on a computer or a tablet

Leader Election via Lease diagram: Leader election via Lease with acquire, renew, expire and failover

Leader Election via Lease, step by step

  1. All three replicas race for the Lease. The first write creates it, and the replica that sent it becomes holder. Every write after that is a compare-and-swap on resourceVersion.

    Play from step 1
  2. Only the leader runs control loops. It PUTs a fresh renewTime well inside leaseDurationSeconds (15s), and the standbys only check the Lease with a GET.

    Play from step 2
  3. Controller-mgr-1 crashes or is partitioned. A partitioned leader stands down at its renew deadline (10s), and a standby may acquire the Lease after leaseDurationSeconds.

    Play from step 3
  4. With the Lease expired, both survivors race for it 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

Sources