<p>We're moving a legacy app to Kubernetes. We will have many instances of it running (a Kubernetes namespace per customer), so we want to automate our application upgrade process.</p>
<p>Kubernetes has well established patterns for rolling upgrades, but I can't use them (yet). My application requires the following process:</p>
<ol>
<li>all existing pods are deleted</li>
<li>a database upgrade job (Kubernetes Job) runs and completes successfully</li>
<li>new pods are created</li>
</ol>
<p>We define the pods through a Deployment. The database upgrade job is idempotent as long as we never run more than one at a time.</p>
<p>I assume my workflow is not an uncommon one for legacy applications, and yet I can't find any established patterns or tools preconfigured. Is there something out there? If I do have to write my own operator (or use something like Kudo), what is the best set of steps for it to perform?</p>

Jesse McDowell
<p>Yes, there is an existing process for that:</p>
<ol>
<li><p>Use the <code>kubectl scale</code> command to scale down the existing Deployment to zero replicas: <code>kubectl scale --replicas=0 deploy/my-legacy-deployment</code></p>
</li>
<li><p>Wait for that to stabilize (there's your requested downtime ;-)</p>
<p>Using <code>kubectl wait</code> will be helpful, although I personally don't have experience introducing downtime in order to know the remaining arguments to <code>wait</code> to suggest here</p>
<p>You can also use something like <code>while true; do [[ 0 -eq $(kubectl get pods -o name -l legacy-deployment-selector | wc -l) ]] &amp;&amp; break; done</code> to stall until there are no pods remaining</p>
</li>
<li><p>Run your database job, or migrations of your choosing</p>
</li>
<li><p>Deploy the new version as you normally would; depending on the tool you use, this may or may not actually influence the currently zero-scaled Deployment</p>
<p>For example, <code>kubectl set image deploy/my-legacy-deployment &quot;*=example.com/my/new/image&quot;</code> will leave the Deployment at zero replicas</p>
<p>But a <code>helm install --upgrade legacy whatever-else</code> may very well set the Deployment's replicas to the value found in the chart</p>
</li>
<li><p>If your tool has not yet scaled up the new Deployment, you can now set it back to the desired value with the opposite command: <code>kubectl scale --replicas=3 deploy/my-legacy-deployment</code></p>
</li>
</ol>
