How to set up Jenkins in Kubernetes environment

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Jenkins is an open-source automation server that enables developers around the world to reliably build, test, and deploy their software. When combined with Kubernetes, a portable, extensible open-source platform for managing containerized workloads and services, it provides a robust foundation for a Continuous Integration/Continuous Deployment (CI/CD) pipeline. In this tutorial, we’ll walk through setting up Jenkins in a Kubernetes environment, going from basic to advanced configurations, with code examples and expected outcomes where applicable.

Prerequisites

  • Kubernetes cluster (You can use Minikube for a local setup)
  • kubectl installed and configured to talk to your cluster
  • Helm v3 installed
  • Basic knowledge of Kubernetes and Jenkins

Step 1: Install Jenkins Using Helm

First, we need to add the Jenkins Helm chart repository and update our Helm repo list:

helm repo add jenkinsci https://charts.jenkins.io
helm repo update

Now, let’s install Jenkins using Helm by creating a custom values file to configure Jenkins parameters:

cat >> jenkins-values.yaml <<EOF
controller:
  adminUser: admin
  adminPassword: <your_password>
  serviceType: NodePort
  nodePort: 32000
  persistence:
    storageClass: use-the-existing-storage-class-here
EOF

Install Jenkins:

helm install jenkins jenkinsci/jenkins -f jenkins-values.yaml

This will set up Jenkins with the specified parameters. You can access Jenkins by finding the NodePort exposed on your Kubernetes cluster:

kubectl get svc -n default

This command lists all services in the default namespace. Look for the ‘jenkins’ service and note the NodePort next to it.

Step 2: Configure Jenkins

Once Jenkins is up, navigate to it using your browser. You’ll be prompted to login with the `admin` username and the password you set in the jenkins-values.yaml file.

After logging in, you’ll be able to customize Jenkins, install plugins, and configure it according to your project needs.

Step 3: Setting up a Jenkins Pipeline

Jenkins pipelines are automated paths that software development processes follow from version control to deployment. To set up a Jenkins pipeline in Kubernetes, create a Jenkinsfile in your repository with the following content:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo "Building..."'
            }
        }
        stage('Test') {
            steps {
                sh 'echo "Testing..."'
            }
        }
        stage('Deploy') {
            steps {
                sh 'echo "Deploying to Kubernetes"'
            }
        }
    }
}

Now, configure Jenkins to build this pipeline:

  • Go to Jenkins dashboard
  • Click on ‘New Item’
  • Name your pipeline and choose ‘Pipeline’ project
  • In the pipeline configuration, select ‘Pipeline script from SCM’
  • Choose the SCM (e.g., Git), enter your repository URL, and path to your Jenkinsfile

When you run this pipeline, Jenkins will execute each stage in the Jenkinsfile, echoing the build, test, and deployment steps.

Step 4: Advanced Configuration – Utilizing Kubernetes Resources

For more advanced Kubernetes integrations, you can configure Jenkins pipelines to utilize Kubernetes resources such as Pods and Volumes directly. This requires installing the Kubernetes plugin in Jenkins.

Once the plugin is installed, you can define a pipeline that runs each stage inside a Kubernetes pod:

pipeline {
    agent {
        kubernetes {
            yaml """
            apiVersion: v1
            kind: Pod
            spec:
              containers:
              - name: maven
                image: maven:3-alpine
                command:
                - cat
                tty: true
            """
        }
    }
    stages {
        stage('Build') {
            steps {
                container('maven') {
                    sh 'mvn clean package'
                }
            }
        }
        stage('Test') {
            steps {
                container('maven') {
                    sh 'mvn test'
                }
            }
        }
    }
}

This Jenkinsfile specifies a pipeline that runs in a Kubernetes pod using a Maven container for build and test stages.

Conclusion

Setting up Jenkins in a Kubernetes environment harnesses the power of both platforms, bringing together the flexibility of Jenkins pipelines with the scalability and resilience of Kubernetes. Following the steps outlined in this tutorial, you can set up a basic Jenkins instance ready for CI/CD in Kubernetes and explore more complex configurations utilizing Kubernetes resources. The journey to fully automated, scalable CI/CD pipelines is an exciting one, and Jenkins with Kubernetes is an excellent place to start.