Search code examples
kuberneteskubernetes-podorchestration

Start one pod at a time when replica is greater than one


Is there a way to ensure that pods are scaled one at a time when setting replica greater than one?

Example: Replica set to 3

  1. Pod 1 - Initializing , pod 2 - Waiting, pod 3 - Waiting
  2. Pod 1 - Running , pod 2 - Initializing, pod 3 - Waiting
  3. Pod 1 - Running , pod 2 - Running, pod 3 - Initializing
  4. Pod 1 - Running , pod 2 - Running, pod 3 - Running

Solution

  • You can acomplish this behavior using StatefulSets. As it goes from Kubernetes docs

    • For a StatefulSet with N replicas, when Pods are being deployed, they are created sequentially, in order from {0..N-1}.
    • When Pods are being deleted, they are terminated in reverse order, from {N-1..0}.
    • Before a scaling operation is applied to a Pod, all of its predecessors must be Running and Ready.
    • Before a Pod is terminated, all of its successors must be completely shutdown.

    So, as you can see here, new pod is not booted up until previous one is initializing.

    Note: this behavior is guranteed by Kubernetes when OrderedReady pod management policy is used (which is default).