Configure Jenkins with Kubernetes.

Chathura Siriwardhana
5 min readJan 11, 2020

--

Introduction

In the production environment, to run a parallel build we can use static Jenkins slave node. You can read more about how to configure Jenkins slave node on this article. In this approach we have to keep slave nodes up and running even there are no build jobs runs. Running slave nodes without running the build on them is a resource wastage. Also, this approach is costly. By dynamically creating and adding slave nodes only when a job is scheduled, we can be optimised the compute resource utilisation on the datacenter or on the cloud. You can use Docker or Kubernetes to dynamically add Jenkins slave nodes to run jobs. This article describes how to integrate on-premise or hosted Kubernetes with Jenkins to run build jobs.

Prerequisite

In order to follow this article, you need to have an installed Jenkins master server and a running Kubernetes cluster. Setup was tested on,

Preparing the Kubernetes cluster

You need to have a server account to connect Jenkins to the cluster. Using kubectl client, create a service account. Use the below command in the terminal to create the services account.

# kubectl create serviceaccount jenkins

Once created, Kubernetes will create a secret token bound to the service account. Using below command on the terminal list the secrets and find the secret bound to jenkins service account.

# kubectl get secret# kubectl describe secrets jenkins-token-z259g

Configure Jenkins Master

Now, copy the secret token and login to your Jenkins master server. From the navigation bar on the left-hand corner, navigate to Credentials -> Systems and click on “Add Credentials”. From the drop-down menu, select “Secret Text” as the kind. On the secret text field past the token, you copped from the Kubernetes service account. On the ID field, type a human-readable name and press ok.

Secret Creation step 01

Integrate Jenkins with Kubernetes

You need to install Kubernetes Jenkins plug-in on Jenkins to integrate Jenkins with Kubernetes. To install the plugin from the left-hand navigation menu, navigate to Manage Jenkins -> Manage Plugins and click on the “Available” tab. On the search box appeared on the right upper corner, search for Kubernetes. At the time of writing this article plugin version is 1.22.5. After installation, go to “Configure System” under Manage Jenkins. Scroll the page to the bottom, there you can see a section named “Cloud” with add new cloud drop-down. If you haven’t install any plugin go under the cloud, only item available you to select is the Kubernetes. Click on Kubernetes and add a new cloud.

Jenkins Kubenetes Configurations

On the name field, name your cloud. For simplicity, I am keeping this as Kubernetes. On the Kubernetes URL, enter your Kubernetes API servers IP / domain name or front load balancer IP address. Select the service account token secret text as the credentials from the drop-down. According to your Kubernetes cluster installation, you may or may not need to disable https certificate check. You can test the connectivity to your Kubernetes cluster by clicking on the “Test Connection” button. A little bit down the page under Pod Templates add a new pod template. Give the pod template a name and a label. Adding a name and a label is a must. You can play around other settings later. For now, I will keep all the default settings.

Test the Jenkins integration with Kubernetes

From the Jenkins main page, by clicking on the new item, add a new freestyle job. Find and mark a tick on “Restrict where this project can be run” and on the text field appear, enter the label name you gave to the pod template. Add a simple bash shell script as the build. I used the below simple bash script.

echo Hi from Kubernetes Pod
java -version
sleep 10

Run the build and test your working. If you did all correct build should runs on a Kubernetes pod.

Take the full power of the Kubernetes with Jenkins

When we run the build plugin use it’s default pod definitions YAML. You can add your own YAML. To add your own pod definition, back in the plugin configuration window you need to add your YAML file or you can fill the fields in the container template. Under pod template, Containers add a container template. Container name must be “jnlp” and add your docker image. Or else, as I mentioned earlier you can use the pod definition in the YAML format. To use YAML, instead of adding a container template find and add your pod YAML to “Raw yaml for the Pod”. Keep in mind you always need to use the name as jnlp.

Jenkins Kubernetes plugin use jenkins/jnlp-slave as its default image. When creating your docker image use below links to start and finally build a docker image like jenkins/jnlp-slave.

https://hub.docker.com/r/jenkins/jnlp-slavehttps://hub.docker.com/r/jenkins/slavehttps://github.com/jenkinsci/docker-slavehttps://github.com/jenkinsci/docker-jnlp-slave

--

--

No responses yet