DevOps CI/CD Pipeline with Jenkins, Kubernetes & GitHub: Part 1

How to set up Jenkins and CI/CD pipelines using GitHub?

@pramodchandrayan
SysopsMicro
7 min readMay 23, 2021

--

source

I feel:

“DevOps is a must for all kinds of tech startups, small or big. It may be complex to start with but will make your life hell easy going forward and can save loads of time and energy for the organization to scale, scale and scale..”

Today we will look into how we can set up a CI/CD pipeline to automate the code deployment using the power of Jenkins.

In this part, we will cover :

  • How to build a Jenkins Docker image?
  • How to deploy the Jenkins image onto our local Minikube cluster?
  • How to run the deployed Jenkins image on our web browser?

Prerequisite Steps :

  • Install docker desktop
  • Create GitHub organization, give it a name, and fork all of the sample repositories from the given URL :

Once you are done with the above-mentioned steps, its time that we learn how to setup Jenkins in our local Minikube cluster

How To Setup Jenkins on Our Minikube K8s cluster?

If you have forked all the repositories from my Github organization repository account, you will find one repo named:

So now you can simply clone the same using the following K8s command, let’s see step by step how to do so

I will be using my mac terminal to run all my bash commands

Step1: Start Minikube cluster :

Here, I am specifying a memory of 4 GB, because Jenkins is quite resourced intensive so please ensure you are starting the Minikube cluster with these extra flags.

$ minikube start — memory 4028
Fig 1.0: Start minkuibe

If you managed to successfully start the minukube you will see the output as shown in Fig 1.0

Then test if everything is fine by coining the below command

$ minikube status 
minikube status output

You should be able to see the output as shown above.

2. Clone the Sample repo:

Given below

In your local machine and keep it inside a folder named CICDdemo(you can keep whatever name to your liking )

3. Enter into the CICDdemo folder in your terminal using the below-given, “change directory” command

$ cd /Applications/K8s/CICDDemo

you will get to see jenkins folder which will have two files. If you further go inside the jenkins folder by changing the directory from your terminal, you will be seeing the below-given files.

  • jenkins.yaml
  • Dockerfile

As shown below,

fig 2.0

Now that you have cloned and are in the jenkins directory, it’s time to build the docker image of Jenkins.

3. Build Jenkins Docker Image:

We will be building a jenkins docker image using Dockerfile present inside the jenkins folder, by making use of the docker build image command, but before that, it is important to change the environment variable so that we can make use of the local docker-daemon service running inside the minikube cluster,

Assuming that you are into your local jenkins directory, type the following commands

$ minikube docker-env$ eval $(minikube -p minikube docker-env)

This will point your shell to dicker-daemon, run, as shown below

fig 3.0

Check that you are able to get all the pre-existing docker images in that docker-daemon env:(refer the fig 3.0, for command and output )

$ docker image ls 

Now that you have the docker -daemon running we can now go ahead and build the docker image by using the command shown below :

$ docker image build -t myjenkins .

Note!, the tag for the Jenkins image is myjenkins , which should match with the image tag mentioned in jenkins.yaml file (existing in your local directory), as shown below, and don't forget to apply the dot at the end with a single space while typing in your terminal, as this instructs the system to build the image from the local Docker file.

4. Wait for the Jenkins Docker Image to build :

Mind it this process is quite heavy and may take some time, so be patient until you get the following message

  • “Successfully built “imageid(this will vary for you)” ”
  • “Successfully tagged myjenkins: latest”

As can be seen below in Fig 4.0

Fig 4.0

5. Deploy Jenkins Image :

Assuming that you are still in your local folder where jenkins.yaml file exists, if yes go ahead and type this k8s CLI command, to create a Jenkins deployment file using the jenkins docker image, we just created in the previous process

$ kubectl apply -f jenkins.yaml

6. Check If Jenkins image is deployed :

$ kubectl get all

You can see that,

  • pod: jenkins-79966d8db-x4kn7 is deployed and the jenkins
  • service(service/jenkins) is also up and running on the NodePort 30020
  • Deployment: deployment.apps/jenkins is also up and running on the NodePort: 31000

7. Running the Jenkins In Your Web :

If everything is set up. nicely without any error, you need to type the following command :

$ minikube ip

The above command gives the local IP of the minikube cluster, we will need this IP, to run the Jenkins UI and see it running in our web

$ minikube ip

As can be seen, for me my cluster IP is: 192.168.49.2

Open the web and type this URL given below :

192.168.49.2:31000, and you will see the following screen with a welcome message

What If Your URL is not working?

It may happen if you are using the docker driver while starting the minikube , instead of the virtual box. In this situation minikube IP will not function and you will be required to start the local tunnel from your terminal using the following commands,

$ minikube service jenkins --url

The above commands start the tunnel for your jenkins service and give you the URL which needs to be used along with NodePort:53960 or 53961(this may vary in your case ), to access the Jenkins UI on your web browser

For me the URL will look like this:

“127.0.0.1:53960”, type this and now your Jenkins UI should work in the browser.

Congratulations!!!

Congratulation you have successfully configured the Jenkins on your local minikube cluster and now you are all set to, configure the Jenkins interface and start automating the code repository deployment without much fuss

What’s Next?

As the length of this piece is becoming big, in part 2 of this series, we will cover how to Configure multibranch repository and GitHub organization, using Jenkins UI interface, and will see its magic to automate our CI/CD pipeline.

Part2 :

Signing Off Message:

“ When people within an organization, start collaborating with the intent to help each other the fabric for a stronger pipeline gets established which is bound to create success and happiness in the long run. DevOps is one such powerful tool to builds such a long-lasting culture ”

Thanks a ton for being with me and see you soon in the next part of CI/CD with Jenkins…

--

--

@pramodchandrayan
SysopsMicro

Building @krishaq: an Agritech startup committed to revive farming, farmers and our ecology | Writes often about agriculture, climate change & technology