Integrating Jenkins with a Kubernetes Cluster for CI/CD

Jenkins is one of the most widely used tools for CI/CD. Integrating Jenkins with Kubernetes allows for a more scalable, efficient, and flexible CI/CD environment, enabling organizations to automate the build, test, and deployment processes in a cloud-native way.

Integrating Jenkins with a Kubernetes Cluster for CI/CD

Jenkins is one of the most widely used tools for continuous integration (CI) and continuous delivery (CD). Integrating Jenkins with Kubernetes allows for a more scalable, efficient, and flexible CI/CD environment, enabling organizations to automate the build, test, and deployment processes in a cloud-native way. Kubernetes offers a platform for orchestrating containerized applications, and when integrated with Jenkins, it can help dynamically manage Jenkins agents, run builds in isolated environments, and deploy applications seamlessly to Kubernetes clusters.

In this post, we’ll walk through the process of integrating Jenkins with Kubernetes for CI/CD, covering the setup, configuration, best practices, and how to leverage this integration to automate end-to-end application deployments.

1. Why Integrate Jenkins with Kubernetes

Kubernetes provides a robust and scalable platform for running containerized applications, and Jenkins is a powerful tool for automating the CI/CD pipeline. When integrated, these two tools can provide significant benefits:

  • Dynamic Agent Provisioning: Jenkins can use Kubernetes to provision agents on-demand, providing an efficient way to run builds in isolated environments.
  • Scalability: Kubernetes automatically scales Jenkins agents based on workload demand, allowing for faster builds and testing cycles.
  • Isolation: Each Jenkins job can be run in a separate Kubernetes pod, ensuring clean and isolated build environments for each job.
  • Cloud-Native Deployment: Applications can be built, tested, and deployed directly into Kubernetes, simplifying the CD process.
  • Resource Efficiency: By using Kubernetes, Jenkins agents are only spun up when required, saving resources during idle times.

2. Prerequisites

Before integrating Jenkins with Kubernetes, ensure you have the following prerequisites in place:

  • Kubernetes Cluster: A Kubernetes cluster (e.g., managed Kubernetes service like Amazon EKS, Google GKE, or an on-premise cluster).
  • Jenkins Installed: Jenkins should be installed and running, either on a Kubernetes cluster or a standalone environment.
  • Kubernetes Plugin for Jenkins: The Kubernetes Plugin must be installed in Jenkins, which will allow Jenkins to provision Kubernetes pods as agents.
  • kubectl: Installed and configured to interact with your Kubernetes cluster (optional for cluster management).

3. Jenkins-Kubernetes Integration Overview

The integration between Jenkins and Kubernetes leverages the Kubernetes Plugin for Jenkins. This plugin allows Jenkins to dynamically create Kubernetes pods as agents to run CI/CD jobs. It can also be used to deploy applications to Kubernetes as part of the CD pipeline.

Key Components of the Jenkins-Kubernetes Integration:

  1. Jenkins Controller: Manages the Jenkins jobs and provisions Kubernetes pods as agents.
  2. Kubernetes Plugin: Manages communication between Jenkins and the Kubernetes cluster.
  3. Kubernetes Pods as Agents: Each CI/CD job runs in an isolated Kubernetes pod, which is provisioned dynamically.
  4. Jenkins Pipeline: A Jenkinsfile defining the CI/CD steps, including build, test, and deployment stages.
  5. Kubernetes Cluster: The infrastructure where the application is deployed and managed.

4. Setting Up Jenkins on Kubernetes

4.1 Installing Jenkins on Kubernetes

Running Jenkins itself on Kubernetes is a popular approach for leveraging the scalability and flexibility of Kubernetes. Here’s how you can install Jenkins on Kubernetes:

Step 1: Create a Namespace for Jenkins

It’s a good practice to isolate Jenkins in its own namespace:

kubectl create namespace jenkins

Step 2: Install Jenkins using Helm

Helm is a package manager for Kubernetes that simplifies the installation of complex applications like Jenkins. To install Jenkins using Helm:

helm repo add jenkins https://charts.jenkins.io
helm repo update
helm install jenkins jenkins/jenkins --namespace jenkins

Step 3: Access Jenkins

After installation, Jenkins can be accessed via a Kubernetes service. To get the admin password and access Jenkins:

kubectl get svc --namespace jenkins
kubectl exec --namespace jenkins -it $(kubectl get pods --namespace jenkins -l "app.kubernetes.io/component=jenkins-master" -o jsonpath="{.items[0].metadata.name}") -- cat /run/secrets/chart-admin-password

