3

Summary

Can I give a deployment the rollout strategy Recreate and also set a fixed maxSurge for the deployment?

More details

I am developing an application that runs in Kubernetes. The backend will have multiple replicas, and runs EF Core with database migrations. I understand there are several ways to solve this; here's my idea at the moment.

On a new release, I would like all replicas to be stopped. Then a single replica at a time should start, and for each replica there should be an init container that runs the migrations (if needed).

This seems to be possible, using the following two configuration values:

  • .spec.strategy.type==Recreate and
  • .spec.strategy.rollingUpdate.maxSurge==1

Is it possible to use these two together? If not, is there any way to control how many replicas a controller will start at once with the Recreate strategy?

"No! You should do this in a completely different way!"

Feel free to suggest other methods as well, if you think I am coming at this from the completely wrong angle.

David Maze
  • 130,717
  • 29
  • 175
  • 215
MW.
  • 12,550
  • 9
  • 36
  • 65
  • How did you end up solving this? – LeonG Sep 03 '20 at 09:08
  • @LeonG I changed from a Deployment to a StatefulSet. I gave it `updateStrategy.type: OnDelete`. Then, as part of the release pipeline, I run a command which manually deletes all the pods in the stateful set. The result is that Kubernetes (after deleting all of the pods) recreates a single pod at a time, until the replica count has been satisfied. The downside is that _each_ replica runs the migration, when in reality only the first replica would need to. But that's acceptable, I figure. – MW. Sep 03 '20 at 11:57
  • Thanks for the update, I ended up opting for a Job in Kubernetes that is triggered by Helm hooks, and calls the application container with an extra parameter to just run the migrations and exit. Jobs compared to init containers are only run once, and not per configured replica. – LeonG Sep 08 '20 at 08:38

1 Answers1

4

Statefulset might help you in this case.

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.
hoque
  • 5,735
  • 1
  • 19
  • 29