Automated CI/CD pipeline for Kubernetes using Jenkins

Anshika Sharma
5 min readJul 10, 2020
Automated CI/CD pipeline

Hello Guys……

This blog is all about automating the Kubernetes — pods and containers using Jenkins.

What is Kubernetes ?

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available. In this we will use minikube to manage kubernetes or the containerized workloads.

What are Pods ?

Pods are the smallest deployable units of computing that can be created and managed in Kubernetes.

A Pod is a group of one or more containers (such as Docker containers), with shared storage/network, and a specification for how to run the containers. A Pod’s contents are always co-located and co-scheduled, and run in a shared context.

Now, Our plan is to integrate GitHub — Jenkins — Kubernetes in such a way that as soon as the developer pushes the code to the GitHub, jenkins will automatically download that complete code to its local workspace and then deploy this code to the kubernetes cluster in order to manage the workloads. Here i am going to create such a setup that as soon as jenkins observe that what type of code is provided by the developer i.e., it is a html code or a php code, on the basis of this it will create a kubernetes deployment with respective image of either html-webserver or for a php-webserver. Since it will run on kubernetes so we need to write any code for monitoring , kubernetes will itself handle as well as monitor everything.

Architectural view of our plan

Lets start creating a fully automated system to manage the workloads through kubernetes using Jenkins. We will create it step by step :

Step 1 : Setup kubectl on the RHEL8 in order to access minikube through it .

Downloading Kubectl
Moving the kubectl software to the bin folder

Now copy all the required files i.e., ca.crt, client.crt and client.key and also the config file from windows where minikube is configured through WinSCP tool. Create a .kube folder and copy this config file to this folder, because this is the default path for creating kubectl config view.

config file for kubectl

Now try to access the resources using kubectl commands like

kubectl get all

kubectl get pods

Step 2 : Create a Dockerfile from which jenkins can automatically installed when we build a docker container and then build the docker image .

Dockerfile for creating Docker image
Building docker container
List of docker images available in the system

Step 3 : Run a Docker container with this Docker image and it will automatically launch jenkins and also provide the authentication key to access the jenkins.

running a docker container
Authentication token to access Jenkins

Step 4 : Setup Jenkins for further automation process.

Initial Authentication page
Installing the useful plugins
Creating user configurations on Jenkins
Dashboard of Jenkins

Step 5 : Create the Automated system , here i will use two jobs i.e.,

Job 1 : With poll scm trigger, access the git repository and make a directory in the host and copy the github code to that directory .

Accessing GitHub Repository
Applying Poll SCM trigger
Making directory at host
Adding Post-build trigger for job 2

Job 2 : In job 2, firstly it will identify the type of code and on the basis of the type it will launch a kubernetes pod or container for it and also it will provide a persistent volume. Here i am going to create a deployment and mount a pvc for 10GiB. Here we will create the deployment through the yml file that is already provided by the developer.

Code for creating Kubernetes deployment with mounted pvc
Adding post-build Email-notification
  • > It will send the Email-notification if the build or job get failed.

Step 6 : Run the job 1 and within a minute check the deployment with the help of kubectl in windows ,

Successfully created Deployment

Go to the website with the IP and the port no. :

Final Website

Hence we have successfully created the Automated integration, with this as soon as the developer will create the some changes in the code , the changes will be updated in the website. Also with the help of kubernetes it will be controlled and the website won’t go down and can handle the traffic through it .

Hence, we have created a great integration of GitHub — Jenkins — Kubernetes.

For the reference of the codes , refer to the below link :

For any queries, issue or suggestion, please feel free to contact anytime. I will try to respond as soon as possible.

Thank You !!

--

--

Anshika Sharma

I am a tech enthusiast, researcher and work for integrations. I love to explore and learn about the new technologies and their right concepts from its core.