Achieving CI/CD with Kubernetes

achievingcicd featured

Ramit Surana - Author

Hola amigos !!(In English – Hello Friends !!) Hope you are having a jolly good day ! Continuous Integration/Delivery is best said in terms of Martin Fowler, according to him it can be defined as, “Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily – leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly.”

In this rather interesting piece of article we are going to discuss and explore two amazing and rather interesting pieces technology.One i.e. Jenkins,a popular Continuous Integration/Deployment tool and Second i.e. Kubernetes,a popular orchestration engine for containers.As an added bonus we are also going to discover fabric8 an awesome tool for microservices platform.So let’s get started,

WARNING Your machine may hang several times while performing the below steps.Choose a pc with high configuration.

Methodology

There are many methodologies using which we can achieve CI/CD for the on our machine.Currently,in this article we are focused on

Overview of Architecture

Before Starting our work,first let’s take a moment and analyze the workflow required to start using kubernetes containers with jenkins.Kubernetes is an amazing orchestration engine for containers developed an amazing open source community.The fact that kubernetes was started by Google,gives Kubernetes an amazing advantage to use multiple open source container projects.By default,docker is the one that is supported and used most with kubernetes. So the workflow,with docker containers looks like,

achieving cicd 1

Similarly while using rkt containers a.k.a rktnetes. Here’s the architecture:

achieving cicd 2

Currently there is no plugin supported for rkt containers by jenkins. But I assume that the workflow will remain the same after its due implementation.

Kubernetes-Jenkins Plugin

Setting up Kubernetes on Host Machine

Setting up kubernetes on your host machine is an easy task.In case you wish to try out on your local machine I would recommend you to try out minikube. In case you wish to the best of kubernetes,please refer to .Here is a quick follow up guide to get you started with setting up minikube on your local machine :

# Ensure Installation of kubectl first
# Visit for http://kubernetes.io/docs/getting-started-guides/binary_release/
# For downloading any prerequisites
# Visit https://github.com/kubernetes/minikube/blob/master/DRIVERS.md
# Download & install Minikube
curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.7.1/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

An amazing work in the direction of using jenkins and kubernetes has been done by carlossg.He has built an awesome kubernetes plugin for jenkins. Using this plugin you can easily start using jenkins with kubernetes directly.Also to provide users with more easy options to configure.He has built a jenkins image which by default contains the kubernetes plugin.This image is available at docker hub.In the next steps we are going to fetch this image from docker hub and create a volume /var/jenkins_home for storing all your jenkins data.

One Problem

Although we are doing everything as we planned to do.We will still run into a problem,you will notice that whenever you are about to restart your jenkins container after closing it down.All your data is lost.Whatever you did like creating jobs,installing plugins etc. will be lost.This is one of the common problems with containers.Let’s discuss it in a bit depth:

A word about Data Containers

Data is a tricky concept when it comes to containers.The containers are not very good example of keeping data secure and available all the time.There have been many incidents in the past where the containers have been seen to leak data.There are many ways to deal with such a problem.One is to use docker volumes.Due to some reasons,I did not found it that useful when used in terms of persistent storage.One of the ways I found to deal with persistent storage is to create another container,called as Data Container, and use it as a source of storing data instead of depending only one image.Here’s a simple figure on how we plan to use the Data Container to ensure reliability of our data,

achieving cicd 3

Here are the steps below to start using the jenkins kubernetes image,

//Pulling down the jenkins-kubernetes image
$ docker pull csanchez/jenkins-kubernetes
//Created a container for containing jenkins data with the image name csanchez/jenkins-kubernetes
$ docker create --name jenkins-k8s csanchez/jenkins-kubernetes

The above command will create and save data in a container called jenkins-k8s,which will be used whenever we wish to further use the jenkins containers with a persistent volume.

//Running jenkins using another container containing data
$ docker run --volumes-from jenkins-k8s -p 8080:8080 -p 50000:50000 -v /var/jenkins_home csanchez/jenkins-kubernetes

Open http://localhost:8080 in your browser,you should see the below screen:

achieving cicd 4

Configuring settings for Kubernetes over Jenkins

Now the jenkins is pre-configured with kubernetes plugin. So let’s jump to the next step.Using the jenkins GUI go to Manage Jenkins -> Configure System -> Cloud -> Add a new Cloud –> Kubernetes The screen looks like below after you have followed the above steps:

achieving cicd 5

Now fill up your configuration settings according to the the pic below:

achieving cicd 6

