Deploying web-server using Jenkins-Groovy-code and Kubernetes

Skabhi
6 min readJul 12, 2020

--

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.

About the DSL :

  • The DSL is scriptable via Groovy and so it is extremely flexible. The DSL also provides direct control over a job’s XML; this makes it possible to automate the creation of any job, regardless of what has been implemented in the DSL. A lot of helper methods are provided which makes it very easy to set up things such as build triggers, version control, shell scripts, and more.

Step 1 : Creating Dockerfile 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
COPY .kube /root/.kube


RUN mkdir -p /home/jenkins


EXPOSE 8080


CMD [ "java", "-jar", "/usr/lib/jenkins/jenkins.war" ]
  • To build this image use the command as shown below :
docker build -t skabhi003/jenkins:v1 .
  • After build is complete push it do docker hub so that anyone can use it .
docker push skabhi003/jenkins:v1

Step 2 : Use the above created image to run the jenkins on top of Kubernetes using the YAML file.

apiVersion: v1
kind: Service
metadata:
name: jenkins
labels:
app: jenkins
spec:
ports:
- port: 8080
selector:
app: jenkins
type: ClusterIP
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jenkins-claim
labels:
app: jenkins
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
---
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: skabhi003/jenkins:v10
name: jenkins
ports:
- containerPort: 8080
name: jenkins
volumeMounts:
- name: jenkins-storage
mountPath: /root/.jenkins/
volumes:
- name: jenkins-storage
persistentVolumeClaim:
claimName: jenkins-claim
  • For running this YAML file , use the following command…
kubectl apply -f file_name
  • First time when you run your jenkins , it will ask password, for getting password
kubectl logs pod_name
  • Enter the above coppied password in the jenkins page .

Step 3 : Setup the Jobs of Jenkins..

  • Before we move forward ,install these plugins:
  1. Job DSL

2. GitHub Plugin

3. Email Extension Plugin

4. Build Pipeline Plugin

5. Script Security Plugin

  • we need to create one Seed job , which create all the job automatically.
  • If you now build this Job they give you the error which says “The Script is not yet Approved”
  • To remove this, go to Jenkins configuration -> In Process Script Approval and approve the script.
  • Now again build and this time output is…
  • You can see that four jobs and one view are created here.

Step 4 :

  • Now if you see Job1 DSL Script

>>This creates Job1 like below:

  • And the Output of Job1 is:
  • Now if you see Job2 DSL Script

>>This creates Job2 like below:

  • And the Output of Job2 is:
  • Now if you see Job3 DSL Script

>>This creates Job3 like below:

  • Now if you see Job4 DSL Script

>>This creates Job4 like below:

  • If you now see the DSL Script of Build Pipeline View:
  • It will build the buildpipeline as

Github Link :

Thanks, hope you guys will like my article. if you have any suggestion or query will free to ask.

#Happylearning #keepsharing #devops

--

--

No responses yet