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: 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 Create a namespace and a PersistentVolume for Jenkins Install Jenkins using Helm Set up and run a pipeline that tests a simple web application Prerequisites Docker. For details about installing Docker, refer to the page. Install Docker A Kubernetes cluster. If you don’t have a running Kubernetes cluster, see the “Create a Kubernetes Cluster with minikube” section. Helm CLI. To install Helm CLI, follow the instructions from the page. Installing Helm 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 page Install minikube 2. Install kubectl. See the instructions from the page. Install and Set Up kubectl 3. You can now create a minikube cluster by entering the following command: minikube 😄 minikube v1 Darwin ✨ Automatically selected the driver (alternates: [virtualbox]) 🔥 Creating hyperkit VM (CPUs= , = MB, Disk= MB) ... 🐳 Preparing Kubernetes v1 Docker ... 🚜 Pulling images ... 🚀 Launching Kubernetes ... ⌛ Waiting : apiserver 🏄 Done! kubectl configured start .5 .2 on 10.15 .2 'hyperkit' 2 Memory 2000 20000 .16 .2 on '18.09.9' for is now to use "minikube" 4. Once the cluster has been created, you can verify its status by entering: minikube status If all is going well, you should see something similar to this: Running Running Running Configured host: kubelet: apiserver: kubeconfig: 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: . : v1 kubernetes-dashboard http:kubernetes-dashboard: your default browser... // 127.0 0.1 56993 /api/ /namespaces/ /services/ /proxy/ in 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 page. Web UI (Dashboard) 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 with the following content: jenkins-namespace.yaml v1 Namespace jenkins apiVersion: kind: metadata: name: Apply the spec by entering: kubectl apply -f jenkins- . / namespace yaml namespace jenkins created 3. Paste the following snippet into a file called : jenkins-volume.yaml v1 PersistentVolume jenkins-pv jenkins jenkins-pv - ReadWriteOnce Gi Retain jenkins-volume/ apiVersion: kind: metadata: name: namespace: spec: storageClassName: accessModes: capacity: storage: 10 persistentVolumeReclaimPolicy: hostPath: path: /data/ 4. Run the following command to apply the spec: 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 to deploy Jenkins. Helm 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 into a file called . https://raw.githubusercontent.com/kubernetes/charts/master/stable/jenkins/values.yaml values.yaml Then, open the file in your favourite text editor and modify the following line: values.yaml storageClass: to: jenkins-pv storageClass: 2. Now you can install Jenkins by running the command and passing it the following arguments: helm install The name of the release ( ) jenkins The flag with the name of the YAML file with overrides ( ) -f values.yaml The name of the chart ( ) stable-jenkins The flag with the name of your namespace ( ) --namespace jenkins helm install jenkins -f .yaml /jenkins values stable --namespace jenkins This outputs something similar to the following: NAME: jenkins LAST DEPLOYED: Mon Dec : : NAMESPACE: jenkins STATUS: deployed REVISION: NOTES: Get your 'admin' user password by running: printf ;echo Get the Jenkins URL visit by running these commands the same shell: export POD_NAME= echo http: kubectl --namespace jenkins port-forward $POD_NAME : Login the password from step the username: admin 30 17 26 08 2019 1 1. $( -- - = | -- ) kubectl get secret namespace jenkins jenkins o jsonpath "{.data.jenkins-admin-password}" base64 decode 2. to in $( -- - - - = ) kubectl get pods namespace jenkins l "app.kubernetes.io/component=jenkins-master" l "app.kubernetes.io/instance=jenkins" o jsonpath "{.items[0].metadata.name}" //127.0.0.1:8080 8080 8080 3. with 1 and 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: kubectl pods =jenkins get --namespace Once Jenkins is installed, the status should be set to as in the following output: Running ❯ kubectl get pods --namespace=jenkins NAME READY STATUS RESTARTS AGE jenkins fbf58d6 xfvj / Running m -645 -6 1 1 0 2 4. To access your Jenkins server, you must retrieve the password by entering the following command: printf $(kubectl --namespace jenkins jenkins -o = | base64 --decode);echo Um1kJLOWQY get secret jsonpath "{.data.jenkins-admin-password}" 👆🏻Note that your password will be different. 5. Get the name of the Pod running that is running Jenkins using the following command : 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 and set its value to the name of the Pod that is running Jenkins. POD_NAME 6. Use the command to set up port forwarding: kubectl kubectl --namespace jenkins port-forward $POD_NAME : Forwarding : -> Forwarding [:: ]: -> 8080 8080 from 127.0 .0 .1 8080 8080 from 1 8080 8080 Add an Executor 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. 1. Point your browser to and log in using the username and the password you retrieved earlier http://localhost:8080 admin 2. In the left-hand column, go to Jenkins -> Manage Jenkins -> Manage Node 3. Select and then click Master Configure 4. Type “1” in the input box and then click the button # of executors Save 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 and select the plugin. Then, click the button. Jenkins -> Manage Manage Jenkins -> Manage Plugins -> Available Plugins NodeJS Install without restart 2. Once the installation is finished, you’ll see something like the following: 3. In the left-hand column, go to and select . Type the name of your NodeJS installation, and then choose a NodeJS. version. Once you’re done, click the button Jenkins -> Manage Jenkins -> Global Tool Configuration Add NodeJS Save 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 command. npm test Lastly, we created a with the following content: Jenkinsfile pipeline { agent any tools {nodejs } stages { stage( ) { steps { git } } stage( ) { steps { sh } } stage( ) { steps { sh } } } } "node" 'Cloning Git' 'https://github.com/andreipope/HelloWorld' 'Install dependencies' 'npm install' 'Test' 'npm test' 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 and enter the name of the pipeline ( ). Once you’re done, click the button. Jenkins -> New Item HelloWorld OK 2. On the next page, select and set the to “ . This is a public repository, meaning that you don’t need to configure any credentials. Pipeline script from SCM Repository URL https://github.com/andreipope/HelloWorld” Jenkins will automatically retrieve the located in the root folder of the repository. Jenkinsfile 3. Now you can manually run the pipeline: 4. Give it some time to run. Once the process is finished, you should see something like the following printed out to the console: + > @ / / / / > ✓ ( ) ( ) } } } } } : 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.