DevOps Assembly Line Task 3

DevOps Assembly Line Task 3

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.

No alt text provided for this image

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.

No alt text provided for this image


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

No alt text provided for this image


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.

No alt text provided for this image


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 .

No alt text provided for this image
No alt text provided for this image


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


No alt text provided for this image

Step3:

Creating Job1. We have to create a git repository and add required files.Now in Jenkins we have configure the git repository.

No alt text provided for this image

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.

No alt text provided for this image
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.

No alt text provided for this image

Now we have to deploy our file in kubernetes pod using respective image.

No alt text provided for this 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:

No alt text provided for this image


Step5:

Creating Job3. This runs after Job2. Here we have to test our site

No alt text provided for this image


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.

No alt text provided for this 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/

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 :

No alt text provided for this image


Step6:

Creating Job4. This Job is created for monitoring of the container and to launch another if the existing fails. This runs after Job3.

No alt text provided for this image
No alt text provided for this image
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:

No alt text provided for this image
No alt text provided for this image

With this we come to the end of this task . Open for any kind of suggestions regarding this task.

No alt text provided for this image


Github url:



GAURAV MALIK

SDE @Freecharge (A subsidiary of Axis Bank) | Ex- TCS | FinTech

4 年

Nice one bro..

Ajinkya Khandave

Associate @JP Morgan Chase and Co. | CIB Technology

4 年

Great work buddy

Aastha Saxena

Software Engineer | NodeJs | TypeScript | MySQL | MongoDB

4 年

Great work Tushar , keep going

要查看或添加评论,请登录

Tushar Saxena的更多文章

  • Dynamic Jenkins

    Dynamic Jenkins

    This article is a simple integration of GitHub, Jenkins, Kubernetes and Docker to automate the process of deploying a…

    2 条评论
  • Configuring CI/CD on Kubernetes with Jenkins and Groovy

    Configuring CI/CD on Kubernetes with Jenkins and Groovy

    This article is a simple integration of GitHub, Jenkins, Kubernetes and Docker to automate the process of deploying a…

    1 条评论
  • INTEGRATING PROMETHEUS AND GRAFANA WITH KUBERNETES

    INTEGRATING PROMETHEUS AND GRAFANA WITH KUBERNETES

    Task : Integrate Prometheus and Grafana and perform in following way: Deploy them as pods on top of Kubernetes by…

  • AWS automation using Terraform and Jenkins

    AWS automation using Terraform and Jenkins

    First lets know about the basic terminologies used in this project . What is Cloud Computing? Cloud computing is a term…

    4 条评论
  • ML + DEVOPS = MLOPS

    ML + DEVOPS = MLOPS

    So before starting this article my one question to everyone is why 90% of machine learning models not deployed ? So…

    4 条评论
  • Face Recognition Model

    Face Recognition Model

    Training a model from scratch requires a very large dataset and takes a lot of time to train. The solution to this is…

  • Docker + Git + Github + Jenkins = Automated Project#2

    Docker + Git + Github + Jenkins = Automated Project#2

    This article is based on a integration of great DevOps tools like git , github , jenkins , docker. Integrating all the…

    6 条评论
  • DevOps Automation using Docker , Jenkins and Git/Github

    DevOps Automation using Docker , Jenkins and Git/Github

    This project uses DevOps tools for continuous development, continuous integration and continuous deployment.We have to…

    2 条评论

社区洞察