Kubernetes – The Kubernetes plugin for Jenkins

Ubuntu Server 20.04 LTS Guide” href=”https://devtutorial.io/ubuntu-server-20-04-lts-guide.html” target=”_blank”>Ubuntu Server 20.04 LTS Guide

One way we can use Kubernetes for our CI/CD pipeline is to run our Jenkins build slaves in a containerized environment. Luckily, there is already a plugin, written by Carlos Sanchez, that allows you to run Jenkins slaves in Kubernetes’ pods.


You’ll need a Jenkins server handy for this next example. If you don’t have one you can use, there is a Docker image available at https://hub.docker.com/_/jenkins/.

Running it from the Docker CLI is as simple as the following command:

docker run --name myjenkins -p 8080:8080 -v /var/jenkins_home jenkins

Installing plugins

Log in to your Jenkins server, and from your home dashboard, click on Manage Jenkins

A note for those installing a new Jenkins server: when you first log in to the Jenkins server, it asks you to install plugins. Choose the default ones, or no plugins will be installed!

Then, on the Manage Jenkins page, select  Manage Plugins from the list, as follows:

The main dashboard in Jenkins

The credentials plugin is required, but should be installed by default. We can check the Installed tab if in doubt, as shown in the following screenshot:

Installed plugins in Jenkins

Next, let’s click on the Available tab. The Kubernetes plugin should be located under Cluster Management and Distributed Build or Misc (cloud). There are many plugins, so you can alternatively search for Kubernetes on the page. Check the box for Kubernetes plugin and click on Install without restart. This will install the  Kubernetes plugin and the Durable Task Plugin:

The plugin installation screen in Jenkins

If you wish to install a nonstandard version, or just like to tinker, you can optionally download the plugins. The latest Kubernetes and durable task plugins can be found here:

  • Kubernetes plugin: https://wiki.jenkins-ci.org/display/JENKINS/Kubernetes+Plugin
  • Durable task plugin: https://wiki.jenkins-ci.org/display/JENKINS/Durable+Task+Plugin

Next, we can click on the  Advanced tab and scroll down to  Upload plugin. Navigate to the durable-task.hpi file and click on  Upload. You should see a screen that shows an installation progress bar. After a minute or two, it will update to  Success.

Finally, install the main Kubernetes plugin. On the left-hand side, click on  Manage Plugins and then the  Advanced tab once again. This time, upload the kubernetes.hpi file and click on  Upload. After a few minutes, the installation should be complete.

Configuring the Kubernetes plugin

Click on Back to Dashboard, or the Jenkins link in the top-left corner. Back on the main dashboard page, click on the Credentials link. Choose a domain from the list; in my case, I just used the default global credentials domain. Click on Add Credentials, and you’ll be presented with the following screen:

The Add Credentials screen

Leave Kind as Username with password and Scope as Global (Jenkins, nodes, items, all child items, etc). Add your Kubernetes admin credentials. Remember that you can find these by running the following config command:

$ kubectl config view

You can leave ID blank, fill in Description with something sensible, and then click on the  OK button.

Now that we have our credentials saved, we can add our Kubernetes server. Click on the Jenkins link in the top-left corner, and then Manage Jenkins. From there, select Configure System and scroll all the way down to the Cloud section. Select Kubernetes from the Add a new cloud drop-down menu and a Kubernetes section will appear, as follows:

The new Kubernetes cloud settings page in Jenkins

You’ll need to specify the URL for your master in the form of https://<Master IP>/.

Next, choose the credentials we added from the drop-down menu. Since Kubernetes uses a self-signed certificate by default, you’ll also need to check the Disable https certificate check checkbox.

Click on  Test Connection, and if all goes well, you should see Connection successful appear next to the button.

If you are using an older version of the plugin, you may not see the Disable https certificate check checkbox. If this is the case, you will need to install the self-signed certificate directly on the Jenkins master.

Finally, we will add a pod template by choosing Kubernetes Pod Template from the Add Pod Template drop-down menu next to Images.

This will create another new section. Use jenkins-slave for the Name and Labels section. Click on  Add next to Containers and again use jenkins-slave for the Name. Use csanchez/jenkins-slave for the Docker Image and leave /home/jenkins for the Working Directory.

Labels can be used later on in the build settings to force the build to use the Kubernetes cluster:

Kubernetes cluster addition

Here is the pod template that expands the cluster addition, as shown in the following screenshot:

The Kubernetes pod template

Click on Save and you are all set. Now, new builds created in Jenkins can use the slaves in the Kubernetes pod we just created.

Here is another note about firewalls. The Jenkins master will need to be reachable by all the machines in your Kubernetes cluster, as the pod could land anywhere. You can find out your port settings in Jenkins under Manage Jenkins | Configure Global Security.

Comments are closed.