Integration of GitHub, Jenkins, Kubernetes and Docker to automate Deployment

Divya Kurothe
4 min readSep 1, 2020

--

Problem Statement:

Perform second task on top of Kubernetes where we use Kubernetes resources like Pods, ReplicaSet, Deployment, PVC and Service.

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 :

a. 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 )

b. Expose your pod so that testing team could perform the testing on the pod

c. 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.

So let’s start with the solution part:

Docker can build images automatically by reading the instructions from a Dockerfile. So first of all we will create an image using Dockerfile. This contains commands to install and configure software and also copy certificates from windows to configure minikube.

FROM centos

RUN yum install wget -y
RUN yum install git -y
RUN yum install sudo -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 net-tools -y
RUN yum install python36 -y
RUN yum install initscripts -y
RUN echo -e "jenkins ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

USER jenkins
ENV USER jenkins

RUN sudo 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 sudo chmod +x ./kubectl
RUN sudo mv ./kubectl /usr/local/bin/kubectl
RUN sudo mkdir /root/.kube

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

CMD ["java" ,"-jar", "/usr/lib/jenkins/jenkins.war"]
EXPOSE 8080

Next we need to write a config file to configure kubectl

The docker build command builds an image from a Dockerfile and a context. And then run the docker container made using this image using docker run command.

docker build -t task3_devops .
docker run -dit -p 8085:8080 --name kube_jenkins task3_devops

We will then create a jenkins job which will pull the Github repo automatically when some developers push repo to Github and create the new image dynamically for the application and copy the application code into that corresponding docker image. We will use ‘trigger builds remotely’ for this.

We will then deploy the pod using kubernetes and if have the container already running then display the massage accordingly.

Now we will test the app if it successfully working or not. So we will create a job in jenkins which will notify the successful working of the app if the status code is 200.

Else if it gets failed then the developer should receive a mail.

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
host_address = "******@gmail.com"
host_pass = "****"
guest_address = "***@gmail.com"
subject = "Pod Status "
content = '''Hello developer, the pod testing failed kindly review.'''
message = MIMEMultipart()
message['From'] = host_address
message['To'] = guest_address
message['Subject'] = subject
message.attach(MIMEText(content, 'plain'))
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
session.login(host_address, host_pass)
text = message.as_string()
session.sendmail(host_address, guest_address , text)
session.quit()
print('Successfully sent')

Now we will create a monitoring job in jenkins whose upstream job is launching of the container if any of them fails and for this we will use poll SCM.

That’s all geeks. With this we come to the end of this task. Any suggestions and feedback is highly appreciated. Thank you.

--

--

No responses yet