Access Jenkins via your browser using the service IP and port.

4.2 Configuring Jenkins for Kubernetes Agent Provisioning

Once Jenkins is installed, configure it to use Kubernetes for dynamic agent provisioning:

  1. Install the Kubernetes Plugin: Go to Manage Jenkins > Manage Plugins and install the Kubernetes Plugin.
  2. Configure Kubernetes Cloud:
    • Navigate to Manage Jenkins > Configure System.
    • Scroll down to Cloud and click Add a new cloud > Kubernetes.
    • Provide the Kubernetes API URL, Jenkins URL, and configure the Kubernetes Service Account to allow Jenkins to provision agents.
  3. Create Pod Templates: Pod templates define the containers in which the Jenkins jobs will run. You can create different templates for different types of jobs.

5. Integrating Jenkins with Kubernetes for CI/CD

With Jenkins configured to use Kubernetes, the next step is to set up CI/CD pipelines that build and deploy applications to Kubernetes.

5.1 Configuring Jenkins Pipeline for Kubernetes

A Jenkinsfile defines the pipeline for building, testing, and deploying the application. Here’s an example of a Jenkinsfile that uses Kubernetes agents and deploys an application to a Kubernetes cluster:

pipeline {
    agent {
        kubernetes {
            label 'my-k8s-agent'
            defaultContainer 'jnlp'
            yaml '''
            apiVersion: v1
            kind: Pod
            spec:
              containers:
              - name: maven
                image: maven:3.6.3-jdk-8
                command:
                - cat
                tty: true
              - name: kubectl
                image: lachlanevenson/k8s-kubectl
                command:
                - cat
                tty: true
            '''
        }
    }
    stages {
        stage('Build') {
            steps {
                container('maven') {
                    sh 'mvn clean install'
                }
            }
        }
        stage('Test') {
            steps {
                container('maven') {
                    sh 'mvn test'
                }
            }
        }
        stage('Deploy to Kubernetes') {
            steps {
                container('kubectl') {
                    sh 'kubectl apply -f deployment.yaml'
                }
            }
        }
    }
}

5.2 Automating Deployments to Kubernetes

In the pipeline example above, the Deploy to Kubernetes stage uses kubectl to deploy the application. The Kubernetes YAML file defines the deployment, service, and other resources. Jenkins can automatically apply the YAML file after a successful build and test process.

5.3 Using Helm for Kubernetes Application Deployment

Helm simplifies the deployment of applications to Kubernetes. Instead of managing individual Kubernetes YAML files, you can package your application into a Helm chart and deploy it via Jenkins.

Example Jenkinsfile for Helm Deployment:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
        stage('Deploy to Kubernetes with Helm') {
            steps {
                sh 'helm upgrade --install myapp ./helm-chart/'
            }
        }
    }
}

6. Best Practices for Jenkins and Kubernetes Integration

  • Use Pod Templates: Define reusable pod templates for different job types to avoid duplication.
  • Isolate Jobs in Separate Pods: Each Jenkins job should run in an isolated pod to ensure that builds are clean and independent.
  • Leverage Auto-scaling: Enable auto-scaling in Kubernetes to dynamically adjust the number of nodes based on Jenkins job demand.
  • Use Secret Management: Use Kubernetes secrets to securely manage credentials and sensitive information.
  • Use Helm: Package your application as a Helm chart to simplify deployment and versioning.

7. Monitoring and Scaling Jenkins with Kubernetes

Monitoring Jenkins

  • Jenkins Dashboard: Monitor jobs and agents via the Jenkins dashboard.
  • **

Prometheus and Grafana**: Integrate Jenkins with Prometheus and Grafana for detailed metrics on resource usage, job performance, and more.

Scaling Jenkins with Kubernetes

Kubernetes can automatically scale Jenkins agents based on workload. Use Kubernetes Horizontal Pod Autoscaler (HPA) to adjust the number of nodes in the cluster as Jenkins jobs are submitted. You can also scale Jenkins itself if necessary by deploying multiple Jenkins controllers.

Conclusion

Integrating Jenkins with Kubernetes provides a powerful platform for automating CI/CD workflows. Jenkins can dynamically provision agents on Kubernetes, enabling you to efficiently manage resources, scale based on demand, and deploy applications directly into Kubernetes clusters. By leveraging Kubernetes features like pod isolation, auto-scaling, and Helm, Jenkins can be transformed into a highly scalable and efficient CI/CD tool for cloud-native applications.

With this integration, you can automate the entire process, from code commits to application deployment, ensuring a faster and more reliable delivery pipeline.

Read next