<p>I wanted to share a solution I did with kubernetes and have your opinion on best practice to do in such case. I'm still new to kubernetes.</p>
<p>I had a problem I wanted to be able to update my application by restarting my deployment pod that execute all the necessary action to do that already in command start.</p>
<p>I'm using microk8s and I wanted to just go to the good folder and execute <code>microk8s kubectl apply -f myfilename</code> and let kubernetes handle the rest with rolling update.</p>
<p>My issue was how to set dynamic value inside my .yaml file so the command would detect the change and start the process.</p>
<p>I've planned to do a bash script that do the job like the following:</p>
<pre class="lang-bash prettyprint-override"><code>file=&quot;my-file-deployment.yaml&quot;
oldstr=`grep  'my' $file | xargs`
timestamp=&quot;$(date +&quot;%Y-%m-%d-%H:%M:%S&quot;)&quot;
newstr=&quot;value: my-version-$timestamp&quot;
sed -i &quot;s/$oldstr/$newstr/g&quot; $file
echo &quot;old version : $oldstr&quot;
echo &quot;Replaced String :  $newstr&quot;

sudo microk8s kubectl apply -f $file
</code></pre>
<p>on my deployment.yaml file I'm giving the following env:</p>
<pre class="lang-yaml prettyprint-override"><code>env:
- name: version
  value: my-version-2022-09-27-00:57:15
</code></pre>
<p>I'm switching with timestamp to a new value then I launch the command:</p>
<p><code>microk8s kubectl apply -f myfilename</code></p>
<p>it is working great for the moment. I still have to configure startupProbe to have a better rolling update execution because I'm having few second downtime which isn't cool.</p>
<p>Is there a better solution to work with rolling update using microk8s?</p>

kevP-Sirius
<p>If you are trying to trigger a rolling update on your deployment (assuming it is a deployment), you can <code>patch</code> the deployment and let the cluster handle the rollout. Here's a trick I use and it's literally a one-liner:</p>
<pre><code>kubectl -n {namespace} patch deployment {name-of-your-deployment} \
  -p &quot;{\&quot;spec\&quot;:{\&quot;template\&quot;:{\&quot;metadata\&quot;:{\&quot;annotations\&quot;:{\&quot;date\&quot;:\&quot;`date +'%s'`\&quot;}}}}}&quot;
</code></pre>
<p>This will patch your deployment, adding an annotation to the template block. In this way, the cluster thinks there is a change requiring an update to the deployment's pods, and will cycle them while following the <code>rollingUpdate</code> clause.</p>
<p>The <code>date +'%s'</code> will resolve to a different number each time so every time you run this, it will cause the cluster to cycle the deployment's pods.</p>
<p>We use this trick to force a rolling update when we have done an update that requires our pods to be restarted.</p>
<p>You can accompany this with the <code>rollout status</code> command to wait for the update to complete:</p>
<pre><code>kubectl rollout status deployment/{name-of-your-deployment} -n {namespace}
</code></pre>
<p>So a complete line would be something like this if I wanted to rolling update my nginx deployment and wait for it to complete:</p>
<pre><code>kubectl -n nginx patch deployment nginx \
  -p &quot;{\&quot;spec\&quot;:{\&quot;template\&quot;:{\&quot;metadata\&quot;:{\&quot;annotations\&quot;:{\&quot;date\&quot;:\&quot;`date +'%s'`\&quot;}}}}}&quot; \
  &amp;&amp; kubectl rollout status deployment/nginx -n nginx
</code></pre>
<p>One caveat, though. Using <code>kubectl patch</code> does not make changes to the yamls on disk, so if you wanted a copy of the change recorded locally, such as for auditing purposes, similar to what you are doing at the moment, then you could adapt this to do it as a dry-run and redirect output to file:</p>
<pre><code>kubectl -n nginx patch deployment nginx \
  -p &quot;{\&quot;spec\&quot;:{\&quot;template\&quot;:{\&quot;metadata\&quot;:{\&quot;annotations\&quot;:{\&quot;date\&quot;:\&quot;`date +'%s'`\&quot;}}}}}&quot; \
  --dry-run=client \
  -o yaml &gt;patched-nginx.yaml
</code></pre>
