Jenkins Kubernetes Plugin Overview - unix1998/technical_notes GitHub Wiki

Jenkins Kubernetes Plugin Overview

The Jenkins Kubernetes plugin allows Jenkins to dynamically create Kubernetes pods that run your Jenkins agents. These pods are defined using various templates that specify the containers, volumes, and other configurations needed for the build environment.

Key Components

  1. ContainerTemplate:

    • Defines a container within a Kubernetes pod. Each ContainerTemplate can specify the Docker image, environment variables, resource limits, and other container-specific configurations.
    containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent', args: '${computer.jnlpmac} ${computer.name}'),
    containerTemplate(name: 'maven', image: 'maven:3.6.3-jdk-8', ttyEnabled: true, command: 'cat')
    
  2. PodTemplate:

    • Defines the overall pod configuration, which can include one or more ContainerTemplate definitions. A PodTemplate can also specify volumes, service account credentials, and other pod-level settings.
    podTemplate(label: 'mypod', containers: [
        containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent', args: '${computer.jnlpmac} ${computer.name}'),
        containerTemplate(name: 'maven', image: 'maven:3.6.3-jdk-8', ttyEnabled: true, command: 'cat')
      ],
      volumes: [
        hostPathVolume(mountPath: '/home/jenkins', hostPath: '/mnt/jenkins')
      ])
    
  3. HostPathVolume:

    • Defines a volume that mounts a directory from the host node's filesystem into the pod. This can be used to share data between the host and the pod.
    hostPathVolume(mountPath: '/home/jenkins', hostPath: '/mnt/jenkins')
    

Example of a Jenkins Pipeline Using Kubernetes Plugin

Here's an example Jenkins pipeline script that defines a pod template with two containers and a host path volume:

pipeline {
    agent {
        kubernetes {
            label 'mypod'
            yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    some-label: some-value
spec:
  containers:
  - name: jnlp
    image: jenkins/inbound-agent
    args: ['\${computer.jnlpmac}', '\${computer.name}']
  - name: maven
    image: maven:3.6.3-jdk-8
    command:
    - cat
    tty: true
  volumes:
  - name: my-volume
    hostPath:
      path: /mnt/jenkins
      type: Directory
"""
        }
    }
    stages {
        stage('Build') {
            steps {
                container('maven') {
                    sh 'mvn clean package'
                }
            }
        }
    }
}

Key Points in the Example:

  • Pod Definition in YAML: The agent section uses the kubernetes directive to define the pod template in YAML format. This includes defining the containers and volumes.
  • Containers: The pod includes two containers: jnlp for the Jenkins agent and maven for running Maven commands.
  • Volumes: A HostPathVolume is defined to mount /mnt/jenkins from the host into the pod at /home/jenkins.

Using Declarative Syntax

If you prefer to use the declarative syntax with Jenkins pipelines, you can also define pod templates and container templates directly within the Jenkinsfile, like so:

pipeline {
    agent {
        kubernetes {
            label 'mypod'
            defaultContainer 'jnlp'
            yaml """
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: maven
    image: maven:3.6.3-jdk-8
    command:
    - cat
    tty: true
  volumes:
  - name: my-volume
    hostPath:
      path: /mnt/jenkins
      type: Directory
"""
        }
    }
    stages {
        stage('Build') {
            steps {
                container('maven') {
                    sh 'mvn clean package'
                }
            }
        }
    }
}

Summary

  • ContainerTemplate, PodTemplate, and HostPathVolume are part of the Jenkins Kubernetes plugin.
  • These templates allow you to define and configure the Kubernetes pods that Jenkins uses to run jobs.
  • You can define these templates directly in the Jenkins pipeline script, either in Groovy DSL or using YAML syntax.