Automating Continuous Deployment in DevOps using Jenkins
Cover Photo by Pooja Maurya

Automating Continuous Deployment in DevOps using Jenkins

What are we going to build :-

  1. Create container image that has Jenkins installed using dockerfile. When we launch this image, it should automatically start Jenkins service in the container.
  2. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins

Job1 : Pull the Github repo automatically when some developers push repo to Github.

Job2 : 

1. By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed )

2. Expose your pod so that testing team could perform the testing on the pod

3. Make the data to remain persistent ( If server collects some data like logs, other user information )

Job3 : Test your Website if it is working or not.

 Job4 : if Website is not working , then send email to developer with error messages and redeploy the application after code is edited by the developer.

Now, for the Jenkins to be installed and run in container with passwordless SSH configured in the docker image, I have explained it in complete detail in the article here - Automating CI/CD Pipeline in DevOps

After We login to Jenkins, We will install a build pipeline plugin, and then we'll create a job chain of 4 Jobs as shown in the screenshots below :-

No alt text provided for this image
No alt text provided for this image

Now we will discuss each and every Job in detail :-

Job 1 - git-pull :- This Job will be triggered using the github webhook as soon as the developer push the code to github, and this will download all the files from github as shown in the screenshots below :-

Github Webhook push to trigger the job

Download and Install Github plugin for this job and enter the repository url to download the files containing the code and required for deployment as shown in the screenshot :-

No alt text provided for this image

Here, we'll check if we have received any html or php files to deploy on our webserver. Then, we'll use ssh to copy the files in our workspace so that we can start the next process (ssh is already setup in jenkins image - link of article given above) :-

No alt text provided for this image

Console log of this job is shown below :-

No alt text provided for this image

Link to the Github repository - https://github.com/dheeth/kube-web

Now moving on to Job 2 - launch web :- For this, let's first check which files we have received in the last job from our github repository :-

No alt text provided for this image

So, now to deploy the Website based on the code we have received (php or html), we have a generalized Dockerfile in which we'll give the arguments during the build time using --build-arg so that the docker image can be built according to the files received. Here is the Dockerfile that we have received in our repo :-

No alt text provided for this image

We'll just have to change the image, files with extension and the directory to send the files according to php or html and then build the docker image. We'll build the image with the name dheeth/kube-web no matter whichever language image is built so that generalized image name can be used in K8s deployment file without having to change anything and to make the data persistent of logs etc. we'll use the pvc, so we have received a file for pvc too.

No alt text provided for this image
No alt text provided for this image

Before starting the deployment process, I just want to tell you that my K8s cluster is running on Azure because my laptop wasn't able to handle the load of K8s, so I just configured K8s cluster on azure and updated the config file in my local machine so that I can use it locally and run the commands whichever I want. In your case, you can do it on your own laptop locally, if you want tutorial for this too, then tell me in the comments section.

No alt text provided for this image

Now, when we have everything ready, let's deploy and expose our kubernetes deployment. We'll trigger this job after job1 git-pull and use the image php:7.2-apache and directory /var/www/html for php files or for php and html both because php server can show both of them and httpd image and /usr/local/apache2/htdocs for html files and build the image with the name dheeth/kube-web and then push it to docker hub so that we can use it with kubernetes. I have already logged in with docker login -u username -p password on my local machine so that it doesn't need credentials at the time of docker push as shown in screenshot below :-

No alt text provided for this image
No alt text provided for this image


Console logs of the Job given above are shown in the screenshots below :-

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Here is the Website deployed on K8s which had php code and deployed on php image :-

No alt text provided for this image

Job 3 test-web :- This job is same as the Test Website Job done in the previous article but the issue here is that we don't know what IP, we'll receive from Azure after the deployment is exposed so we can't fix a url for the curl to get the status code. Here, we'll use json output from kubernets to extract the url/ip of the website to get the status code :-

No alt text provided for this image
No alt text provided for this image

Console log of this job is shown in the screenshot below :-

No alt text provided for this image

Job 4 notify :- This job is also explained in detail in the article link shared above, we'll keep checking the website every minute, it will send the email to the developer if something goes wrong, for the setup configuration etc. refer to my article link given above :-

No alt text provided for this image
No alt text provided for this image

Console log of this job is also shown below :-

No alt text provided for this image

If something fails, we'll get the email as shown below :-

No alt text provided for this image

Here is the Link to my docker hub repository - https://hub.docker.com/u/dheeth

That's how we accomplished our task, do share your feedback and suggestions in the comments. You can also ask if you have any doubt, I'll be happy to help you.

Sahid Ali

Serving Notice Period / Software Engineer (DevOps + Cloud + Python) at Tech Mahindra

4 年

Good article

要查看或添加评论,请登录

Pawan Kumar的更多文章

  • DevOps Automation using Groovy in Jenkins Job DSL

    DevOps Automation using Groovy in Jenkins Job DSL

    Whenever we talk about DevOps, it comprises of 2 words Dev + Ops meaning Developers sharing some load of the Operations…

    3 条评论
  • Integrating Prometheus & Grafana Keeping their Data Persistent in K8s

    Integrating Prometheus & Grafana Keeping their Data Persistent in K8s

    You must have always wondered about keeping the data of your monitoring system having prometheus and grafana running as…

    2 条评论
  • Creating a WebServer Infrastructure on AWS using Terraform

    Creating a WebServer Infrastructure on AWS using Terraform

    Few years ago, World started using cloud for their infrastructure as it saves a lot of their cost for the hardwares and…

    1 条评论
  • Rolling Updates With Docker Jenkins and Kubernetes

    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…

  • Automating CI/CD Pipeline in DevOps

    Automating CI/CD Pipeline in DevOps

    In this Article, We are going to Automate the Deployment and Monitoring of a Web App through the Jenkins running in…

    5 条评论
  • Creating a Basic Pipeline in DevOps

    Creating a Basic Pipeline in DevOps

    This is a Project based on the first 8 days of DevOps Assembly Lines training under our mentor Vimal Daga Sir and In…

    2 条评论
  • A big thanks to IIEC Rise

    A big thanks to IIEC Rise

    Finally completed the Docker project and this is my joomla cms running on the docker containers. Few days ago, I wasn't…

社区洞察

其他会员也浏览了