Deployment update strategy – rolling-update

The following will introduce the subcommands edit and set, for the purpose of updating the containers under Deployment:

  1. First, let's update the Pods in Deployment with a new command:
// get into editor mode with the command below
// the flag "--record" is for recording the update
// add the command argument as below and save the change
$ kubectl edit deployment simple-nginx --record
spec:
replicas: 5
...
template:
...
spec:
containers:
- image: nginx
command:
- sh
- -c
- echo "Happy Programming with Kubernetes!" > /usr/share/nginx/html/index.html && service nginx stop && nginx -g "daemon off;"
imagePullPolicy: Always
...
deployment.extensions "simple-nginx" edited

We are not only doing the update; we record this change as well. With the flag --record, we keep the command line as a tag in revision.

  1. After editing the Deployment, check the status of rolling-update with the subcommand rollout right away:
// you may see different output on your screen, but definitely has the last line showing update successfully
$ kubectl rollout status deployment simple-nginx
Waiting for rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for rollout to finish: 1 old replicas are pending termination...
Waiting for rollout to finish: 1 old replicas are pending termination...
Waiting for rollout to finish: 1 old replicas are pending termination...
Waiting for rollout to finish: 4 of 5 updated replicas are available...
deployment "simple-nginx" successfully rolled out

It is possible that you get several Waiting for … lines, as shown in the preceding code. They are the standard output showing the status of the update.

  1. For whole updating procedures, check the details of the Deployment to list its events:
// describe the Deployment again
$ kubectl describe deployment simple-nginx
Name: simple-nginx
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 1h deployment-controller Scaled up replica set simple-nginx-585f6cddcd to 5
Normal ScalingReplicaSet 1h deployment-controller Scaled up replica set simple-nginx-694f94f77d to 1
Normal ScalingReplicaSet 1h deployment-controller Scaled down replica set simple-nginx-585f6cddcd to 4
Normal ScalingReplicaSet 1h deployment-controller Scaled up replica set simple-nginx-694f94f77d to 2
Normal ScalingReplicaSet 1h deployment-controller Scaled down replica set simple-nginx-585f6cddcd to 3
Normal ScalingReplicaSet 1h deployment-controller Scaled up replica set simple-nginx-694f94f77d to 3
Normal ScalingReplicaSet 1h deployment-controller Scaled down replica set simple-nginx-585f6cddcd to 2
Normal ScalingReplicaSet 1h deployment-controller Scaled up replica set simple-nginx-694f94f77d to 4
Normal ScalingReplicaSet 1h deployment-controller Scaled down replica set simple-nginx-585f6cddcd to
Normal ScalingReplicaSet 1h deployment-controller Scaled up replica set simple-nginx-694f94f77d to 5
Normal ScalingReplicaSet 1h deployment-controller (combined from similar events): Scaled down replica set simple-nginx-585f6cddcd to 0

As you see, a new replica set simple-nginx-694f94f77d is created in the Deployment simple-nginx. Each time the new ReplicaSet scales one Pod up successfully, the old ReplicaSet will scale one Pod down. The scaling process finishes at the moment that the new ReplicaSet meets the original desired Pod number (as said, 5 Pods), and the old ReplicaSet has zero Pods.

  1. Go ahead and check the new ReplicaSet and existing Service for this update:
// look at the new ReplicaSet in detail, you will find it copied the labels of the old one
$ kubectl describe rs simple-nginx-694f94f77d
Name: simple-nginx-694f94f77d
Namespace: default
Selector: env=test,pod-template-hash=2509509338,project=My-Happy-Web,role=frontend
Labels: env=test
pod-template-hash=2509509338
project=My-Happy-Web
role=frontend
...
// send request to the same endpoint of Service.
$ curl $SERVICE_URL
Happy Programming with Kubernetes!
  1. Let's make another update! This time, use the subcommand set to modify a specific configuration of a Pod.
  2. To set a new image to certain containers in a Deployment, the subcommand format would look like this: kubectl set image deployment <Deployment name> <Container name>=<image name>:
// change the image version with the subcommand "set"
// when describing the deployment, we can know that the container name is the same as the name of the Deployment
// record this change as well
$ kubectl set image deployment simple-nginx simple-nginx=nginx:stable --record
deployment.apps "simple-nginx" image updated
What else could the subcommand "set" help to configure? 
The subcommand set helps to define the configuration of the application. Until version 1.9, CLI with set could assign or update the following resources:
Subcommand after set Acting resource Updating item
env Pod Environment variables
image Pod Container image
resources Pod Computing resource requirement or limitation
selector Any resource Selector
serviceaccount Any resource ServiceAccount
subject RoleBinding or ClusterRoleBinding User, group, or ServiceAccount
  1. Now, check if the update has finished and whether the image is changed:
// check update status by rollout
$ kubectl rollout status deployment simple-nginx
...
deployment "simple-nginx" successfully rolled out
// check the image of Pod in simple-nginx
$ kubectl describe deployment simple-nginx
Name: simple-nginx
...
Pod Template:
Labels: env=test
project=My-Happy-Web
role=frontend
Containers:
simple-nginx:
Image: nginx:stable
Port: 80/TCP
Host Port: 0/TCP
...
  1. You can also check out the ReplicaSets. There should be another one taking responsibility of the Pods for Deployment:
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
simple-nginx-585f6cddcd 0 0 0 1h
simple-nginx-694f94f77d 0 0 0 1h
simple-nginx-b549cc75c 5 5 5 1h
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.25.41