#DEVops Task3

Jainrishabh
5 min readAug 20, 2020

Task Description:

1. Create a 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 start the Jenkins service in the container.

3. Create a job chain of job1, job2, job3 using build pipeline plugin in Jenkins

4. Job1: Pull the Github repo automatically when some developers push the repo to Github.

5. 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 the testing team could perform the testing on the pod.

6. Job3: if the app is not working, then send an email to the developer with error messages.

IMPLEMENTATION..

Firstly, We’re going to create Dockerfile for creating the image which will contain pre-installed Jenkins and Kubernetes services as we will need them to create a job chain And then we need to copy config, Ca.crt, and client.crt files from your system .kube and minikube folder respectively into your workspace where you are going to build Dockerfile so that the docker file is build and kubernetes is configured too.

==> firstly start the minikube

Dockerfile used.

FROM centos
RUN yum install sudo -y
RUN sudo yum install wget -y
RUN yum install git -y


#Jenkins
RUN sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
RUN sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
RUN yum install java-11-openjdk.x86_64 -y
RUN yum install initscripts.x86_64 -y
RUN yum install jenkins -y
RUN echo -e "jenkins ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers


#Kubernetes
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 cp ./kubectl /usr/local/bin/kubectl
RUN cp ./kubectl /usr/bin/
RUN mkdir /root/.kube
COPY client.key /root/.kube
COPY client.crt /root/.kube
COPY ca.crt /root/.kube
COPY config /root/.kube


CMD /etc/rc.d/init.d/jenkins start && /bin/bash
  1. First copy all the certificates of minikube inside the directory where your Dockerfile is present.
  2. You get this file from where you installed your minikube…
  3. I installed minikube in windows and I found these files at C:\Users\jainr\.minikube && C:\Users\jainr\.minikube\profiles\minikube.
  4. After copy creates a “config” file.

now build the docker file

docker build -t <Image_name> .

Now we can run this command for running Docker Container:

docker run -dit -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker --name jenkins_task3 -v /:/Base_OS -p 1234:8080 dockerfile

In git do post commit

Now, Lets Begin with JOBS

JOB1

In this job as soon as the developer pushes the code Jenkins will automatically trigger and get the code from GitHub.

JOB2: If job1 is successful then job2 will be automatically triggered. In job2 I create a container on the basis of the content file. When our developer pushing the code he only needs to do only one extra thing just enter the filetype in content.txt file and we fetch the programming type from here and launch the desired container over the kubernetes

the Html.yml file for kubernetes is :

apiVersion: v1
kind: Service
metadata:
name: http-server
spec:
selector:
type: http
type: NodePort
ports:
- port: 80
name: http
nodePort: 30000
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: http-pvc
labels:
name: httppvc


spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-deploy
labels:
type: http
spec:
selector:
matchLabels:
type: http
strategy:
type: Recreate
template:
metadata:
labels:
type: http
spec:
containers:
- image: httpd
name: httpd
ports:
- containerPort: 80
name: httpd
volumeMounts:
- name: httpd-storage
mountPath: /usr/local/apache2/htdocs/
volumes:
- name: httpd-storage
persistentVolumeClaim:
claimName: http-pvc

for php.yml is

apiVersion: v1
kind: Service
metadata:
name: php-server
spec:
selector:
type: php
type: NodePort
ports:
- port: 80
name: php
nodePort: 31000
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: php-pvc
labels:
name: phppvc


spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-deploy
labels:
type: php
spec:
selector:
matchLabels:
type: php
strategy:
type: Recreate
template:
metadata:
labels:
type: php
spec:
containers:
- image: vimal13/apache-webserver-php
name: php
ports:
- containerPort: 80
name: php
volumeMounts:
- name: php-storage
mountPath: /var/www/html/
volumes:
- name: php-storage
persistentVolumeClaim:
claimName: php-pvc

JOB3: I Merge Job3 and Job4 in single Job3

When job2 launches the environment then in this job I will do testing and sending the email to developer if there is any problem in accessing the website.

Thanks To All the readers and my teammate priyanshu goyal for helping in every moment for building such a great project . Iwould also like to thanks Mr Vimal daga sir For making us such capable for building such an amazing projects.

--

--