Automated Deployment on Kubernetes using Jenkins (DevOps Task 3)

Automated Deployment on Kubernetes using Jenkins (DevOps Task 3)

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:

Start the minikube and obtain the IP

> minikube start

> minikube ip

Now copy the following three files in the VM where we created the Dockerfile

> C:\Users\User\.minikube\ca.crt

> C:\Users\User\.minikube\profiles\minikube\client.crt

> C:\Users\User\.minikube\profiles\minikube\client.key

Let's create a Jenkins Image having jenkins and kubectl installed in it. For this, we create a Dockerfile

FROM centos:latest


RUN yum install wget -y
RUN yum install net-tools -y
RUN wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins.io/redhat-stable/jenkins.repo
RUN rpm --import http://pkg.jenkins.io/redhat-stable/jenkins.io.key
RUN yum install java -y
RUN yum install jenkins -y
RUN yum install git -y
RUN yum install python36 -y


RUN sed 's/JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true"/JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dmail.smtp.starttls.enable=true -Dmail.smtp.ssl.protocols=TLSv1.2"/g' /etc/sysconfig/jenkins


RUN touch /etc/yum.repos.d/kubernetes.repo
RUN echo $'[kubernetes]\n\
name=Kubernetes\n\
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64\n\
enabled=1\n\
gpgcheck=1\n\
repo_gpgcheck=1\n\
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg' >> /etc/yum.repos.d/kubernetes.repo


RUN yum install -y kubectl

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

Now build the docker file using the command :

docker build -t jenkins:kube .

Start the container

docker run -it -v /root/kube-files:/root/.kube -P jenkin:kube


JOB 1: Pull the code from GitHub

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

Here above, i have used Poll SCM to pull the code from github every hour to simplify the things.

But the other way is to use the github webhooks for this purpose which makes the process very optimized. The steps to that can be found on the below article:

Now, we need to add the below code in the Build tab.

No alt text provided for this image

JOB 2: Deploy the code on the kubernetes pods

No alt text provided for this image

Add the following code in the build tab ( execute shell ) as shown above

export len1=$(ls -l /root/.jenkins/workspace/job1 | grep html | wc -l)
if [ $len1 -gt 0 ]
then
export len2=$(kubectl get deployments | grep webserver | wc -l)
if [ $len2 -gt 0 ]
then
kubectl delete deployment webserver
fi
export len3=$(kubectl get services | grep webserver | wc -l)
if [ $len3 -gt 0 ]
then
kubectl delete service webserver	
fi
kubectl create deployment webserver --image=httpd
kubectl scale deployment webserver --replicas=2
kubectl expose deployment webserver --port 80 --type NodePort
sleep 20
python3 /root/kubectl-cp.py
kubectl get svc webserver
fi

I have used the following kubectl.py file for copying the code in the pods.

#!/usr/bin/python3
  
import os

cmd = os.popen("kubectl get pods | grep webserver")
cmd_op = cmd.read()
x = cmd_op.split('\n')
del x[-1]
for y in x:
    z = y.split()
    print(z)
    os.system("kubectl cp /root/.jenkins/workspace/job1/{}:/usr/local/apache2/htdocs/".format(z[0]))


JOB 3: Monitor the app if its working or not

We can see that our pods are working fine or not using the command "kubectl get all"

You will see the below output:

No alt text provided for this image

Now we need to monitor the pods but it is not required as we used the deployment resource in kubernetes which will automatically restart the pods if any of them fails to start.

The further process sending an email remains the same as we did in the task-2 , provided below is the link to that article

Thanks for reading this article. Please ping me for any queries or suggestions, will be happy to have a chat.

To view or add a comment, sign in

More articles by Abhishek Khandelwal

Others also viewed

Explore content categories