Effectively Using the Kubernetes Plugin with Jenkins

Editor’s Note: This is a guest blog post written by Niklas Tankskanen, consultant for eficode.com. This post first appeared in GitHub. 

Kubernetes, the container orchestration platform is rapidly becoming popular. There are more and more workloads that you can run on top of Kubernetes. It’s becoming an enabling layer of your hyper-convergened infrastructure.

If you set up Kubernetes as a cloud provider in Jenkins, you’ll get a very powerful couple for running your workloads. To do that, you can simply install the Kubernetes plugin. Kubernetes is able to run your Jenkins workloads as long as they run in a container. And containers are an awesome way if your workload is a build, because you can pack all your application and OS dependencies in a container and then run it anywhere!

Let’s imagine that you have been running a Kubernetes cluster setup in your organization for a while now. First, it was all about proof of concept but now it’s becoming more popular with your developers and you have to think about scaling and orchestration. Resource quotas are a part of that and every responsible operator should set those up in both development and production clusters. Otherwise people will be lazy and just reserve all the resources of your cluster without actually using those resources for anything. By introducing quotas into your cluster, you can control how many resources each namespace should have.

Quotas are a mature feature of Kubernetes already. You have the possibility to create very fine-grained quotas for different hardware resources, whenever it’s fast disk, GPUs or CPU time. You can also specify multiple scopes of quota per one namespace. For example, you can have a quota for workloads that are to infinity like web servers or databases. Or have quota for workloads that are short-lived like builds or test automation runs.

Scope Description
Terminating Match pods where .spec.activeDeadlineSeconds >= 0
NotTerminating Match pods where .spec.activeDeadlineSeconds is nil
BestEffort Match pods that have best effort quality of service.
NotBestEffort Match pods that do not have best effort quality of service.

Different scopes of the Kubernetes quota

Since Jenkins is all about running short workloads, you should aim for the Terminating scope of quota. But how do you specify workloads in Jenkins so that correct scope is used?

If you were to do this in Kubernetes, you have to specify .spec.activeDeadlineSeconds. The same field can also be specified by the Kubernetes plugin when you are specifying a pod template.

Specifying .spec.activeDeadlineSeconds in the Kubernetes plugin.

The same configuration is available in the Jenkinsfile as well if you don’t like static configurations.

podTemplate(label: 'maven', activeDeadlineSeconds: 180, containers: [
    containerTemplate(name: 'maven', image: 'maven:3.5.4-jdk-10-slim')
  ]) {
  // maven magic
}

This was just a small sample of features of the Kubernetes plugin in Jenkins. For more, be sure to attend our talk at DevOps World | Jenkins World 2018 where we share more of how you can utilize Kubernetes with Jenkins!