Deploying web-server using Jenkins-Groovy-code and Kubernetes
Task Overview:
1. Create container image that’s has Jenkins installed using dockerfile.
2. When we launch this image, it should automatically start Jenkins service in the container.
3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
4. Seed Job: Pull the Github repo automatically when some developers push the 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. Job2: Test your app if it is working or not.
8. Job3: if app is not working, then send email to the developer with error messages and redeploy the application after code is being edited by the developer
- Docker file for jenkins
FROM centos:latest
RUN yum install wget -y
RUN wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
RUN rpm — import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
RUN yum install java-11-openjdk.x86_64 -y
RUN yum install jenkins -y
RUN yum install git -y
RUN yum install python3 net-tools -y
RUN echo -e “jenkins ALL=(ALL) NOPASSWD: ALL” >> /etc/sudoers
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin/kubectl
EXPOSE 8080
CMD [ “java”, “-jar”, “/usr/lib/jenkins/jenkins.war” ]
Save this file with name Dockerfile and build the image with command “docker bulid -t <image name>. “
This image push it to in docker hub because it is used in the kubernetes
- Running Jenkins server on top of Kubernetes
For this ,the best way is to create a yaml file
apiVersion: v1
kind: Service
metadata:
name: jenkins
labels:
app: jenkins
spec:
ports:
- port: 8080
selector:
app: jenkins
type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jenkins-claim
labels:
app: jenkins
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: jenkins
labels:
app: jenkins
spec:
selector:
matchLabels:
app: jenkins
strategy:
type: Recreate
template:
metadata:
labels:
app: jenkins
spec:
containers:
- image: guptakanav1999/kanav_jenkin
name: jenkins
ports:
- containerPort: 8080
name: jenkins
volumeMounts:
- name: jenkins-storage
mountPath: /root/.jenkins/
volumes:
- name: jenkins-storage
persistentVolumeClaim:
claimName: jenkins-claim
First we have to run kubernetes by using this command “minikube start”

Now we have to run yaml file using command “kubectl apply -f <file name>”


For running jenkin we need ip address



For the password, Run “kubectl logs pod_name”


We have to install github plugin for github project and for groovy language we have to install job dsl plugin and build pipeline plugin
2. Jobs in Jenkins:
- Seed Job(Pull the GitHub repo when developer pushes some code):
Whenever the developer pushes any code in Github, this job automatically detects and copy in host OS and also create other Jobs.





After run this job ,we have to go to Jenkins configuration -> In Process Script Approval and approve the script.

Click on approve and again build.
Two jobs and one view are created


Click on run and finally all jobs are running
- Job1(Deploy deployment for website):
It launches the deployment with PVC and services.I uses apache webserver.
job('job1-deploy-web-server') {
description(' job to deploy web-server')
steps {
shell('sh /code/code.sh')
}
}
Code.sh
if kubectl get pods | grep httpd
then
echo "service is running"
else
kubectl create -f /code/http.yaml
sleep 20
fi
POD=$(kubectl get pod -l app=httpd -o jsonpath="{.items[0].metadata.name}")
kubectl cp /code/*.html $POD:/var/www/html/

YAML code for launching the website
apiVersion: v1
kind: Service
metadata:
name: httpd
labels:
app: httpd
spec:
ports:
- nodePort: 80
port: 80
protocol: TCP
targetPort: 80
selector:
app: httpd
type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: httpd-claim
labels:
app: httpd
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: httpd
labels:
app: httpd
spec:
selector:
matchLabels:
app: httpd
strategy:
type: Recreate
template:
metadata:
labels:
app: httpd
spec:
containers:
- image: guptakanav1999/httpd
name: httpd
ports:
- containerPort: 8080
name: httpd
volumeMounts:
- name: httpd-storage
mountPath: /var/www/html/
volumes:
- name: httpd-storage
persistentVolumeClaim:
claimName: httpd-claim
- Job2(Testing that website is running or not) and Job3(Sent a mail to the developer):
After successfully build, Job1 will trigger Job2 and it checks the website that is working or not. If not working, it sent a notification to the developer.
Groovy code for this
job('job2-test_code') {
description('job to test the code and sent notification to developer')
triggers {
scm('@daily')
upstream {
upstreamProjects('job1-deploy-web-server')
threshold('SUCCESS')
}
}
steps {
shell('sh /code/test_code.sh')
}
}
test_code.sh
#!/bin/bash
status=$(curl -o /dev/null -s -w "{%http_code}" http://192.168.99.100:30570)
if [[ $status==200 ]]
then
python3 /code/mail.py
else
python3 /code/failed_mail.py
fi


For pipeline view, we use the code
buildPipelineView('Task 6') {
filterBuildQueue()
filterExecutors()
title('Project for deploy web-server')
displayedBuilds(5)
selectedJob('job1-deploy-web-server')
alwaysAllowManualTrigger()
showPipelineParameters()
refreshFrequency(60)
}