0

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.

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.

I'm using microk8s and I wanted to just go to the good folder and execute microk8s kubectl apply -f myfilename and let kubernetes handle the rest with rolling update.

My issue was how to set dynamic value inside my .yaml file so the command would detect the change and start the process.

I've planned to do a bash script that do the job like the following:

file="my-file-deployment.yaml"
oldstr=`grep  'my' $file | xargs`
timestamp="$(date +"%Y-%m-%d-%H:%M:%S")"
newstr="value: my-version-$timestamp"
sed -i "s/$oldstr/$newstr/g" $file
echo "old version : $oldstr"
echo "Replaced String :  $newstr"

sudo microk8s kubectl apply -f $file

on my deployment.yaml file I'm giving the following env:

env:
- name: version
  value: my-version-2022-09-27-00:57:15

I'm switching with timestamp to a new value then I launch the command:

microk8s kubectl apply -f myfilename

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.

Is there a better solution to work with rolling update using microk8s?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
kevP-Sirius
  • 93
  • 1
  • 9
  • Tools like Helm or Kustomize could probably fill in this value a little more safely than sed(1). Are you essentially asking [How to rolling restart pods without changing deployment yaml in kubernetes?](https://stackoverflow.com/questions/57559357/how-to-rolling-restart-pods-without-changing-deployment-yaml-in-kubernetes) If you have a working solution, what are you hoping to change? – David Maze Sep 27 '22 at 13:09
  • my main goal is to change in fact the yaml file and register the change in a log file out the project folder but in the server and another tool will scrap log , and / or push the change on gitlab on a specific branch – kevP-Sirius Sep 28 '22 at 00:21

1 Answers1

2

If you are trying to trigger a rolling update on your deployment (assuming it is a deployment), you can patch the deployment and let the cluster handle the rollout. Here's a trick I use and it's literally a one-liner:

kubectl -n {namespace} patch deployment {name-of-your-deployment} \
  -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}"

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 rollingUpdate clause.

The date +'%s' 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.

We use this trick to force a rolling update when we have done an update that requires our pods to be restarted.

You can accompany this with the rollout status command to wait for the update to complete:

kubectl rollout status deployment/{name-of-your-deployment} -n {namespace}

So a complete line would be something like this if I wanted to rolling update my nginx deployment and wait for it to complete:

kubectl -n nginx patch deployment nginx \
  -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}" \
  && kubectl rollout status deployment/nginx -n nginx

One caveat, though. Using kubectl patch 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:

kubectl -n nginx patch deployment nginx \
  -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}" \
  --dry-run=client \
  -o yaml >patched-nginx.yaml
Blender Fox
  • 4,442
  • 2
  • 17
  • 30
  • Hi , your solution is pretty good too and yes i was planning to commit and push the change with the new version updated but i'll take your solution for sure as a good alt way to do it , thanks for sharing . – kevP-Sirius Sep 27 '22 at 07:34
  • That specific `kubectl patch` annotation is very similar to what `kubectl rollout restart deployment/some-name` does, FWIW. – David Maze Sep 27 '22 at 13:07
  • @David Maze , thanks for sharing ill take a try with this command on local to see if its working great for my use case – kevP-Sirius Sep 27 '22 at 14:24