How to Set Up and Run Jenkins on Kubernetes Clusters

2436 VIEWS

·

Jenkins is a powerful continuous integration/continuous delivery and deployment (CI/CD) platform that is Java-based. It runs in servlet containers. Jenkins also automates the building, testing, and deploying of software in the software development life cycle (SDLC). It enables developers to quickly and continually integrate source code changes into the master project. 

Jenkins configures CI/CD pipelines to automate the continuous integration/continuous delivery and deployment of software. With Jenkins, you can install various plugins to help you with continuous integration and deployment. 

Jenkins increases productivity in teams and organizations through the automation of repetitive tasks. This tutorial will guide you on how to set up and run Jenkins on Kubernetes clusters.

How to Setup and Run Jenkins on Kubernetes Cluster

There are various ways of setting up Jenkins on Kubernetes such as:

  • Installing Jenkins with Helm and Helm Charts
  • Installing Jenkins with Jenkins Operator
  • Installing Jenkins with Deployment and Service YAML files

In this tutorial, we will install Jenkins on Kubernetes Cluster with Deployment and Service YAML files. To install Jenkins on Kubernetes using this method, we will follow the following steps:

  1. Create and run a Kubernetes Cluster.
  2. Create a Jenkins namespace in your Kubernetes Cluster.
  3. Create a jenkins-deployment.yaml file
  4. Create a jenkins-service.yaml file
  5. Exposing the Jenkins Kubernetes Service
  6. Accessing Jenkins running inside your Kubernetes Cluster
  7. Run a Jenkins Pipeline on Kubernetes Cluster

Let’s start implementing these steps and procedures:

Step One: Create and Run a Kubernetes Cluster

Kubernetes is an open-source platform that automates the management and deployment of containers. A Kubernetes Cluster is a Kubernetes deployment environment that has a set of master and worker nodes that run the containers.

We have different Kubernetes clusters that DevOps engineers can create such as Amazon EKS cloud clusterLinode Kubernetes EngineCivo KubernetesGoogle Kubernetes EngineDigitalOcean Kubernetes and Azure Kubernetes Service. In this tutorial, we will use the Minikube local Kubernetes Cluster. To get more details on Minikube, read this article on Sweet Code.

NOTE: If you have any other Kubernetes cluster, you can use it. The process of installing Jenkins will be the same no matter which Kubernetes cluster you are using.

Before you continue, ensure you have Docker running on your machine. If you do not know how to run Docker, read this article on Sweet Code. To start and run Minikube, run this command in your terminal:

minikube start

The command will start Minikube and display the following output in your terminal:

From the image above, we can see Minikube has started. Kubectl is configured to use the Minikube cluster. We can now run and apply the Kubectl command on the Minikube cluster.

Step Two: Create a Jenkins Namespace in Your Kubernetes Cluster

Now that we have started the Minikube cluster, let’s create a Jenkins namespace in the Minikube cluster. You will run the following kubectl command in your terminal:

kubectl create namespace jenkins

The command will create a ‘jenkins’ namespace in the Minikube Kubernetes Cluster. To get the created ‘jenkins’ namespace, apply the following kubectl command in your terminal:

kubectl get namespaces

The command will display the following namespaces in your terminal:

The output shows the ‘jenkins’ namespace is active, and we can start using the namespace. We will create the Jenkins Deployment and Service in this namespace.

Step Three: Create a jenkins-deployment.yaml File

We will create a ‘jenkins-deployment.yaml’ file in our local machine and then apply it to the Minikube Kubernetes Cluster. In your computer, create a ‘jenkins-deployment.yaml’ file and add the following YAML snippet:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins #name of the Jenkins Kubernetes Deployment
spec:
  replicas: 1 #number of Jenkins pods created in the Minikube Kubernetes Cluster
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
      - name: jenkins #name of the Jenkins container
        image: jenkins/jenkins:lts-jdk11 #name of the Jenkins image used to create the Jenkins container
        ports:
        - containerPort: 8080 #the port in which the Jenkins container will run inside the Minikube Kubernetes Cluster
        volumeMounts: # Volumes for mounting and storing the Jenkins plugins
        - name: jenkins-home
          mountPath: /var/jenkins_home #Location of the Jenkins volume
      volumes:
      - name: jenkins-home
        emptyDir: { }

The ‘jenkins-deployment.yaml’ will create a Jenkins Kubernetes Deployment in the ‘jenkins’ namespace. The Jenkins deployment.yaml defines the Jenkins image (jenkins/jenkins:lts-jdk11) that will create the Jenkins container. This file also has other container volume configurations for mounting the Jenkins container onto the Kubernetes cluster. It will expose port 8080. The port in which the Jenkins container will run inside the Minikube Kubernetes Cluster.

The Jenkins Kubernetes Deployment will create one pod (replica) for the Jenkins application. It will also assign Kubernetes resources to the Kubernetes application. For a detailed understanding on Kubernetes YAML files, you can read this article on Sweet Code. 

Applying the `jenkins-deployment.yaml` to Your Kubernetes Cluster

To apply this file to your Kubernetes cluster, run the following kubectl command in your terminal:

#Create a Jenkins Kubernetes Deployment in the `jenkins` namespace
kubectl apply -f jenkins-deployment.yaml --namespace jenkins

After a few minutes, Kubernetes will use the Jenkins images to create a Jenkins Kubernetes Deployment in the `jenkins` namespace. To get the created Jenkins Kubernetes Deployment, apply the following kubectl command in your terminal:

kubectl get deployment --namespace jenkins

The kubectl command displays the following deployments in your terminal:

It has created the Jenkins Kubernetes Deployment. To get the running Jenkins pod, apply the following kubectl command in your terminal:

