In Kubernetes, how to wait for a pod to finish deleting before starting new one

4/26/2020

I have a deployment where I need just one instance to be up at a time. When gracefully shutting down the pod, another starts up immediately -- without waiting for the pod to finish exiting. How can I make sure that the gracefully deleted pod is completely gone before the new pod starts up?

Things I've tried: 1. Rolling Update with maxSurge: 0 and maxUnavailable: 1

  strategy:
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
    type: RollingUpdate

Result: When gracefully deleting the pod, another instance immediately starts up, so there are two pods running at the same time, seemingly ignoring the maxSurge value.

  1. Same as 1, except using percents instead of integers

  2. Using Recreate instead of Rolling Update

strategy:
    type: Recreate

Result: same as 1

Is there a way to get the desired behavior directly in Kubernetes, or do you have to do something like add an init container with a delay, etc.?

-- cmm
kubernetes

2 Answers

4/26/2020

When gracefully deleting the pod, another instance immediately starts up

Recreate deployments wait for the termination of an old version only during a deployment upgrade. Except deployment upgrade it cannot ensure consistency.

Ref: https://github.com/kubernetes/kubernetes/issues/27362#issuecomment-524664492

-- hoque
Source: StackOverflow

4/26/2020

Use StatefulSets

StatefulSets are valuable for applications that require one or more of the following.

 - Stable, unique network identifiers. 
 - Stable, persistent storage.
 - Ordered, graceful deployment and scaling. 
 - Ordered, automated rolling updates.
-- FL3SH
Source: StackOverflow