DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Containerize Gradle Apps and Deploy to Kubernetes With JKube Kubernetes Gradle Plugin
  • Create a Kubernetes Cluster With Centos
  • Setup and Configure Velero on AKS

Trending

  • How to Format Articles for DZone
  • How to Convert XLS to XLSX in Java
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • How To Replicate Oracle Data to BigQuery With Google Cloud Datastream
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to Set Up Jenkins on Kubernetes

How to Set Up Jenkins on Kubernetes

By 
Sudip Sengupta user avatar
Sudip Sengupta
DZone Core CORE ·
May. 26, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
13.2K Views

Join the DZone community and get the full member experience.

Join For Free

This guide will walk you through the process of setting up Jenkins on Kubernetes. Jenkins is a widely-used open source CI server that provides hundreds of plugins to support building, deploying and automating your projects.

In the next sections, you will:

  1. Create a Kubernetes cluster with minikube (This is an optional step. If you already have a running Kubernetes cluster, then you can skip this step)
  2. Create a namespace and a PersistentVolume for Jenkins
  3. Install Jenkins using Helm
  4. Set up and run a pipeline that tests a simple web application

Prerequisites

  1. Docker. For details about installing Docker, refer to the Install Docker page.
  2. A Kubernetes cluster. If you don’t have a running Kubernetes cluster, see the “Create a Kubernetes Cluster with minikube” section.
  3. Helm CLI. To install Helm CLI, follow the instructions from the Installing Helm page.

Create a Kubernetes Cluster with minikube (Optional)

Minikube is a tool that creates a single-node Kubernetes cluster on your computer. Follow these steps if you don't have a running Kubernetes cluster:

  1. Install minikube by following the steps from the Install minikube page
  2. Install kubectl. See the instructions from the Install and Set Up kubectl page.
  3. You can now create a minikube cluster by entering the following command:
Shell
x
 
1
minikube start
Shell
xxxxxxxxxx
1
 
1
��  minikube v1.5.2 on Darwin 10.15.2
2
✨  Automatically selected the 'hyperkit' driver (alternates: [virtualbox])
3
��  Creating hyperkit VM (CPUs=2, Memory=2000MB, Disk=20000MB) ...
4
��  Preparing Kubernetes v1.16.2 on Docker '18.09.9' ...
5
��  Pulling images ...
6
��  Launching Kubernetes ...
7
⌛  Waiting for: apiserver
8
��  Done! kubectl is now configured to use "minikube"

4. Once the cluster has been created, you can verify its status by entering: 

Shell
xxxxxxxxxx
1
 
1
minikube status


If all is going well, you should see something similar to this:

Shell
xxxxxxxxxx
1
 
1
host: Running
2
kubelet: Running
3
apiserver: Running
4
kubeconfig: Configured


Note that you can also get an overview of your cluster by using the built-in Kubernetes user interface. You can access it by running the following command:

Shell
xxxxxxxxxx
1
 
1
minikube dashboard
Shell
 
xxxxxxxxxx
1
 
1
Verifying dashboard health ...
2
��  Launching proxy ...
3
��  Verifying proxy health ...
4
��  Opening http://127.0.0.1:56993/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...

This opens a web page providing information on the state of your Kubernetes cluster: 

You can also use the dashboard to interact with your Kubernetes cluster. However, this is beyond the scope of this tutorial. If you’re inclined to learn more about creating or modifying Kubernetes resources using the Kubernetes dashboard, please refer to the Web UI (Dashboard) page.

Getting Set Up for Jenkins

To set up Jenkins you will:

  • Create a namespace that allows you to segregate Jenkins objects within the Kubernetes cluster.
  • Create a PersistentVolume to store your Jenkins data. This will preserve data across restarts.

Here’s how you can get your environment set up for Jenkins:

  1. Create a file called jenkins-namespace.yaml with the following content:
Shell
xxxxxxxxxx
1
 
1
apiVersion: v1
2
kind: Namespace
3
metadata:
4
  name: jenkins

2. Apply the spec by entering: 

Shell
xxxxxxxxxx
1
 
1
kubectl apply -f jenkins-namespace.yaml
Shell
xxxxxxxxxx
1
 
1
namespace/jenkins created

3. Paste the following snippet into a file called jenkins-volume.yaml: 

Shell
xxxxxxxxxx
1
14
 
1
apiVersion: v1
2
kind: PersistentVolume
3
metadata:
4
  name: jenkins-pv
5
  namespace: jenkins
6
spec:
7
  storageClassName: jenkins-pv
