When processing a rolling update with database migrations, how does kubernetes handle this?
For an instance - I have an app that gets updated from app-v1 to app-v2, which includes a migration step to alter an existing table. So this would mean it requires me to run something like db:migrate
for a rails app once deployed.
When a rolling deployment takes place on 3 replica set. It will deploy from one pod to another. Potentially allowing PODs that don't have the new version of the app to break.
Although this scenario is not something that happens very often. It's quite possible that it would. I would like to learn about the best/recommended approaches for this scenario.
Rolling Deployment A rolling deployment is the default deployment strategy in Kubernetes. It replaces the existing version of pods with a new version, updating pods slowly one by one, without cluster downtime.
Rolling updates allow Deployments' update to take place with zero downtime by incrementally updating Pods instances with new ones. The new Pods will be scheduled on Nodes with available resources.
Rolling Update Deployment. The rolling deployment is the default deployment strategy in Kubernetes. It replaces pods, one by one, of the previous version of our application with pods of the new version without any cluster downtime.
One way to prevent an old version from breaking is to split a migration into multiple steps.
E.g. you want to rename a column in the database. Renaming the column directly would break old versions of the app. This can be split into multiple steps:
This is unfortunately quite a hassle, but prevents having a downtime with a maintenance page up.
Kubernetes does NOT natively handle rolling updates with db migrations. This is application specific, so the application developer will have to handle it. You may have to do the same things as you would do in a non-k8s setting.
Anyway, the way I would do it is:
This is not fool-proof but doesn't involve code change. There is a small window between the db:migrate step and the actual server listening where a request will go to the older replica (which will terminate as soon as the new replica is ready). That request may or may not fail depending on whether the code block is directly related to the schema change.
If I didn't care much about downtime, then I would just use the recreate strategy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With