Integrating Kubernetes, jenkins, github.

- 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 - Job1 : Pull the Github repo automatically when some developers push repo to Github.
3. 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 )
4. Job3 : Test your app if it is working or not.
5. 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
Lets start the process
So first we have to create one folder inside the vm . In this folder copy all the requirements like ca.crt, client.crt , client.key and config file .
These all are the requirements by kubectl to connect to kubernetes cluster that is running on some other vm. After this in this folder we gonna create one Dockerfile that has jenkins and kubectl installed for launching the pod on top of kubernetes

Here is my Dockrfile .
In this Dockerfile i copied all the requirements used by kubectl to connect to kubernetes cluster. So when a container launch from this image , kubectl from the container can connect o to kubernetes cluster for launching the pods.

Creating the docker image-
docker build -t “image-name:v1” .

It is downloading all the requirements that we have listed inside the Dockerfile.



After the image has been build successfully then from this image we launch one container with the help of docker.
docker run -it — name jenkinsos -p 3211:8080 anurag08/devops-task:v1
we are exposing this container for the connectivity from other operating system .

After container has been launched successfully, we can see that jenkins has been started in this container. In this we have to put the password that has been provided at time of launching the container.

Before creating any job in the jenkins we have to first install some plugins in jenkins so that jenkins can able to download the code from github.
Plugins like “Git” and “Github”.

This is the code that developer that has been pushed or uploaded to the github repository.
Github -URL

Creating first job for downloading the code from the github

Providing the URL of the jenkins repository where developer has uploaded the code.

This job will go to github in every one one min and check if developer have uploaded any code.

This job will create one folder inside the jenkins-container and copy the code that is download by jenkins from github inside this folder.

Creating one build pipeline for visualizing the chaining of the jobs in a beautiful format-


Creating second job of the task-
“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 )”

This job will run only when first job build successfully.

Here I am creating one yaml file for creating one PVC (Persistent volume claim) for the pod. when pod request for storage to PVC then PVC will get the storage from PV and provide to pod and mount the folder of pod. PV will create dynamically when PVC created.

Here i am creating yaml file for launching the pod on top of kubernetes. This pod will run with the httpd image. When there is HTML code then pod with this image will launch. For making the data persistent attaching one PVC to folder — /usr/local/apache2/htdocs
I have created the both the yaml inside the /code folder in jenkins-container

Here i am creating yaml file for launching the pod on top of kubernetes. This pod will run with the php image. When there is php code then pod with this image will launch. For making the data persistent attaching one PVC to folder — /usr/src/myapp

This is the code for job2.
First jenkins will check if the code is written in HTML then first creating one PVC for the pod through YAML file after this launching one pod with httpd image through yaml file. copy the code that jenkins download from github to the container running inside the pod. Expose this pod so that client can connect to the website running on this container inside the pod.
if the code is written in PHP then first creating one PVC for the pod through YAML file after this launching one pod with php image through yaml file. copy the code that jenkins download from github to the container running inside the pod. Expose this pod so that client can connect to the website running on this container inside the pod.

Here you can see jenkins detect that code is in HTML so launch the pod with HTTD image and also mount or attach one pvc to the conatiner and also expose this pod.

Now creating the third job of the task

This job will run only when second job will build successfully.

This job will check that the site on this pod is running fine or not. If the site is not running fine then this job will fail and sent the email-notification to developer

For sending the email we have to first install two plugins inside the jenkins
“Email Extension” and “Email Extension Template”

After installing these plugins go to manage jenkins — -> configure system

Here in email notification we have to provide following details for sending email to the developer.
SMTP Server — smtp.gmail.com
Default user e-mail suffix- @gmail.com
Smtp port — 465
user name and password of the email from which you want to send the email to the developer

Now in job 3
if this job fail means site is not running fine . In this case it send mail to developer
In post-build actions → E-mail Notification-
recipents- email address of the developer

Creating Job4 :

This job will run after every one minute

This job will redeploy the application after code is being edited by the developer (in case when application is not running good).
