Deploying web-server using Jenkins and Kubernetes

kanav Gupta
4 min readAug 3, 2020

--

Task Overview:

  • Create a container image that’s has Jenkins installed using Dockerfile.
  • When we launch this image, it should automatically start the Jenkins service in the container.
  • 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 the repo to Github.
  • Job2 :

1. By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image pod 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 )

  • Job3: Test your app if it is working or not.

Project Description:

  1. Create Jenkins installed Dockerfile:

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 with persistent storage.

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”

2. Jobs in Jenkins:

  • Job1(Copy Github Code):

Whenever the developer pushes any code in Github, this job automatically detects and copy in host OS.

  • Job2(Deploy deployment for website):

If my job1 is successfully built, it triggers job2 and launches the container.

if kubectl get pods | grep httpd
then
echo “service is running”
else
kubectl create -f /code/http.yaml
sleep 20
POD=$(kubectl get pod -l app=httpd -o jsonpath =”{.items[0].metdata.name}”)
kubectl cp /code/*.html $POD:/var/www/html
fi

html.yaml is below

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: gaurav0417/http-server
name: httpd
ports:
- containerPort: 8080
name: httpd
volumeMounts:
- name: httpd-storage
mountPath: /var/www/html/
volumes:
- name: httpd-storage
persistentVolumeClaim:
claimName: httpd-claim
  • Job3(Testing that website is running or not)

After successfully build, Job2 will trigger Job3 and it checks the website that is working or not.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response