8
  accessModes:
9
    - ReadWriteOnce
10
  capacity:
11
    storage: 10Gi
12
  persistentVolumeReclaimPolicy: Retain
13
  hostPath:
14
    path: /data/jenkins-volume/

4. Run the following command to apply the spec: 

Shell
xxxxxxxxxx
1
 
1
kubectl apply -f jenkins.volume.yaml

It’s worth noting that, in the above spec, hostPath uses the /data/jenkins-volume/ of your node to emulate network-attached storage. This approach is only suited for development and testing purposes. For production, you should provide a network resource like a Google Compute Engine persistent disk, or an Amazon Elastic Block Store volume.

Install Jenkins

A typical Jenkins deployment is comprised of a master node and, optionally, one or more agents. Jenkins is a complex application that relies on several components and, to simplify the deployment, you’ll use Helm to deploy Jenkins. Helm is a package manager for Kubernetes and its package format is called a chart. Many community-developed charts are available on GitHub.

  1. To enable persistence, you will create an override file and pass it as an argument to the Helm CLI. Paste the content from https://raw.githubusercontent.com/kubernetes/charts/master/stable/jenkins/values.yaml into a file called values.yaml. Then, open the values.yaml file in your favorite text editor and modify the following line:
Shell
 
xxxxxxxxxx
1
 
1
storageClass:


to:

Shell
xxxxxxxxxx
1
 
1
storageClass: jenkins-pv
  1. Now you can install Jenkins by running the helm install command and passing it the following arguments:
  • The name of the release ( jenkins)
  • The -f flag with the name of the YAML file with overrides ( values.yaml)
  • The name of the chart ( stable-jenkins)
  • The --namespace flag with the name of your namespace ( jenkins)
Shell
xxxxxxxxxx
1
 
1
helm install jenkins -f values.yaml stable/jenkins --namespace jenkins


This outputs something similar to the following:

Shell
xxxxxxxxxx
1
14
 
