Kubernetes-jenkins-Docker Integration

Amangupta
5 min readAug 26, 2020

Here I am doing the integration of kubernetes with docker,jenkins & github for push the code as Source code management tool. I am performing this task given below:-

1. Create container image that’s has Jenkins installed and remote access of kubernetes using dockerfile.

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 : By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code ( eg. If code is of PHP, then Jenkins should start the pod that has PHP already installed ).

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.

firstly i build a Docker Image by using a Dockerfile in which jenkins are running and have a remote access of kubernetes that i have installed in base os “windows”. The Code of Docker file is Given below:-

FROM centos

RUN yum install sudo -y
RUN yum install wget -y
RUN yum install curl -y
RUN yum install git -y
RUN yum install net-tools -y
RUN sudo 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 jenkins -y
RUN yum install java-11-openjdk.x86_64 -y
RUN echo “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 sudo mv ./kubectl /usr/local/bin/kubectl
RUN kubectl version — client
RUN export PATH=/usr/local/bin/kubectl:$PATH

COPY config /root/.kube/
COPY ca.crt /root/
COPY client.crt /root/
COPY client.key /root/

CMD java -jar /usr/lib/jenkins/jenkins.war

EXPOSE 8080

Docker file

for configure the remote access of kubernetes we need to copy some files from base os in which kubernetes installed given below :-

  1. ca.crt
  2. client.crt
  3. client.key

for config file I build a yaml file:-

apiVersion: v1
kind: Config

clusters:
- cluster:
server:
https://192.168.99.100:8443
certificate-authority: /root/ca.crt
name: aman-cluster

contexts:
- context:
cluster: aman-cluster
user: aman

users:
- name: aman
user:
client-key: /root/client.key
client-certificate: /root/client.crt

Paste these files in workspace along with Dockerfile.

:- docker build -t amangupta2562/kube-jenkins:1 .

I run this command to build Docker images.

after building a image in which jenkins is running and have the remote access of kubernetes. then we launch the container by using this image:-

:- docker run -it -p 8085:8080 — name kubejen amangupta2562/kube-jenkins:1

start jenkins gui by connecting to 192.168.99.101:8085
cut the password from docker host and paste here to start jenkins

Jenkins runs!

install required plugins- github, build pipeline and their dependent plugins….

plugins start installing

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

github repo- https://github.com/amang1451/task3.git

I use scm tool as github and Build trigger as poll scm of every minute.

code of Execute shell:-

execute shell code

sudo rm -rf /website
sudo mkdir /website
if ls | grep .html
then
sudo cp -r *.html /website/
else
sudo cp -r *.php /website/
fi
sudo rm -rf /kubeCode
sudo mkdir /kubeCode
sudo cp -r *.yml /kubeCode/

job1 complete!

Job2 : By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code ( eg. If code is of PHP, then Jenkins should start the pod that has PHP already installed ).

job2 Execute shell

code :-

if ls /website/ | grep .html
then
if
kubectl get pvc | grep htmlwebpvc
then
echo “pvc already exist”
kubectl apply -f /kubeCode/htmlwebpvc.yml
else
kubectl create -f /kubeCode/htmlwebpvc.yml
sleep 5
fi
if kubectl get deployment | grep myhtmlweb
then
echo “deployment already exist”
kubectl apply -f /kubeCode/htmlwebdeployment.yml
else
kubectl create -f /kubeCode/htmlwebdeployment.yml
sleep 15
kubectl cp /website/* $(kubectl get pods -o=jsonpath=’{.items[0].metadata.name}’):/usr/local/apache2/htdocs/
fi
else
if
kubectl get pvc | grep phpwebpvc
then
echo “pvc already exist”
kubectl apply -f /kubeCode/phpwebpvc.yml
else
kubectl create -f /kubeCode/phpwebpvc.yml
sleep 5
fi
if kubectl get deployment | grep myphpweb
then
echo “deployment already exist”
kubectl apply -f /kubeCode/phpwebdeployment.yml
else
kubectl create -f /kubeCode/phpwebdeployment.yml
sleep 15
kubectl cp /website/* $(kubectl get pods -o=jsonpath=’{.items[0].metadata.name}’):/var/www/html/
fi
fi

job2 complete!

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

check the website…..

job3 complete!

Job4 : if app is not working , then send email to developer with error messages.

like this if job is unstable it will send mail

to configure:-

configuration for sending mail when job fails

job4 complete!

Overview through pipelines:-

when developer push code job automatically started

task3 complete!

--

--