How to restart Kubernetes deployment with persistent volume claims?

6/30/2021

I have a deployment (starterservice) that deploys a single pod with a persistent volume claim. This works. However restart fails:

kubectl rollout restart deploy starterservice

The new pod is started before the old one has terminated and it cannot attach the volume (Multi-Attach error for volume "pvc-..."). I can work around this by scaling to zero and then back up to 1 instead:

kubectl scale --replicas=0 deployment/starterservice
kubectl scale --replicas=1 deployment/starterservice

I was wondering if there was a way to get kubectl rollout restart to wait for the old pod to terminate before starting a new one? Tx.

-- David Tinker
kubernetes

1 Answer

6/30/2021

You need to set deployment strategy = recreate.

spec:
  strategy:
    type: Recreate

The difference between the Recreate strategy compared to RollingUpdate (default) is that Recreate will terminate the old pod before creating new one while RollingUpdate will create new pod before terminating the old one.

https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#recreate-deployment

-- Lukman
Source: StackOverflow