Rolling Updates With Docker Jenkins and Kubernetes
Almost each and everything keeps improving and innovating in this world and this is necessary because if you don't, you will be kicked out of the market and this competition game but have you ever wondered how these companies updates their websites or webapps? If they do it directly then what about those who were doing some transactions at the time the company updated their website? Also, they need to have zero downtime as for the big MNCs if the system is down for even a few seconds, it is on the every news source the next day. An example for this is, WhatsApp Went down for 1 hour because of New Year wishes at midnight on New Year 2018 and it was on News everywhere.
So the question is, What's the solution? and How do they do it because We see new features being added and new updates everyday. I have tried to show a demo for this. The strategy used for these kind of updates where we do not disturb or interrupt the client already in our website is called Rolling Updates. We are going to use Kubernetes to deploy our Website and Manage it, if it goes down, Launch it again immediately and if any update is rolled out, do not interrupt the existing clients and let the enjoy the version of website they are on already and show the new updated website to the client who will visit after the update.
Step 1 :- Developer will write the website code and Dockerfile for the Docker image and push the code to Github. Create a webhook at Github to trigger the job of jenkins, So the setup will look something like this :-
Check the above shown Github Repository and my Dockerfile if you want.
Step 2 :- Setup jenkins for building the docker image and leave everything to it, it will handle all the things till deployment using Kubernetes.
We are going to Install a Plugin called docker to setup our jenkins master as docker client to build the image etc. and push it to docker hub so that our kubernetes can use it to deploy our website on the server. The setup after Installing the Plugin will look like :-
I have configured the docker service file at port 4243 and I have used the same in jenkins to configure is as docker client to run the commands at docker server
In the docker Agent template, we will use our own created docker image which will be launched to do our job of git clone and then deploy, for this We have built an image which has Git and Kubectl (client setup of kubernetes) installed and configured and we will mount the volume to the folder where we have our certificates as it can be critical to put those in the docker image and I have named the image as dheeth/gitkube, the dockerfile and kubectl config file of that image is shown below :-
Step 3 :- Configure a job in jenkins to clone the git repo and then build a docker image for us and push it to docker hub using the docker plugin, We will provide our credentials for the account to push the image and the label is used for the job to dynamically launch the docker container and run the job, Also, this job will be automatically triggered by the github webook if developer push any update, so it is just a one time setup. The setup will look like as shown in screenshots below :-
Output Console Logs of the job setup above are shown in the screenshots below :-
Step 4 :- Configure another job and set it to trigger after the previous job, so that it will automatically create deployment using kubernetes and if any update is pushed, it will automatically rollout the update, We have set the label in this job too, for it to use our dynamic node from docker and do the job, It will use the image built in previous job to deploy our code on the webserver from the image. The job configuration is shown in the screenshots below :-
After this job completes, the output console is shown in the screenshots below and our website is deployed on the server :-
For every job, jenkins launched dynamic nodes as docker containers shown in screenshot below :-
Now if any update is pushed by developer, jenkins job will be automatically triggered by the Github webhook, it will build the new image and push it to the docker hub and trigger the next job, after that, the next job will check if the deployment is already created in Kubernetes, then it will rollout the update, till the time the existing clients are there, it will keep showing them the older version of website and as soon as all the traffic is sent to the new update, K8s will keep terminating the old pods and launch the new pods with the new update and it won't affect anything. Website is rolled out with zero downtime and no interruptions. The output screenshots of console and updated website are shown below :-
I have used kubectl get pods -w because I wanted to take live screenshots of pods older pods being terminated and new pods being launched but my slow system didn't allow that. You can still see it in status what I wanted to show.
Website after the Update is shown below :-
Some of the screenshots from build pipline view of both the jobs are also shown below :-
I hope you liked the basic demo of the Rolling Updates strategy used by the MNCs and Startups. Also, Feedbacks and Suggestions are always welcome.