Jenkins + Docker + Kubernetes

Mansisaini
4 min readJul 3, 2020

This is the depiction of integration of jenkins, docker, kubernetes and github.

Jenkins controlling docker and k8s

This is the upgradation of task 2 in which only docker has been used (reference to task 2) whereas in task 3 kubernetes on top of docker which is ultimately a container orchasteration tool has been used.

So here is the description of task 3:-

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

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

4. Job1 : Pull the Github repo automatically when some developers push 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 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 )

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

7. Job4 : if app is not working , then send email to developer with error messages and redeploy the application after code is being edited by the developer.

Step:-1 (creating image with this Dockerfile):-

docker build -t mansi1709/myk8simg:v2

We can build this image with above command and push this image into dockerhub with the below command:-

docker push mansi1709/myk8simg:v2

Step:-2

Now we can launch pod with this image with kubectl and also pvc will be created so that jenkins data will remain persistent and service resource will also created so that it will be publically available for outer world :-

Now, in browser hit 192.168.99.100:31000 and jenkins portal will be opened and we can login and now further we can create our jobs here:-

Job 1:-

We are using this html-pod.yml file for launching html pod:-

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: html-pvc
labels:
name: dev
spec:
accessModes:
— ReadWriteOnce
resources:
requests:
storage: 2Gi

— -

apiVersion: v1
kind: Pod
metadata:
name: html
labels:
env: testing
region: IN
spec:
containers:
— name: myweb-con
image: httpd
volumeMounts:
— name: web-vol1
mountPath: /usr/local/apache2/htdocs/
volumes:
— name: web-vol1
persistentVolumeClaim:
claimName: html-pvc

— -

apiVersion: v1
kind: Service
metadata:
name: my-service-html
labels:
app: myweb-service
spec:
selector:
env: testing
type: NodePort
ports:
— nodePort: 31001
port: 80
targetPort: 80

We are using this php-pod.yml file for launching php pod:-

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: php-pvc
labels:
name: dev
spec:
accessModes:
— ReadWriteOnce
resources:
requests:
storage: 2Gi

— -

apiVersion: v1
kind: Pod
metadata:
name: php
labels:
env: testing
region: IN
spec:
containers:
— name: myweb-con
image: httpd
volumeMounts:
— name: web-vol1
mountPath: /usr/local/apache2/htdocs/
volumes:
— name: web-vol1
persistentVolumeClaim:
claimName: php-pvc

— -

apiVersion: v1
kind: Service
metadata:
name: my-service-php
labels:
app: myweb-service
spec:
selector:
env: testing
type: NodePort
ports:
— nodePort: 31001
port: 80
targetPort: 80

Job 2:-

We are checking whether our website is working or not if not send the email to the developer:-

We can see that both the jobs have successully completed:-

Finally deployed website is:-

Github url:-

Thank You !

--

--