This article is a simple integration of GitHub, Jenkins, Kubernetes and Docker to automate the process of deploying a website by creating custom Docker images using Dockerfile.
Before starting lets get familiar with some terms
Jenkins:
Jenkins is a free and open source automation server. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery. It is a server-based system that runs in servlet containers such as Apache Tomcat.
Github:
GitHub is a Git repository hosting service, but it adds many of its own features. While Git is a command line tool, GitHub provides a Web-based graphical interface. It also provides access control and several collaboration features, such as a wikis and basic task management tools for every project.
Kubernetes:
Kubernetes (commonly stylized as k8s) is an open-source container-orchestration system for automating application deployment, scaling, and management. ... It aims to provide a "platform for automating deployment, scaling, and operations of application containers across clusters of hosts".
Docker:
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.
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 :
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.
Solution:
Step1:
First we have to create Dockerfile
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
The docker file includes some software installation and to run some commands.
We have copy some certificates from windows like ca.crt, client.crt, client.key from windows which is to connect to minikube from the docker container.
We also have to create a file called config and copy that to folder /root/.kube/
apiVersion: v1
kind: Config
clusters:
- cluster:
server: https://192.168.99.101:8443
certificate-authority: /root/.kube/ca.crt
name: lwcluster
contexts:
- context:
cluster: lwcluster
user: Tushar
users:
- name: Tushar
user:
client-key: /root/.kube/client.key
client-certificate: /root/.kube/client.crt
Step2:
Now we have to build this dockerfile to build an image from which we can launch a container
docker build -t task3_devops .
Now run the docker container and expose it
docker run -dit -p 8085:8080 --name kube_jenkins task3_devops
After launching the container you can access jenkins dashboard by using the web address.
VM IP:Port number
Step3:
Creating Job1. We have to create a git repository and add required files.Now in Jenkins we have configure the git repository.
In Repository URL give your git repository url. For build trigger we can use "Trigger builds remotely (e.g., from scripts)"
Now we have to copy the files to container from github.
sudo rm -rf /task3_devops
sudo mkdir /task3_devops
sudo cp -rvf *.yml /task3_devops
sudo cp -rvf *.
sudo cp -rvf *.php /task3_devops
Step4:
Creating Job2. This job has to run after Job1.
Now we have to deploy our file in kubernetes pod using respective image.
if ls /task3_devops/ | grep '[a-z].php'
then
echo "It is a php code"
if sudo kubectl get deploy | grep webserver
then
echo "php container is running"
else
sudo kubectl create -f /task3_devops/deploy.yml
fi
else
echo "No php code found"
fi
sudo kubectl cp /task3_devops/a.php webserver-577c6f958d-5w6g6:/var/www/html/
Link to deployment file:
After Job2 we can see the output:
Step5:
Creating Job3. This runs after Job2. Here we have to test our site
If the status code is 200 then it means that the site was opened successfully else we will send a mail to developer regarding it.
if ls /task3_devops/ | grep '[a-z].php'
then
echo "It is a php code"
if sudo kubectl get deploy | grep webserver
then
echo "php container is running"
else
sudo kubectl create -f /task3_devops/deploy.yml
fi
else
echo "No php code found"
fi
sudo kubectl cp /task3_devops/a.php webserver-577c6f958d-5w6g6:/var/www/html/
Python Code for mailing developer:
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 = "Regarding pod status "
content = '''Hello, Developer this is an email regarding your last job. The pod testing failed kindly review.
THANK YOU ...'''
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 your mail')
If status code is other than 200 then :
Step6:
Creating Job4. This Job is created for monitoring of the container and to launch another if the existing fails. This runs after Job3.
if sudo kubectl get deployment | grep webserver
then
exit 0
else
sudo kubectl create -f /task3_devops/deploy.yml
sleep 10
fi
if sudo kubectl get pods | grep running
then
exit 0
else
echo "Pod is not running"
fi
Now overview of all the jobs:
With this we come to the end of this task . Open for any kind of suggestions regarding this task.
Github url:
SDE @Freecharge (A subsidiary of Axis Bank) | Ex- TCS | FinTech
4 年Nice one bro..
Associate @JP Morgan Chase and Co. | CIB Technology
4 年Great work buddy
Software Engineer | NodeJs | TypeScript | MySQL | MongoDB
4 年Great work Tushar , keep going