4 min read

CI/CD: Running Jenkins Pipelines on Kubernetes Made Simple

In containerization world we run Jenkins pipeline on Kubernetes platform as a pod instead of running it on legacy nodes.
CI/CD: Running Jenkins Pipelines on Kubernetes Made Simple

Many organizations rely on Jenkins as their go-to CI/CD automation tool, while Kubernetes serves as the preferred platform for running applications. Jenkins pipelines are incredibly versatile, allowing us to handle a wide range of tasks, including building applications, deploying them, and even orchestrating production code upgrades. Let's explore this synergy with a practical example.

Table fo contents

In traditional setups, Jenkins pipelines operated on worker nodes, orchestrated by the master node. However, in the containerized landscape, allocating additional virtual machines solely for running Jenkins pipelines isn't practical. Instead, we leverage existing Kubernetes clusters to efficiently schedule our pipelines within containers.

To facilitate this integration, we employ the Kubernetes Plugin for Jenkins configuration.

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
  namespace: build-ns
    job: bootvar-build-pod
  - 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


This guide has unveiled the dynamic partnership between Jenkins and Kubernetes, pivotal forces in modern software development. With Jenkins pipelines as versatile tools for application development and deployment, we've demonstrated their prowess through practical examples.