Automating Continuous Deployment in DevOps using Jenkins
What are we going to build :-
- Create container image that has Jenkins installed using dockerfile. When we launch this image, it should automatically start Jenkins service in the container.
- 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 :-
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 :-
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 :-
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) :-
Console log of this job is shown below :-
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 :-
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 :-
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.
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.
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 :-
Console logs of the Job given above are shown in the screenshots below :-
Here is the Website deployed on K8s which had php code and deployed on php 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 :-
Console log of this job is shown in the screenshot below :-
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 :-
Console log of this job is also shown below :-
If something fails, we'll get the email as shown below :-
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.
Serving Notice Period / Software Engineer (DevOps + Cloud + Python) at Tech Mahindra
4 年Good article