Integration of jenkins , kubernetes and github

Jenkins: jenkins is the devops tool which helps to automation of projects , by which projects are tracked and done automatically. jenkins can be used as integration tool also . By jenkins many devops tool are integrated and done project in loop automaticaly without using more human resources .
Kubernetes: kubernetes is devops tool which use to launch container amd operate it . When any container is launched and failed then kubernetes track the availibility of and launch again without delay . kubernetes work as load balancer also .
Github: Github provides cloud service which has more interesting service , user can create own private/public repository where store data . Github has master branch, which is default branch for developer . Developer can create multiple branch .
Project description
. Perform task on top of kubernetes where used kubernetes services like Pods , Deployment , PVC , Service .
Deployment: Deployment is one of the main resources of kubernetes which call replicaset and launching of pods , deployment manage the pod, when any pod is down then deployment again launch pod and manage pods according to set Replicaset resource .
Pods: Pods is one of the main resources of kubernetes , which track the container launched ,and manage all the service of container.
PVC: PVC is one of the main resources of kubernetes , which provide the service of persistent volume , this service manage the storage of pods .
Service: Service is the main resource of kubernetes which provides the service of load balancer and pods can be exposed using this resource
. Create container image that’s has jenkins installed using Dockerfile
First of all created a directory “devops_task3” using cmd
mkdir devops_task3
After creating directory go to that directory and create a Dockerfile for image creating that has jenkins installed and have power to operate kubernetes resources
cd ḍevops_task3

Dockerfile:

After creating Dockerfile run below cmd to create image
docker build -t jenkins_image:v2 .

After creating image tagged a name with image name and upload on github
docker tag jenkins_image:v2 firsttalk26/jenkins_image:v2
docker push firsttalk26/jenkins_image:v2

After uploading image on github , i have created deployment configuration file for deployment of jenkins on kubernetes .
jenkins_deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins-deployment
labels:
app: jenkins
spec:
replicas: 1
selector:
matchLabels:
app: jenkins
strategy:
type: Recreate
template:
metadata:
labels:
app: jenkins
spec:
containers:
- image: firsttalk26/jenkins_image:v2
name: jenkins
ports:
- containerPort: 8080
name: jenkins
volumeMounts:
- name: jenkins-pvc
mountPath:
/root/.jenkins
volumes:
- name: jenkins-pvc
persistentVolumeClaim:
claimName: jenkins-pvc
jenkins_pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jenkins-pvc
labels:
name: jenkinspvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
jenkins_service.yml
apiVersion: v1
kind: Service
metadata:
name: jenkins-service
labels:
app: jenkinsService
spec:
ports:
- port: 8080
nodePort: 30006
selector:
type: jenkins
type: NodePort
For launch jenkins deployment , service and pvc run below cmd
kubectl create -f jenkins_pvc.yml
kubectl create -f jenkins_deployment.yml
kubectl create -f jenkins_service.yml
After launch pod of jenkins open jenkins dashboard using url and created job for complete project .And created three job
Job in jenkins:
Job1: Pull the github repo automatically when some developers push repo on github , and copy in the folder task3_data
Job2: By looking on the code or programme file , jenkins should automatically start the respective language interpreter installed image container to deploy code on top of kubernetes .
Job3: Test app , it is working or not . If app is not working then sent email to developer with error messages
Job1




Execute shell:
if ls /root/.jenkins/workspace/ | grep task3_data
then
cp -rf * /root/.jenkins/workspace/task3_data/
echo "folder created"
else
sudo mkdir /root/.jenkins/workspace/task3_data
cp -rf * /root/.jenkins/workspace/task3_data/
fi
Job2:


Execute shell:
if ls /root/.jenkins/workspace/task3_data/ | grep .html
then
if kubectl get deployment | grep webserver-deployment
then
echo "already webserver-deployment created"
else
kubectl create -f /root/.jenkins/workspace/task3/webserver_deployment.yml
fi
else
echo "it's not html code"
if ls /tas3/ | grep .phpcode
then
echo " it is php code"
if kubectl get deployment | grep webapp-deployment
then
echo " webapp-deployment is already created"
else
kubectl create -f /root/.jenkins/workspace/task3/php.yml
fi
else
echo "recheck code"
fifi
webserver configuration file :
apiVersion: v1
kind: Service
metadata:
name: webserver
labels:
app: webserver
spec:
ports:
- port: 80
nodePort: 30001
selector:
type: html
type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-html
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-deploy
labels:
app: webserver
type: html
spec:
selector:
matchLabels:
type: html
strategy:
type: Recreate
template:
metadata:
labels:
app: webserver
type: html
spec:
containers:
- image: httpd
name: webserver
ports:
- containerPort: 80
name: webserver
volumeMounts:
- name: webserver-pv
mountPath: /usr/local/apache2/htdocs/
volumes:
- name: webserver-pv
persistentVolumeClaim:
claimName: pvc-html
Job3:


Execute shell:
podname=$(kubectl get pods -l type=html --output=jsonpath={.items..metadata.name})kubectl cp /root/.jenkins/workspace/task3_data/ $podname:/usr/local/apache2/htdocs/status=$(curl -o /dev/null -s -w "%{http_code}" http://192.168.99.109:30001)
if [ $status == 200 ]
then
exit 0
else
exit 1
fi