1
NAME: jenkins
2
LAST DEPLOYED: Mon Dec 30 17:26:08 2019
3
NAMESPACE: jenkins
4
STATUS: deployed
5
REVISION: 1
6
NOTES:
7
1. Get your 'admin' user password by running:
8
  printf $(kubectl get secret --namespace jenkins jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo
9
2. Get the Jenkins URL to visit by running these commands in the same shell:
10
  export POD_NAME=$(kubectl get pods --namespace jenkins -l "app.kubernetes.io/component=jenkins-master" -l "app.kubernetes.io/instance=jenkins" -o jsonpath="{.items[0].metadata.name}")
11
  echo http://127.0.0.1:8080
12
  kubectl --namespace jenkins port-forward $POD_NAME 8080:8080
13

          
14
3. Login with the password from step 1 and the username: admin

3. Depending on your environment, it can take a bit of time for Jenkins to start up. Enter the following command to inspect the status of your Pod: 

Shell
xxxxxxxxxx
1
 
1
kubectl get pods --namespace=jenkins


Once Jenkins is installed, the status should be set to Running as in the following output: 

Shell
xxxxxxxxxx
1
 
1
❯ kubectl get pods --namespace=jenkins
2
NAME                       READY   STATUS    RESTARTS   AGE
3
jenkins-645fbf58d6-6xfvj   1/1     Running   0          2m

4. To access your Jenkins server, you must retrieve the password by entering the following command: 

Shell
xxxxxxxxxx
1
 
1
printf $(kubectl get secret --namespace jenkins jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo
Shell
xxxxxxxxxx
1
 
1
Um1kJLOWQY
Shell
xxxxxxxxxx
1
 
1
����Note that your password will be different.

5. Get the name of the Pod running that is running Jenkins using the following command : 

Shell
xxxxxxxxxx
1
 
1
export POD_NAME=$(kubectl get pods --namespace jenkins -l "app.kubernetes.io/component=jenkins-master" -l "app.kubernetes.io/instance=jenkins" -o jsonpath="{.items[0].metadata.name}")


This will create an environment variable called POD_NAME and set its value to the name of the Pod that is running Jenkins.

  1. Use the kubectl command to set up port forwarding:
Shell
xxxxxxxxxx
1
 
1
kubectl --namespace jenkins port-forward $POD_NAME 8080:8080
Shell
 
xxxxxxxxxx
1
 
1
Forwarding from 127.0.0.1:8080 -> 8080
2
Forwarding from [::1]:8080 -> 8080


Add an Executor

The Jenkins documentation defines an executor as “A slot for the execution of work defined by a Pipeline or Project on a Node. A Node may have zero or more Executors configured which corresponds to how many concurrent Projects or Pipelines are able to execute on that Node.” In this section, you will add an executor to your Jenkins node.

  1. Point your browser to http://localhost:8080 and log in using the admin username and the password you retrieved earlier
  2. In the left-hand column, go to Jenkins -> Manage Jenkins -> Manage Node

  1. Select Master and then click Configure

  1. Type “1” in the # of executors input box and then click the Save button

Install and Configure the Node.js Plugin

The Node.js plugin integrates Jenkins with Node.js and, for the scope of this tutorial, you’ll use this plugin to deploy and test a simple web application written in Express.js.

Follow these steps to install the Node.js plugin:

  1. In the left-hand column, go to Jenkins -> Manage Manage Jenkins -> Manage Plugins -> Available Plugins and select the NodeJS plugin. Then, click the Install without restart button.

  1. Once the installation is finished, you’ll see something like the following:

  1. In the left-hand column, go to Jenkins -> Manage Jenkins -> Global Tool Configuration and select Add NodeJS. Type the name of your NodeJS installation, and then choose a NodeJS. version. Once you’re done, click the Save button

The HelloWorld Repository

To showcase how Jenkins works, we created a simple Express.js server that listens on port 3000 and prints “Hello World!”. Then, we wrote a unit test that checks if the web application works as expected. We also updated the package.json file so that Jenkins can execute the unit test with the npm test command. Lastly, we created a Jenkinsfile with the following content:

Shell
 
xxxxxxxxxx
1
26
 
1
pipeline {
2
  agent any
3
    
4
  tools {nodejs "node"}
5
    
6
  stages {
7
        
8
    stage('Cloning Git') {
9
      steps {
10
        git 'https://github.com/andreipope/HelloWorld'
11
      }
12
    }
13
        
14
    stage('Install dependencies') {
15
      steps {
16
        sh 'npm install'
17
      }
18
    }
19
     
20
    stage('Test') {
21
      steps {
22
         sh 'npm test'
23
      }
24
    }      
25
  }
26
}


The above listing defines a three-stage CD pipeline:

  • First, it clones the HelloWorld Git repository
  • Second, it installs the dependencies by running the npm install
  • Lastly, it executes the unit test.

In the next section, you’ll configure Jenkins to run this script.

Create and Run a Pipeline

A pipeline provides a repeatable and consistent process for delivering software. With Jenkins,  you can write your pipeline as a DSL script based on the Groovy programming language.
Follow these steps to create and run the pipeline:

  1. Go to Jenkins -> New Item and enter the name of the pipeline (HelloWorld). Once you’re done, click the OK button.

  1. On the next page, select Pipeline script from SCM and set the Repository URL to “https://github.com/andreipope/HelloWorld”. This is a public repository, meaning that you don’t need to configure any credentials.

Jenkins will automatically retrieve the Jenkinsfile located in the root folder of the repository.
  1. Now you can manually run the pipeline:

  1. Give it some time to run. Once the process is finished, you should see something like the following printed out to the console:
Shell
 
xxxxxxxxxx
1
26
 
1
+ npm test
2

          
3

          
4
> HelloWorld@1.0.0 test /var/jenkins_home/workspace/HelloWorld
5
> mocha
6

          
7

          
8

          
9
  ExpressJS server
10
    ✓ Prints out Hello World (291ms)
11

          
12

          
13
  1 passing (373ms)
14

          
15
[Pipeline] }
16
[Pipeline] // withEnv
17
[Pipeline] }
18
[Pipeline] // stage
19
[Pipeline] }
20
[Pipeline] // withEnv
21
[Pipeline] }
22
[Pipeline] // withEnv
23
[Pipeline] }
24
[Pipeline] // node
25
[Pipeline] End of Pipeline
26
Finished: SUCCESS


Congratulations! You’ve set up Jenkins with Kubernetes and then used it to test a simple web application. Even if you might feel like you’ve covered a lot of ground, the truth is you’ve only got your feet wet with what Jenkins and Kubernetes can do! In future tutorials, we'll walk through examples of more advanced use cases.

Till then, here are some more Kubernetes and Jenkins tutorials that you might find helpful.

Jenkins (software) Kubernetes Docker (software) shell

Published at DZone with permission of Sudip Sengupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Containerize Gradle Apps and Deploy to Kubernetes With JKube Kubernetes Gradle Plugin
  • Create a Kubernetes Cluster With Centos
  • Setup and Configure Velero on AKS

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!