➤ Rolling Updates

$ kubectl create -f deployment.yml

$ kubectl get pods
NAME                                READY     STATUS    RESTARTS   AGE
demo-app-1564180363-khku8           1/1       Running   0          14s
demo-app-1564180363-nacti           1/1       Running   0          14s
demo-app-1564180363-z9gth           1/1       Running   0          14s

Now, if we need to update the deployment or need to push the new version out, assuming CI has already pushed the new image to ECR, we can just copy and update the image URL.

$ kubectl set image deployment.apps/demo-app image=73570586743739.dkr.ecr.us-west-2.amazonaws.com/demo-app:v2.0
deployment.apps/demo-app image updated

$ kubectl annotate deployment.apps/demo-app kubernetes.io/change-cause="demo version changed from 1.0 to 2.0"

Check the status of the rolling update.
$ kubectl rollout status deployment.apps/demo-app
Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
deployment "demo-app" successfully rolled out

Finally, running get pods should now show only the new Pods.
$ kubectl get pods
NAME                                READY     STATUS    RESTARTS   AGE
demo-app-1564180365-khku8           1/1       Running   0          14s
demo-app-1564180365-nacti           1/1       Running   0          14s
demo-app-1564180365-z9gth           1/1       Running   0          14s


➤ Rolling Back
We may want to rollback a Deployment and Kubernetes By default maintain Deployment’s rollout history so that we can rollback anytime we want.

$ kubectl set image deployment.apps/demo-app image=73570586743739.dkr.ecr.us-west-2.amazonaws.com/demo-app:v3.0

$ kubectl annotate deployment.apps/demo-app kubernetes.io/change-cause="demo version changed from 2.0 to 3.0"

Verify the rollout status.
$ kubectl rollout status deployment.apps/demo-app
Waiting for rollout to finish: 1 out of 3 new replicas has been updated...

Looking at the Pods created, we can see that all the Pods are stuck.
$ kubectl get pods 
NAME                                READY     STATUS             RESTARTS   AGE
demo-app-1564180366-70iae           0/1       Running            0          3s
demo-app-1564180366-jbqqo           0/1       Running            0          3s
demo-app-1564180366-hysrc           0/1       Running            0          3s

We can see, it says 0 out of 1 ready.
We need to rollback the deployment to a stable version. To rollback, we need to check the rollout history.

$ kubectl rollout history deployment.apps/demo-app
deployments "demo-app"
REVISION    CHANGE-CAUSE
1           "from file demo-app.yml"
2           "demo version changed from 1.0 to 2.0"
3           "demo version changed from 2.0 to 3.0"

It shows revisions with change cause which we had added after updating the deployment, and in our case REVISION, 2 was stable.
We can rollback to a specific version by specifying it with "--to-revision" flag.

$ kubectl rollout undo deployment.apps/demo-app --to-revision=2
deployment.apps/demo-app rolled back

Check if the rollback was successful and the Deployment is running as expected, run.
$ kubectl get deployment demo-app
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
demo-app           3/3     3            3           12s

Check the status of pods as well.
$ kubectl get pods
NAME                                READY     STATUS            RESTARTS   AGE
demo-app-1564180365-khku8           1/1       Running           0          14s
demo-app-1564180365-nacti           1/1       Running           0          14s
demo-app-1564180365-z9gth           1/1       Running           0          14s


[LINKS]
https://blog.saeloun.com/2022/06/06/kubernetes-rollback/