In case you wish to use jenkins slave you can use the jnlp-slave image on docker hub. This is a simple image that is used to set up slave node template for you.You configure a slave pod by creating a template like in the figure below,

achieving cicd 7

In order to use jenkins slave on the run. While creating a new job on jenkins, do this under configure settings of your job

achieving cicd 8

Now just put the name of the label you put in kubernetes pod template under the restrict section.Now save and apply the settings for your new job.When build this job you should see the slave node now running after you have build up the job. That’s all folks !! You are ready to go,you can now add more of your plugins as per your needs.

Fabric8

It is an open source microservices platform based on Docker, Kubernetes and Jenkins.It is built by the Red Hat guys.The purpose of the project is to make it easy to create microservices, build, test and deploy them via Continuous Delivery pipelines then run and manage them with Continuous Improvement and ChatOps.

Fabric8 installs and configures the following things for you automatically

Here’s a brief pic of the architecture of Fabric8

achieving cicd 9

In order to get started,first you need to install the command line tool for fabric8 i.e. gofabric8.You can do that by downloading gofabric8 from https://github.com/fabric8io/gofabric8/releases.Unzipping it and use

$ sudo cp /usr/local/bin/ gofabric8

You can check its installation by running `$ gofabric8’ on your terminal.Now run the following commands below,

$ gofabric8 deploy -y

Your terminal screen should look like this

achieving cicd 10

Generating Secrets

$ gofabric8 secrets -y

Your terminal screen should look like this

achieving cicd 11

Check for the status of pods using kubectl

$ kubectl get pods
achieving cicd 12
It will take a while to get all the container images to pull down and getting started.So you can go out and drink coffee :) You can use kubectl describe pods to check if something fails.

achievingcicd 13

You can checkout the status of your pods via a opening the kubernetes dashboard in a browser:

http://192.168.99.100:30000

achieving cicd 14

Similarly you can open the fabric8 hawtio browser interface

achieving cicd 15

From my analysis here’s a depiction of what happened when you ran the above commands.Below is a simple workflow diagram for the same.

achieving cicd 16

Achieving CI/CD

Easier said than done,building jenkins from source and integrating kubernetes is one part of the story.But achieving Continous Delivery with your setup is another very different and complex part of the story.Here are some of my tips on using certain plugins that could help you in achieving Continous Delivery with jenkins:

This is a core plugin built by the jenkins community.This plugin ensures that you can easily integrate any orchestration engine with your enviorment with very less complexity.Currently,I believe this was started as different communities had started building different plugins for various engines and they had to depend on majorly the Jenkins UI part to do so.Using this plugin users now can now directly implement there project’s entire build/test/deploy pipeline in a Jenkinsfile and store that alongside their code, treating their pipeline as another piece of code to be checked into source control

These days most companies are using github as SCM tool.In this case I would recommend you to use this plugin.This plugin helps you to push the code from github and analyze,test it over Jenkins.For authentication purposes I would recommend you to look Github Oauth Plugin

For Docker guys this is one of the most suitable plugin that helps you do almost everything with docker.This plugin also helps you to use docker containers as slaves. There are several other docker plugins that according to time and your usage you can switch over with.

The AWS guys have introduced an awesome service called as pipeline.This particular service helps you to attain continous delivery with your aws setup.Currently this plugin is under heavy development and might not be suitable for production enviorments.Also checkout AWS CodeCommit.

For the openstack users,this plugin is suitable to configure your openstack settings with your openstack enviorment.

The deployment manager is service started by the Google Cloud platform.Using Deployment Manager,you can create flexible and declarative templates that can deploy a variety of Cloud Platform services, such as Google Cloud Storage, Google Compute Engine, Google Cloud SQL, and leave it to Deployment Manager to manage the Cloud Platform resources defined in your templates as deployments. This is a very new plugin.But I think it is worth a try if you wish to automate and sort things out with Google Cloud Platform.

In the end,I hope you enjoyed reading this article. Please let me know your valuable thoughts in the comments section below. Regarding the blog post above I have posted my slides below. Hope you had a fun time experimenting and have a lovely day :)

 

The following two tabs change content below.
I am an Open Source Guy. I have been working in the DevOps and Open Source Field in the past. Contributor to the Docker, Kubernetes & DevOps community. An avid speaker and attendee at several different conferences and meetups. Creator of awesome-kubernetes. Passionate about reading Books and Traveling. Podcast Lover. Gopher.Sys admin. Cloud Infrastructure Expert.

Latest posts by Ramit Surana (see all)

Authors

Related posts

One Comment;

  1. Pingback: KubeWeekly #73 – KubeWeekly

*

Top