DEV Community

Cover image for CKA & CKAD Series (Part 4): Deployment & roll out
Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on • Updated on

CKA & CKAD Series (Part 4): Deployment & roll out

What is a rollout?
A rollout is the process of
gradually deploying or upgrading your application containers.

Image description

What happens in a deployment?

When you first create a deployment, it triggers a rollout. A new rollout creates a new Deployment revision. Let’s call it revision 1.

Image description

In the future when the application is upgraded – meaning when the container version is updated to a new one – a new rollout is triggered and a new deployment revision is created named Revision 2.
Image description

This helps us keep track of the
changes made to our deployment and enables us to rollback to a previous version of deployment if necessary.

You can see the status of your rollout by running the command: kubectl rollout status followed by the name of the deployment.
To see the revisions and history of rollout run the command kubectl rollout history followed by the deployment name and this will show you the revisions.
Image description

When you create a deployment, automatically a replcaset is created and automatically the replicaset creates pods.
Lets create a deployment:
Image description

here we have just changed the kind to Deployment. Other than this, most of the things are same as replicaset.
We have given the file name as deployment.yaml.

Lets create a deployment:
"kubectl create -f "

kubectl create -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Image description
Lets check the deployments

kubectl get deployments
Enter fullscreen mode Exit fullscreen mode

Image description

It shows we have 1 deployment and 3 out of 3 ready pods.
check the pods

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Image description

lets check more informations

kubectl describe deployment myapp-deployment
Enter fullscreen mode Exit fullscreen mode

myapp-deployment is the deployment name.

Image description

Now use this command

kubectl get all
Enter fullscreen mode Exit fullscreen mode

Other than the service, all of the things are related to the deployment
Image description

Deployment strategy

  1. Way 1 (Recreate strategy): There are two types of deployment strategies. Say for example you have 5 replicas of your web application instance deployed. One way to upgrade these to a newer version is to destroy all of these and then create newer versions of application instances. Image description

Meaning first, destroy the 5 running instances and then deploy 5 new instances of the new application version. The problem with this as you can imagine,
is that during the period after the older versions are down and before any newer version is up, the application is down and inaccessible to users.
Image description

This strategy is known as the Recreate strategy, and thankfully this is NOT the default deployment strategy

Image description

Image description

  1. Way 2 (Rolling update by default): The second strategy is were we do not destroy all of them at once. Instead we take down the older version and bring up a newer version one by one. This way the application never goes down and the upgrade is seamless. Image description Image description Image description

Note: if you do not specify a strategy while creating the deployment, it will assume it to be Rolling Update. In other words, RollingUpdate is the default Deployment Strategy.

Image description

Commands:
Image description

Lets do it practically:

Lets delete our deployment thus no pods exist and we will modify our deployment and then use rollout and others here.

kubectl delete deploy myapp-deployment
Enter fullscreen mode Exit fullscreen mode

Image description

Lets update our replicas to 6

Image description
Image description
Lets create the deployment

kubectl create -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Image description
You can now check the status of deployment by watching the 6replcas (pods) getting created live :

kubectl get pods -w
Enter fullscreen mode Exit fullscreen mode

Image description

Lets use the rollout status command to see status:

kubectl  rollout status deployment.apps/myapp-deployment 
Enter fullscreen mode Exit fullscreen mode

Image description

Did you realize what happened?
I guess no.Let's delete the deployment and create it fast . Then check the rollout status.
Image description

Lets check the history of the deployment

kubectl  rollout history deployment.apps/myapp-deployment 
Enter fullscreen mode Exit fullscreen mode

Image description
You can see that, we have just 1 revision. You can seethe Cause change None as we did not set anything to record while deployment.

Lets delete the deployment and set this time.
Image description

Lets create the deployment with --record command to record the Cause of change this time.

kubectl create -f deployment.yaml --record
Enter fullscreen mode Exit fullscreen mode

Now, check the rollout status & history too
Image description
You can see the cause of change.

As we have used the --record command, it has recorded the command "kubectl create --filename=deployment.yaml --record=true"
You can check the deployment information properly:

kubectl describe deployment myapp-deployment
Enter fullscreen mode Exit fullscreen mode

Image description

Lets change the image of the replicas (pods) :

kubectl edit deployment myapp-deployment --record
Enter fullscreen mode Exit fullscreen mode

we are using --record to record whatever we are doing...
Image description

first press "i" to get into insert mode and then change the image to nginx:1.22 which is a stable version and older one.
Image description
Image description

Once we have updated the image, you can press "Esc" then ":wq!" to exit

Now the old pods (Which had nginx image) are terminated and new pods are created using image nginx:1.22.
Image description

If you type this command within a very short time,

kubectl  rollout status deployment.apps/myapp-deployment 
Enter fullscreen mode Exit fullscreen mode

you may see something like this;

Image description

Now check the pods or replicas;
Image description

They are completely new made of ngnix:1.22 image

If you check the description of the deployment now, you can see something like this

kubectl describe deployment myapp-deployment

Enter fullscreen mode Exit fullscreen mode

Image description
You can see the image has been changed to nginx:1.22 and events . Also it is revision 2 now.
Image description

Lets change the image to nginx:1.22-perl again. You may use
Image description

kubectl edit deployment myapp-deployment --record
Enter fullscreen mode Exit fullscreen mode

or,

"kubectl set image deployment "

SO, in this case

kubectl set image deployment myapp-deployment nginx=nginx:1.22-perl
Enter fullscreen mode Exit fullscreen mode

Remember for this yaml file , we did set container name as nginx and image was set to nginx:1.22 which we are changing to nginx:1.22-perl

Image description

You can now check the rollout status
Image description
You can see the old replicas to be terminated.

Now, you may check the history of rollouts:

kubectl  rollout history deployment.apps/myapp-deployment 
Enter fullscreen mode Exit fullscreen mode

There are totally 3 revisions. for the 1st time, when we created the deployment, 1 rollout happened and 1 version was created .

Then we edited the image t nginx:1.22 and thus another rollout happened and new revision appeared. Again, when we changed the image to nginx:1.22-perl, another rollout happened and another revision was created.

Now, assume that your boss told you that the revision 3 was a bad decision. You should have been in revision 2, then what should you do? You can undo changes . Use this command:

kubectl rollout undo deployment.apps/myapp-deployment
Enter fullscreen mode Exit fullscreen mode

This will undo revision 3 and take you back . This will create revision 4.
Thus new pods will be created which will have image nginx:1.22

Now check out he description of the deployment and you can see the image nginx:1.22
Image description

Wait, a second. We now have 3 revisions (revision 1,3,4). Where is revision 2,Right?

Image description
Because the revision 2 actually the revision 4.

Hey! Did you notice that we were just applying RollingUpdate strategy by now?

kubectl edit deployment myapp-deployment --record
Enter fullscreen mode Exit fullscreen mode

You will find a section under spec defining strategy
Image description

If you need to change it to Recreate or any other, you can change it to "Recreate"
For example, for the Recreate strategy, you need to edit it to:

 strategy:
    type: Recreate

Enter fullscreen mode Exit fullscreen mode

This will do.....

Done! Congratulations boss!

Top comments (1)

Collapse
 
jhawithu profile image
Alok Kumar

Very nice collection of information on CKA preparation. I also created a dedicated course on Udemy for CKA exam and more than 1k+ people get benefited

My CKA Course