Show image tags for each revisions of Kubernetes deployments
One of the useful and powerful function is the roll back for me. Basically, all applications on our cluster are deployed automatically by CodePipeline in AWS but if some trouble happen, rolling back to the previous version is one of our options. At that time, all I have to do is the following command.
1$ kubectl rollout undo deployment my-app
When I execute the following command, the list of the revisions which are still remaining on the database on the Kubernetes cluster will be displayed.
1$ kubectl rollout history deployment my-app
The command’s response is:
1REVISION CHANGE-CAUSE
229 <none>
330 <none>
431 <none>
532 <none>
633 <none>
734 <none>
835 <none>
936 <none>
1037 <none>
1138 <none>
1239 <none>
And then, if the application should be rolled back to the specific revisions, the following command will work.
1$ kubectl rollout undo deployment my-app --to-revision=35
However, I want to know which image was used for each revision because we use the date string like "yyyymmddHHMMSS" for image tags as I posted before.
Docker: What’s better image tag than latest, none, and versions?
So, If I can see the list of revision numbers and image tags will be helpful for me. However, there’s no function in the kubectl command so I made a shell script like this.
1#!/bin/bash
2deployment_name=${1:-NONE}
3
4if [ $deployment_name == NONE ];then
5 echo "USAGE: $0 DEPLOYMENT_NAME"
6 exit 1
7fi
8
9resource_name=`kubectl rollout history deployment $deployment_name -o name`
10echo "Resource name: $resource_name"
11for rev in `kubectl rollout history $resource_name | egrep '^[0-9]+' | awk '{print $1}'`
12do
13 image=`kubectl rollout history $resource_name --revision=${rev} | grep Image: | awk '{print $2}'`
14 printf "%02d : %s\n" $rev $image
15done
And its response is like this.
1Resource name: deployment.apps/my-app
232 : my-registry/my-app:202010061110
333 : my-registry/my-app:202010070906
434 : my-registry/my-app:202010080051
535 : my-registry/my-app:202010080202
637 : my-registry/my-app:202010130703
738 : my-registry/my-app:202010130728
839 : my-registry/my-app:202010080300
940 : my-registry/my-app:202010130754
1041 : my-registry/my-app:202010130823
1142 : my-registry/my-app:202010130823
1243 : my-registry/my-app:202010130940
As a result of this, I can choose the relevant revision by the deployment date.