Rolling out Updates

Kubernetes makes it easy to rollout updates to your applications using the builtin rolling update mechanism. In this chapter you will learn how to:

  • Modify deployments to tigger rolling updates

  • Pause and resume an active rolling update

  • Rollback a deployment to a previous revision

Tutorial: Rollout a new version of the Auth service

First look at the current history of the Deployment resource for the auth service:

kubectl rollout history deployment auth

Then modify the auth deployment to replace the pod’s container image version. Edit manifests/app/deployments/auth.yaml in the Cloud Shell editor and save.

Look for following line and upgrade version 1.0.0 to 2.0.0:

image: "kelseyhightower/auth:1.0.0"

With kubectl apply, you can live-update a deployment. --record option let you keep a trace of the commands related to each deployment change:

kubectl apply -f manifests/app/deployments/auth.yaml --record
kubectl describe deployments auth

Notice the events at the end of deployment desciption. To upgrade an application, the deployment-controller creates a new replica set with the new pod template to replace the old one.

Output

Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  44m    deployment-controller  Scaled up replica set auth-65c65db967 to 1
  Normal  ScalingReplicaSet  7m55s  deployment-controller  Scaled up replica set auth-69f6f69db6 to 1
  Normal  ScalingReplicaSet  7m55s  deployment-controller  Scaled down replica set auth-65c65db967 to 0

If you list ReplicaSets, you’ll see one for the original version and the other for the new version.

kubectl get replicasets

Now list Deployment history again to see your change history.

kubectl rollout history deployment auth

In fact, Deployment history is backed by the ReplicaSet

Tutorial: Pause and Resume an Active Rollout

It’s easy to deploy a new version using Kubernetes, but you’ll see that you can do that without any service interruption. So let’s try to see a deployment in progress.

First, you’ll scale the hello Deployment to 6 pods:

kubectl scale deployments hello --replicas=6

Look at your Deployment history, there is currently a single revision of your deployment

kubectl rollout history deployment hello

Now modify the hello deployment image to update hello docker image`s version:

sed -i.bak -e 's/hello:1.0.0/hello:2.0.0/' -e 's/replicas: 1/replicas: 6/' manifests/app/deployments/hello.yaml

Now, run following commands all together:

kubectl apply -f manifests/app/deployments/hello.yaml --record
kubectl describe deployments hello
kubectl rollout pause deployment hello
kubectl get replicaset

You’ll see the Deployment rolling-upgrade stopped in the middle. The last command show you two hello-xxxxxxx replicasets, with part of the 6 (or more) pods in each (like 5 and 2).

The deployment-controller is replacing old pods (in the old replicaset) one at a time with new pods (from the new replicaset), so that you’ll never have less than the 6 replicas asked, whatever the version.

Finally resume the rollout to terminates the rolling-upgrade.

kubectl rollout resume deployment hello
kubectl describe deployments hello

Notice in the event the progressive scale-up of the new replicaset and the scale-down of the old one.

Warning: to do rolling updates, your application must support running two version in parallel !

Exercise: Rollback the Hello service

Use the kubectl rollout undo command to rollback to a previous deployment of the Hello service.

Summary

In this chapter you learned how to rollout updates to your applications by modifying deployment objects to trigger rolling updates. You also learned how to pause and resume an active rolling update and rollback it back using the kubectl rollout command.