DevOps-Project-6
I have successfully done my DevOps-task-6 where I created a pipeline via Groovy code on top of Kubernetes.
My Task
Perform this task with the help of Jenkins coding(groovy) file ( called as Jenkins file approach ) and perform the with following phases:
1. Create container image that’s has Jenkins installed using dockerfile Or You can use the Jenkins Server on RHEL 8/7
2. When we launch this image, it should automatically starts Jenkins service in the container.
3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
4. Job2 ( Seed Job ) : Pull the Github repo automatically when some developers push repo to Github.
5. Further on jobs should be pipeline using written code using Groovy language by the developer
6. Job1 :
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 using PVC ( If server collects some data like logs, other user information )
7. Job3 : Test your app if it is working or not.
8. Job4 : if app is not working , then send email to developer with error messages and redeploy the application after code is being edited by the developer.
That's it
Basic Tools required
- Redhat-8 Linux Enterprise
- Kubernetes
- Jenkins
- Docker
Let us discuss whole task in very simple steps and simple words...........
Creating Dockerfile
Before using the Kubernetes, we need some certificates, keys and config file of kubernetes server wherever it is running but I used my local VM. Then we can create a Dockerfile in which Kubernetes and Jenkins already configured . Here is my Dockerfile where It has Jenkins start as well as Kubernetes client program is also started.
Now I have created my own image via above Dockerfile. So I directly exposed for outside world. But container always runs in isolated world so nobody can access is directly via internet. So we have to use concept called patting
follow syntax to expose container
- docker run -dit -p 8085:8080 --name <name_of_container> <image: tag>
Job-1
I created my Job1 as a seed Job. So it can create other Job without creating Jobs manually but we can't to this directly. We have to use Jenkins own language coding and name of that language is Groovy. So I used Groovy language to create my Seed Job.
Why Groovy ???
Because this is DevOps world and we never like to do work manually in DevOps world. we also know, we can't totally depends on human guys and we know human is very much slow to do work, too much lazy and also human error occurs so we need a automation to avoid to create Jobs by manually by operations guys.
Manage some work of operation guys by Developer guys is known as DevOps. This is also right definition of DevOps world that's why we are using Groovy language to manage some work of operation guys.
Creating Job-1
I am scheduling my Job-1 using pollscm it will go to github and check if any new code uploaded by developer guys so it will automatically download the code. Even I used Groovy code so we have to download some plugin before use groovy code.
Name of plugin
- Job DSL
I am copying all the code of PHP and HTML in the folder /mycode so management would be so easy.
Groovy code which i used as DSL script
job("JOB-2") { description("This job will launch the pods and deploy the code") triggers { upstream('JOB-1', 'SUCCESS') } steps { shell('cd /devops6\nkubectl create -f webpage.yml\nsleep 15\npodename=$(kubectl get pods -o=name | grep webdeploy | sed "s/^.\\{4\\}//")\nkubectl cp /devops6/mycode $podename:/var/www/html') } } job("JOB-3") { triggers { upstream('JOB-2', 'SUCCESS') } steps{ shell('status=$(curl -o /dev/null -s -w "%{http_code}" https://192.168.99.110:32000/mycode/satyam.html)\nif [[ $status == 200 ]]\nthen\nexit 0\nelse\nexit 1\nfi') } publishers { extendedEmail { recipientList('[email protected]') defaultSubject('Oops') defaultContent('Something broken') contentType('text/html') triggers { beforeBuild() stillUnstable { subject('Subject') content('Body') sendTo { developers() requester() culprits() } } } } } }
Output of Job-1
After running the Job-1, automatically Job-2, Job-3 and Job-4 created but we don't need to create seperate Job-4 for email notifications. I merged Job-4 inside Job-3.
Job-2
Job-2 is created by dynamically by using groovy lanaguage.
In Job2, I used YML code for launching Resources like PVC, Deployment and service. When developer pushed the code in above Job then Job-2 automatically deploy the entire code in folder /var/www/html. I supposed /mycode is fixed directory where developer keep on writing the code and pushed the code on github. So management would be so easy.
Output of Job-2
Job-3 & Job-4
Now we are gonna create our Job-3 on Jenkins in which Our task is "Tasting our app or code". For this I used concept of exit status code. If any site have exit status code is 200 then it means that it is working fine otherwise not.
Note
- I merged Job-4 inside Job-3 because I attached build Log of Job-3 as a email notification. When Job-3 would be run then Automatically Jenkins will send notification of Output Log of Job-3 so that after seeing the Log, developer guys can come to conclusion that Code is working fine or not.
How to configure email notification ?????
First we need to install two plugin inside Jenkins
- Email extension
- Email extension template
Then go to Jenkins configuration and set your details inside Email notification option.
--------------------------------------------------
Let's test Email Notification Option
---------------------------------------------------
Case-1: When our application is working fine
Output
Case-2: When our application is not working
Output
In such case, Jenkins will automatically send Job-3 Console output Log to developer guys. Here I received email notification via Jenkins
WOW ! It's working fine.
For better look and feel, we can also create End to End Build Pipeline.
I explained whole project as much as simple possible . If you have any query and any suggestion so please share in comment section below.
-----------------------------------------------------
Thank you for giving your time
------------------------------------------------------