ReplicaSet Reconcile and Ownership

A ReplicaSet replaces a deleted Pod, pulls in a bare Pod whose labels match its selector, and lets a Pod relabelled off the selector drop out and keep running. How does it keep count, and which Pods are its own?

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

ReplicaSet Reconcile and Ownership diagram: ReplicaSet controller

ReplicaSet Reconcile and Ownership, step by step

  1. Every Pod the ReplicaSet manages carries a metadata.ownerReferences entry pointing back to it, with controller set to true. The links in the owned band show that ownership, and those entries let garbage collection remove the Pods when the ReplicaSet is deleted. This ReplicaSet is standalone, no Deployment owns it, so to scale it you edit its own spec.replicas, and it is what creates and deletes the Pods. A ReplicaSet owned by a Deployment also selects on a pod-template-hash label.

    Play from step 1
  2. The controller runs a continuous reconcile loop. On every relevant change it compares the desired count against the Pods it owns and acts only on the difference. The mark on the band is spec.replicas, so if the set ends exactly on the mark nothing needs doing. The loop is level-triggered, which means it works off the current observed state rather than off one-time events. So a missed event or a controller restart still converges to the same result.

    Play from step 2
  3. One Pod is lost. It was deleted, or its Node failed and the Pod was evicted once its default 300 second toleration ran out. A Pod being deleted no longer counts, so the owned band falls to two, a gap opens before the mark, and the controller creates a replacement to close that gap. The controller learns of the loss through its Pod watch and acts on the difference, not on the event. This is self-healing. A bare Pod created on its own, though, has no owner watching it, so once gone it stays gone.

    Play from step 3
  4. A standalone Pod is already running with the label app=web and no controller ownerReference. That is exactly what the unowned band holds. The ReplicaSet matches Pods by selector and not by who created them, so it adopts this Pod and PATCHes metadata.ownerReferences to point at itself. Adoption only restamps the owner, so the Pod keeps running and crosses up into the owned band past the mark as a fourth replica.

    Play from step 4
  5. Adoption pushed the owned count to four, one past the mark. The same reconcile loop now deletes a Pod to return to exactly three. A ReplicaSet never settles above its desired count, no matter where the extra Pod came from. When picking the victim it prefers Pending and unschedulable Pods, then not ready ones, then the lowest controller.kubernetes.io/pod-deletion-cost value, honored best effort, not guaranteed.

    Play from step 5
  6. The reverse of adoption. A Pod is relabelled from app=web to app=debug, so it no longer matches the selector. The ReplicaSet releases it by removing its ownerReference, and it drops into the unowned band, still running. That leaves the owned band short of the mark, so the same loop creates a replacement to close the gap. Labels decide membership, so changing them moves a Pod between the two bands.

    Play from step 6

Sources