Ukraine needs your help!

Support
Series: Kubernetes based Jenkins

Jenkins Kubernetes Plugin

Kubernetes, Tools

Defining the environments needed for the build is not really easy if you are using the standard way of Jenkins pipelines. Here, I propose to use Jenkins Kubernetes Plugin to be able to use containers as nodes.

Jenkins has to be installed in the Kubernetes cluster to be able accessing to the cluster. How to install Jenkins to Kubernetes I described earlier here.

Kubernetes Plugin

Kubernetes Plugin allows you to create a Pod with dynamic containers, which you will define a bit later and this Pod will be used by Jenkins as a Node where will be running your pipelines.

Install the Kubernetes Plugin

Go to Manage JenkinsManage PluginsAvailable and find the Kubernetes:

Jenkins Kubernetes Plugin

Let’s install and restart your Jenkins.

Jenkins has to be able access to the cluster, for that we have to go Manage JenkinsManage Credentials and select Store named Jenkins → Global credentials, after click Add Credentials:

Jenkins Kubernetes Plugin

As a kind choose a Secret file, the Scope has to be Global to be able using this credentials for any pipelines and to File field attach the kubeconfig.yaml file.

To get kubeconfig to execute the following command in your cluster:

kubectl config view --minify

Configure Kubernetes Plugin

Go to Manage JenkinsManage Nodes and Clouds in the left menu select Configure Clouds, where you will see Kubernetes:

Jenkins Kubernetes Plugin

If you cannot see Kubernetes section, click “Add a new cloud” and select Kubernetes.

Next, we have to configure that and click Kubernetes Cloud details.

  • Kubernetes URL - an API or domain name of your cluster.

    Jenkins Kubernetes Plugin

  • Disable https certificate check - set as true if access by the defined URL above does not use HTTPS protocol.

  • Kubernetes Namespace - the name of Kubernetes Namespace where Jenkins will create new nodes for your builds.

  • Credential - as a credential choose early created creds, which contains kubeconfig.yaml.

    Jenkins Kubernetes Plugin

  • Jenkins URL - this URL is an URL to Jenkins through Kubernetes Service, in my case, it named as jenkins [http://jenkins:8080].

  • Jenkins tunnel - the same as for Jenkins URL contains the name of Kubernetes Service for Jenkins Agent by port.

    Jenkins Kubernetes Plugin

Simple Jenkinsfile based on Kubernetes

Now, we need to create a Jenkinsfile with an agent define the kubernetes {} where we could define the YAML file or as demonstrated below put the YAML content containing information about needed Node for our builds. Which containers and which version should we use.

After, we are able to use container ([name]) {} construction if we want to execute specific instructions under a particular container.

container ([name]) {
  ..
}

Bellow, I provided a simple Jenkins Pipeline where I use this way:

// Jenkinsfile

pipeline {
  agent {
    kubernetes {
      yaml """\
        apiVersion: v1
        kind: Pod
        metadata:
          labels:
            some-label: some-label-value
        spec:
          containers:
          - name: maven
            image: maven:alpine
            command:
            - cat
            tty: true
          - name: busybox
            image: busybox
            command:
            - cat
            tty: true
        """.stripIndent()
    }
  }
  stages {
    stage('Run maven') {
      steps {
        container('maven') {
          sh 'mvn -version'
        }
        container('busybox') {
          sh '/bin/busybox'
        }
      }
    }
  }
}

If you still have questions, feel free to ask in the comments below.

Comments

Alex BarchukBlog about programming 🚀bcode.dev © 2022