Jenkins builds on Kubernetes cluster

Reading Time: 4 minutes

jenkins build on kubernetes cluster

Hello everyone, so in this blog, we are going to learn today that how can we run Jenkins build in Kubernetes as a pod.

Jenkins is an open-source tool written in java and also have a plugin built for continuous integration and build automation purpose. It helps in automating the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery.

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation.

Prerequisite

  1. Jenkins master server running on local or as a docker container.
  2. Kubernetes cluster running on minikube used to run jenkins builds.

Concept of running Jenkins build on Kubernetes

Jenkins supports the master-agent architecture (many build agents completing work scheduled by a master server) making it highly scalable. The master’s job is to schedule build jobs, distribute the jobs to agents for actual execution, monitor the agents, and get the build results. Master servers can also execute the build job directly.

The agents’ task is to build the job sent by the master. A job can be configured to run on a particular type of agent, or if there are no special requirements, Jenkins can simply choose the next available agent.

Jenkins scalability provides many benefits:

  • Running many build plans in parallel using pods
  • Automatically spinning up and removing agents to save costs
  • Distributing the load

Setting up the things

I assume that you already have jenkins server and minikube running.

Installing Kubernetes plugin in jenkins

Firstly, we need to login to jenkins master server and install the kubernetes plugin. For this you need to follow the following steps:

manage jenkins> manage plugin > available > Kubernetes

And install the kubernetes plugin.

Configuring Kubernetes Plugin

Now, we need to configure kubernetes plugin so that our jenkins build will run on pods. Goto following way:

manage jenkins> configure system> cloud> add new cloud > kubernetes

After going as above you will see an option as Kubernetes Cloud details Click on it to configure Kubernetes plugin:

kubernetes cloud setup on jenkins

After that you need to provide some configurations:

  1. You can add all the cluster information manually like URL, key, and namespace otherwise you can add a config file that is present in.kube directory as credentials.
  2. For credentials, you need to add credentials as a secret file and select .kube/config, so it will automatically get all the information regarding your cluster.
  3. Then click on test connection, if it show version details means connection is successful.
  4. Now leave every thing default.
  5. Now add pod label according to your use case so that you can easily access these pods from CLI using these labels.

pod template setup

6. Now, click on Pod Template and give the meaningful name of your pod.

pod template details

7. Then click on Pod Template details.

pod template details

8. Now provide pod template specification like name, namespace, labels (here labels are necessary because it is used to specify that use these label for jenkins build on kubernetes).

9. Then, click on save and you are successfully setup the kubernetes plugin.

10. You don’t need to specify any container and environment variable information as this plugin already setup this for you. It basically use image of jenkins/inbound-agent:4.3-4 to setup slave on pods.

*note: Also you need to do one thing go in configure global security and set tcp port for inbound agent to 50000

Configuring Jenkins job to use kubernetes

So now create a new freestyle project.

  1. Now in the general setting check the box Restrict where this project can be run and give the label of the pod you given in the pod template.

pipeline labels

2. Now after this add a build step as shell execute and write a command hostname and save the project.

jenkins pipeline

3. Now build the project and you will see a new pod is created as a slave and performing the build step on pods.

build status

4. After the build step is finished the pod get terminated automatically.

Console Output

jenkins console output

In the console output we also have the same pod name which is used for jenkins build.

Conclusion

After reading this blog you learned how can we run jenkins build on kubernetes easily and efficiently. We can also setup this easily on any kubernetes cluster easily like gcp, eks etc.

References

https://plugins.jenkins.io/kubernetes/

knoldus Bottom Image