How to create Jenkins pipeline to run on Kubernetes?

Most organizations use Jenkins as there CI/CD automation tool and Kubernetes as the platform to run the application. We can achieve almost everything using Jenkins pipeline from building an application to deploying it and upgrading the code in production. Let's see it with example.


In legacy systems Jenkins pipeline used to run on worker node scheduled through master node. In containerization world we can not have extra virtual machine just for running Jenkins pipeline instead we can use existing Kubernetes cluster and schedule our pipelines in form of container.

For configuring Kubernetes with Jenkins we use "Kubernetes Plugin".

How to Create, Update, Delete and Trigger Jenkins job using API?
Jenkins provides REST API to automate actions from external systems. We can create, update, delete and trigger Jenkins Jobs/Pipeline using API.

Configure Kubernetes Cloud in Jenkins

Once Jenkins and Kubernetes plugin are installed it's time we connect it with our Kubernetes cluster. For configuration we need access to Kubernetes cluster with credentials like username and password or secret token to access the cluster.

Go to configure page of Jenkins you will see "Configure clouds" section and add details:

  • Name of your cloud: When you add multiple clouds Name will be used to run pipeline on particular cloud.
  • Kubernetes URL: Kubernetes API URL, can be found in Kubernetes configuration details
  • Server certificate key: Server key for the Kubernetes certificate. For testing you can disable https certificate check instead of certificate key.
  • Credentials: You can add credentials in "Manage Credentials" section. For secret use secret text as the credential type.
Add Credential in Jenkins
Add Credential in Jenkins
  • After filling all the details you can test connection if it's successful, then we can proceed further.
Configure Cloud in Jenkins
Configure Cloud in Jenkins
  • Add Jenkins URL, Jenkins Tunnel and keep timeout parameters as it is, you can tweak these parameters going further according to your needs.
  • Note: Jenkins tunnel should not have protocol mentioned

We can add multiple clouds Kubernetes plugin supports multiple flavors of Kubernetes including Openshift.

Jenkins minimal installation on Kubernetes
We can install Jenkins in different ways but sometimes we just need Jenkins with minimal resources and configuration.

Configure Pod Template

Pod template specifies how your pod will look like, what labels it will have and in which namespace it will be created the cluster.

  • Name: Name of the pod
  • Namespace: In which namespace pod should be created. Keeping it blank as we can override this in pipeline.
  • Labels: Custom Labels for spawn pod
Pod Template configuration

Configure Container Template

Each pod can have multiple containers. By default Jenkins will spawn jnlp container with default image which will handle communication between Jenkins and the Pod.

If you want to use custom agent image you can add container with name "jnlp" with image details.

Container Template Configuration

Add custom environment variable if needed for the container.

Once this configuration is complete now we can head over to creating a pipeline which will run on this configured cluster.

Create a test Pipeline

Now create a pipeline with pipeline script using new item wizard. Use "Pipeline" as type and go further to configure it.

Jenkins Create pipeline wizard
Jenkins Create pipeline wizard

In "Advanced Project Options" select "Pipeline Script" and paste code given below in the snippet.

Jenkins Create pipeline wizard
Jenkins Create pipeline wizard

Below declarative code can be used to create a test pipeline.

Note: cloud directive specifies name of the cluster we configured.

pipeline {
    agent {
        kubernetes {
            label "build-pod"
            cloud "kubernetes"
            yaml '''
apiVersion: v1
kind: Pod
metadata:
  namespace: build-ns
  labels:
    job: bootvar-build-pod
spec:
  containers:
  - name: bootvar-container
    image: alpine:latest
    tty: true
    command: ['cat']
'''
        }
    }
    stages {
        stage("First") {
            steps {
                container("bootvar-container") {
                    sh "ls -lart"
                }
            }
        }
    }
}

Now run the pipeline it should start pod on the Kubernetes cluster.

PS C:\Users\Suhas\Documnts> kubectl get pods --namespace=build-ns     
NAME                                    READY   STATUS      RESTARTS   AGE
build-pod-6c58b4d8bb                    1/1     Running     0          3s
Suhas Adhav
In love with web3, DApps and Blockchain Technology | DevOps Expert | Kubernetes | Docker | Jenkins | Cloud | Hadoop
The Internet