Mobile
Streaming Platform Products
Virtual & Dedicated Servers Products
Containers Products
Serverless Computing Products
AI & Machine Learning Products
Private and Hybrid Solutions Products
Monitoring Products
Custom Services Products
Media & Entertainment
Financial Services
IT / Technology
Retail
Education
Website Acceleration
Video Streaming
Security & Protection
Cloud
Partnership Solutions
Corporate Solutions
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:
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:
minikube
by following the steps from the Install minikube pagekubectl
. See the instructions from the Install and Set Up kubectl page.minikube start
😄 minikube v1.5.2 on Darwin 10.15.2 ✨ Automatically selected the 'hyperkit' driver (alternates: [virtualbox]) 🔥 Creating hyperkit VM (CPUs=2, Memory=2000MB, Disk=20000MB) ... 🐳 Preparing Kubernetes v1.16.2 on Docker '18.09.9' ... 🚜 Pulling images ... 🚀 Launching Kubernetes ... ⌛ Waiting for: apiserver 🏄 Done! kubectl is now configured to use "minikube"
minikube status
If all is going well, you should see something similar to this:
host: Running kubelet: Running apiserver: Running 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:
minikube dashboard
Verifying dashboard health ... 🚀 Launching proxy ... 🤔 Verifying proxy health ... 🎉 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.
To set up Jenkins you will:
Here’s how you can get your environment set up for Jenkins:
jenkins-namespace.yaml
with the following content:apiVersion: v1 kind: Namespace metadata: name: jenkins
kubectl apply -f jenkins-namespace.yaml
namespace/jenkins created
jenkins-volume.yaml
:apiVersion: v1 kind: PersistentVolume metadata: name: jenkins-pv namespace: jenkins spec: storageClassName: jenkins-pv accessModes: - ReadWriteOnce capacity: storage: 10Gi persistentVolumeReclaimPolicy: Retain hostPath: path: /data/jenkins-volume/
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.
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.
values.yaml
. Then, open the values.yaml
file in your favourite text editor and modify the following line:storageClass:
to:
storageClass: jenkins-pv
helm install
command and passing it the following arguments:jenkins
)-f
flag with the name of the YAML file with overrides ( values.yaml
)stable-jenkins
)--namespace
flag with the name of your namespace ( jenkins
)helm install jenkins -f values.yaml stable/jenkins --namespace jenkins
This outputs something similar to the following:
NAME: jenkins LAST DEPLOYED: Mon Dec 30 17:26:08 2019 NAMESPACE: jenkins STATUS: deployed REVISION: 1 NOTES: 1. Get your 'admin' user password by running: printf $(kubectl get secret --namespace jenkins jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo 2. Get the Jenkins URL to visit by running these commands in the same shell: 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}") echo http://127.0.0.1:8080 kubectl --namespace jenkins port-forward $POD_NAME 8080:8080 3. Login with the password from step 1 and the username: admin
kubectl get pods --namespace=jenkins
Once Jenkins is installed, the status should be set to Running
as in the following output:
❯ kubectl get pods --namespace=jenkins NAME READY STATUS RESTARTS AGE jenkins-645fbf58d6-6xfvj 1/1 Running 0 2m
printf $(kubectl get secret --namespace jenkins jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo
Um1kJLOWQY
👆🏻 Note that your password will be different.
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.
kubectl
command to set up port forwarding:kubectl --namespace jenkins port-forward $POD_NAME 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080 Forwarding from [::1]:8080 -> 8080
The Jenkins documentation defines an executor as “A slot for 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.
admin
username and the password you retrieved earlierThe 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:
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:
pipeline { agent any tools {nodejs "node"} stages { stage('Cloning Git') { steps { git 'https://github.com/andreipope/HelloWorld' } } stage('Install dependencies') { steps { sh 'npm install' } } stage('Test') { steps { sh 'npm test' } } } }
The above listing defines a three-stage CD pipeline:
npm install
In the next section, you’ll configure Jenkins to run this script.
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:
HelloWorld
). Once you’re done, click the OK button.Jenkins will automatically retrieve the
Jenkinsfile
located in the root folder of the repository.
+ npm test > HelloWorld@1.0.0 test /var/jenkins_home/workspace/HelloWorld > mocha ExpressJS server ✓ Prints out Hello World (291ms) 1 passing (373ms) [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline 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.