Automating Deployment with CI/CD pipeline using Jenkins with using the power of Kubernetes

Sathvika Kolisetty
5 min readJul 11, 2020

--

Problem Statement

  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 start the 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 the 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 the app is not working, then send email to the developer with error messages and redeploy the application after code is being edited by the developer

Tools Required

  1. Windows as base OS
  2. Jenkins as a CI/CD tool.
  3. GitHub as SCM.
  4. Red Hat 8 system as a server running Docker as Containerize technology.
  5. Pre-configured Kubernetes single node cluster, Minikube
  6. kubectl as a client program for the Kubernetes cluster.

Start RHEL8 on your Virtual machine which is the host for docker. Create a folder which must have ca.crt, client.crt, client.key, and config file. Copy these files using WinSCP. These files are needed to Kubernetes in Docker containers.

Now we will create one Dockerfile that has Jenkins and kubectl installed for launching the pod on top of Kubernetes. In this Dockerfile, I have copied all the requirements used by kubectl to connect to the Kubernetes cluster. So when a container launch from this image, kubectl from the container can connect to Kubernetes cluster for launching the pods

Now built this docker image with this command

docker build -t jenkkube:v .

After the image has been built successfully then from this image we launch one container with the help of docker.

docker run -it --name task6os -p 1122:8080 jenkkube:v

After the container has been launched successfully, we can see that Jenkins has been started In this we have to keep the password that has been provided at the time of launching the container.

Now we will be creating the first job using Jenkins

JOB1:

This job will copy the code that is downloaded by Jenkins from GitHub into the workspace of job1 inside the Jenkins container.

JOB1 output

JOB2:

This job will detect code, either PHP or HTML, and run kubectl client container with the image created before and run Kubernetes pod with the required interpreter.

This job will run only when the first job builds successfully.

Here I am creating one YAML file for creating one PVC (Persistent volume claim) for the pod. when pod request for storage to PVC then PVC will get the storage from PV and provide to the pod and mount the folder of the pod. PV will create dynamically when PVC created.

Here I am creating YAML file for launching the pod on top of Kubernetes. This pod will run with the httpd image. When there is HTML code then pod with this image will launch. For making the data persistent attaching one PVC to a folder — /usr/local/apache2/htdocs

I have created both the YAML inside the /work folder in Jenkins container

First, Jenkins will check if the code is written in HTML then first creating one PVC for the pod through the YAML file after this launching one pod with httpd image through the YAML file. copy the code that Jenkins download from GitHub to the container running inside the pod. Expose this pod so that client can connect to the website running on this container inside the pod.

JOB2 Output

JOB3:

For email notification, we have to provide the following details for sending emails to the developer.

SMTP Server — smtp.gmail.com

Default user e-mail suffix- @gmail.com

Smtp port — 465

user name and password of the email from which you want to send the email to the developer

Now in job 3

if this job fails means the site is not running fine. In this case, it sends mail to developer

In post-build actions → E-mail Notification-

recipients- email address of the developer

FINAL OUTPUT

PIPELINE

Creating one build pipeline for visualizing the chaining of the jobs

--

--