kubectl get pod --namespace jenkins

The kubectl command displays the following Kubernetes Deployment in your terminal:

Step Four: Create a jenkins-service.yaml File

We will create a ‘jenkins-service.yaml’ file in our local machine and then apply it to the Minikube Kubernetes Cluster. In your computer, create a ‘jenkins-service.yaml’ file and add the following YAML snippet:

apiVersion: v1
kind: Service
metadata:
  name: jenkins #name of the Jenkins Kubernetes Service
spec:
  type: NodePort #Type of the Jenkins Kubernetes Service
  ports:
  - port: 8080 #port for the Jenkins Kubernetes Service
    targetPort: 8080 #port for the Jenkins pod
  selector:
    app: jenkins

The ‘jenkins-service.yaml’ will create a Jenkins Kubernetes Service in the ‘jenkins’ namespace. The file will create a ‘NodePort’ Kubernetes Service. It will expose the running Jenkins pods on port 8080 outside the Minikube cluster. This will enable us to access the Jenkins platform in a web browser on port 8080.

Applying the ‘jenkins-service.yaml’ to Your Kubernetes Cluster

To apply this file to your Kubernetes cluster, run the following kubectl command in your terminal:

#Create a Jenkins Kubernetes Service in the `jenkins` namespace
kubectl apply -f jenkins-service.yaml --namespace jenkins

To get the created Jenkins Kubernetes Service, apply the following kubectl command in you terminal:

kubectl get service --namespace jenkins

The kubectl command displays the following Kubernetes Service in your terminal:

Step Five: Exposing the Jenkins Kubernetes Service

To expose the Jenkins Kubernetes Service, you will need to know the IP address of your Minikube Kubernetes Cluster. To get the IP address of the Minikube Kubernetes Cluster, apply the following Minikube command in your terminal:

minikube ip

It gives the following output:

192.168.145.146

You will use this Minikube IP address and the ‘NodePort’ service port number to access Jenkins running inside the Minikube Kubernetes Cluster.

Step Six: Accessing Jenkins Running Inside the Minikube Kubernetes Cluster

To access Jenkins running inside the Minikube Kubernetes Cluster, open your browser and add the following URL: 192.168.145.146:32217. The URL will display an `Unlock Jenkins` page as shown below:

You will use your initial admin password to unlock Jenkins inside the Minikube Kubernetes Cluster. To get your initial admin password, apply the following kubectl command in your terminal:

kubectl --namespace jenkins logs jenkins

The kubectl command displays the following output in your terminal:

Copy the initial admin password displayed in your terminal and paste it into the text area to unlock Jenkins. You will then click continue. A ‘Customize Jenkins’ page will be displayed as shown below:

Additional Steps

Follow the additional steps below to complete setting up Jenkins:

Click Install Suggested Plugins

It will install the Plugins that the Jenkins community finds most useful and common in CI/CD operations. It will immediately start installing the plugins as shown below:

Be patient and wait for the installation to finish. 

Create First Admin User

When the installation is complete, you will create the first admin user as shown in the image:

Input your details to create a first admin user and click `Save and Continue`. You will use this admin user the next time you want to login into Jenkins.

Instance Configuration

You will add the Jenkins URL as shown below:

After adding the Jenkins URL, click `Save and Finish`. You will have completed setting up Jenkins, and it will be ready to use as shown below:

You will then click ‘Start using Jenkins’. It will launch a Jenkins Dashboard where you can start creating your CI/CD pipelines as shown below:

Step Seven: Run a Jenkins Pipeline on Kubernetes Cluster

Jenkins is a powerful tool for creating CI/CD pipelines. In this tutorial, we will build a simple pipeline that outputs `Hello World`. The Jenkins pipeline will have only one stage named ‘Hello’. To run this pipeline, follow these steps:

1. Create a job

In the Jenkins Dashboard, click ‘Create a job’ as shown below:

2. Enter the Item Name

A new page will appear, and you will enter a new item name as shown below:

You will select ‘Pipeline’ and then click ‘OK’. You will then be redirected to another page where you will configure the pipeline script.

3. Configure Pipeline Script

To configure the pipeline script, select the ‘Hello World’ script and then click ‘Apply’:

4. Click ‘Build Now’

It will start the Hello World Jenkins pipeline:

5. Click ‘Console Output’

It will display the output of our Hello world pipeline:

Our Jenkins pipeline has completed building and outputs ‘Hello world’. We have successfully set up and run Jenkins on Kubernetes Cluster.

Conclusion

In this tutorial, you have learned how to set up and run Jenkins on Kubernetes Cluster. First, we installed Jenkins with Deployment and Service YAML files. We started by creating and running a Minikube Kubernetes Cluster Create. Then, we created a ‘jenkins-deployment.yaml’ file and ‘jenkins-service.yaml’ file. 

Using these files, we created the Jenkins Kubernetes Deployment and Service inside the Minikube Kubernetes Cluster. Additionally, we then accessed Jenkins running inside the Minikube Kubernetes Cluster. We followed additional steps to complete setting up Jenkins. 

Finally, we used Jenkins to build a simple pipeline that outputs ‘Hello World’. This pipeline is simple, but you can still leverage the power of Jenkins and build more sophisticated Jenkins Pipelines.


Lloyd Matereke is a DevOps engineer and technical writer. He has experience with Docker, Docker Compose, Kubernetes, CI/CD tools and Infrastructure as Code tools. He has deployed multiple containerized applications to Kubernetes cluster. He also has experience with Kubernetes architecture, Jenkins and creating infrastructure using Terraform. He has written various technical articles explaining DevOps tools and concepts.


Discussion

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu
Skip to toolbar