How to Install Jenkins on Kubernetes Cluster

Kubernetes provides an automation platform that can manage Jenkins instances, ensuring high availability and fault tolerance. By exploring Kubernetes, Jenkins can dynamically scale to handle varying workloads, optimize resource utilization, and facilitate seamless updates and rollbacks. This integration enhances the efficiency and reliability of the CI/CD process, enabling development teams to deliver software faster and with greater confidence.

In this tutorial, we will demonstrate the step-by-step instructions to install and configure Jenkins on Kubernetes.

How to Install Jenkins on Kubernetes?

Installing Jenkins on Kubernetes offers a scalable and resilient solution for continuous integration and continuous delivery (CI/CD) pipelines. For installing Jenkins on Kubernetes, consider the below steps:

Prerequisites

Before starting the guide, make sure that you meet the following prerequisites:

  • You must install Kubernetes on Ubuntu or any other operating system that you are using.
  • Make sure the kubectl command-line tool is configured to connect to your cluster.
  • Basic knowledge of Kubernetes and Jenkins.

If you meet the prerequisites, you are good to go with the installation process.

Step 1: Create a Namespace for Jenkins

First, create a namespace for Jenkins to keep its resources isolated from other applications:

sudo kubectl create namespace jenkins
create jenkins namespace

Utilize the below command to list existing namespaces:

sudo kubectl get namespaces

The result shows that the jenkins namespace is successfully created:

verify jenkins namespace

Step 2: Create a Service Account

In this step, users can create a service account with Jenkins-specific permissions. The below file defines a cluster role with administrative privileges. It creates a new service account called admin and links it to the previously established cluster role.

Let’s create a file called sa-jenkins.yaml and add the following content:

sudo nano sa-jenkins.yaml

Copy and paste the below lines in the sa-jenkins.yaml file:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: admin
rules:
  - apiGroups: [""]
    resources: ["*"]
    verbs: ["*"]
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin
  namespace: jenkins
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: admin
subjects:
- kind: ServiceAccount
  name: admin
  namespace: jenkins
jenkins.yaml file

After that, apply the configuration by executing the following command:

sudo kubectl apply -f sa-jenkins.yaml
apply configuration

Step 3: Create Persistent Volume and Persistent Volume Claim

Now, creating a persistent volume as well as persistent volume claim for Jenkins data. A persistent volume stores main Jenkins data beyond the lifetime of a pod. Follow the steps below to generate a YAML file that defines the storage-related components of the deployment.

Let’s create a file named volume-jenkins.yaml with the following command:

sudo nano volume-jenkins.yaml

Now add the following content in the newly created file:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-pv
  labels:
    type: local
spec:
  storageClassName: local-storage
  claimRef:
    name: jenkins-pvc
    namespace: jenkins
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  local:
    path: /mnt
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - minikube
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-pvc
  namespace: jenkins
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

The first section of the file defines the local-storage storage class. The second section addresses the Jenkins-pv persistent volume, and the third builds a persistent volume claim called jenkins-pvc that binds to the Jenkins-pv volume.

In the nodeAffinity section, the values contain the name of the node that jenkins will utilize. In the above, minikube is used, hence the node’s name is minikube:

persistent volume claim

After that, apply the configuration with the following command:

sudo kubectl apply -f volume-jenkins.yaml
apply configuration

Step 4: Deploy Jenkins

In this step, create a deployment for Jenkins. A deployment object manages scalability and pod updates, as well as the number of pods that are deployed. Now, create a Jenkins deployment by following the instructions below:

Let’s create a file called deploy-jenkins.yaml and include the following content:

sudo nano deploy-jenkins.yaml

Paste the below script in the created file: 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins-server
  template:
    metadata:
      labels:
        app: jenkins-server
    spec:
      securityContext:
            fsGroup: 1000
            runAsUser: 1000
      serviceAccountName: admin
      containers:
        - name: jenkins
          image: jenkins/jenkins:lts
          resources:
            limits:
              memory: "2Gi"
              cpu: "1000m"
            requests:
              memory: "500Mi"
              cpu: "500m"
          ports:
            - name: httpport
              containerPort: 8080
            - name: jnlpport
              containerPort: 50000
          livenessProbe:
            httpGet:
              path: "/login"
              port: 8080
            initialDelaySeconds: 90
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 5
          readinessProbe:
            httpGet:
              path: "/login"
              port: 8080
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          volumeMounts:
            - name: jenkins-data
              mountPath: /var/jenkins_home
      volumes:
        - name: jenkins-data
          persistentVolumeClaim:
              claimName: jenkins-pvc

The deployment file in this scenario uses the jenkins/jenkins:lts Docker image to generate a single replica that will be exposed on port 8080.

The volumeMounts portion of the file mounts the persistent volume established in the preceding step. The livenessProbe and readinessProbe sections define probes for restarting failed pods and determining when pods are ready:

Now, apply the configuration:

sudo kubectl apply -f deploy-jenkins.yaml
apply configuration

Step 5: Expose Jenkins Service File

Create a service to expose Jenkins. A Kubernetes service is an abstraction that connects Jenkins to the larger network. It enables the user to retain a persistent connection to the pod independent of changes in the cluster.

Let us create a file called service-jenkins.yaml and insert the below content:

sudo nano service-jenkins.yaml

Now, insert the below code to the file:

apiVersion: v1
kind: Service
metadata:
  name: jenkins-svc
  namespace: jenkins
  annotations:
      prometheus.io/scrape: 'true'
      prometheus.io/path:   /
      prometheus.io/port:   '8080'
spec:
  selector:
    app: jenkins-server
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 32000
expose jenkins service file

Apply the configuration by executing the following command:

sudo kubectl apply -f service-jenkins.yaml
apply configuration

Step 6: Install Jenkins Operator Using YAML Files (Optional)

Jenkins on Kubernetes is managed by the Jenkins Operator, a native Kubernetes software extension. You can use the below-given kubectl commands to install it:

sudo kubectl apply -f https://raw.githubusercontent.com/jenkinsci/kubernetes-operator/master/config/crd/bases/jenkins.io_jenkins.yaml
create jenkins operator file

After that, install the Jenkins operator through the YAML files as below:

sudo kubectl apply -f https://raw.githubusercontent.com/jenkinsci/kubernetes-operator/master/deploy/all-in-one-v1alpha2.yaml
install jenkins operator

To see the operator become ready, add the -w option to the kubectl get command as shown below:

sudo kubectl get pods -w
see operatory status

If you find any connection-relevant error, execute the “minikube start –force” command.

Step 7: Access Jenkins (Post-Installation Setup)

When you initially access Jenkins, you will be requested to enter an initial admin password. The output’s status portion contains the node IP address:

sudo kubect get nodes -o yaml
post installation setup

Once Jenkins is deployed and the service is created, users can easily access Jenkins through the NodePort. Open your browser and navigate to http://<NodeIP>:32000:

access jenkins

Look for the pod’s Name in the output of the following command to obtain the password:

sudo kubectl get pods -n jenkins
get password

Now, retrieve the password from the Jenkins pod:

sudo kubectl logs [pod_name] --namespace jenkins
retrieve password from jenkins pod

Locate the password, which is a lengthy alphanumerical string, at the end of the log:

find password

Users can copy the given password and put it into the Jenkins unlock window:

unlock jenkins

Step 8: Install Suggested Plugins

After you unlock Jenkins, you will be prompted to install suggested plugins. Here, pick the Select plugins to install to start option:

customize jenkins

Follow the on-screen instructions to complete the installation. The left side of the page displays a list of plugins arranged by category. Pick the plugins to use and click the Install button:

install plugins

Step 9: Create an Admin User

Now, create an admin user to manage Jenkins. After entering the required information in the user creation form, click Save and Continue button:

create admin user

Step 10: Configure Jenkins

Once the setup is complete, you can start configuring Jenkins according to your needs:

jenkins dashboard

You have successfully installed Jenkins on Kubernetes. Users can now start creating as well as managing CI/CD pipelines.

Conclusion

Installing Jenkins on Kubernetes is essential for achieving a robust and scalable CI/CD pipeline. Kubernetes provides an automation platform that ensures high availability, fault tolerance, and efficient resource management for Jenkins instances. This setup allows Jenkins to handle varying workloads dynamically, optimize resource utilization, and facilitate seamless updates and rollbacks. By searching Kubernetes, development teams can enhance the reliability and efficiency of their CI/CD processes, ultimately accelerating software delivery and improving overall productivity.

We hope this guide has helped you install Jenkins on Kubernetes. Consider Ultahost’s Linux VPS hosting which provides maximum flexibility and control. Ultahost handles all administrative tasks to ensure your servers operate smoothly, efficiently, and with reliable uptime. Buy VPS now and take your online presence to the next level!

FAQ

What are the benefits of installing Jenkins on Kubernetes?
How do I create a namespace for Jenkins in Kubernetes?
What is the purpose of creating a service account for Jenkins?
How do I create a persistent volume for Jenkins Kubernetes deployment?
How can I expose the Jenkins service on Kubernetes?
How do I deploy Jenkins on Kubernetes?
What is the Jenkins Operator, and how do I install it?

Related Post

How to Deploy WordPress Instance on Kubernete

Kubernetes, a powerful container orchestration platform...

How to Install Jenkins on Windows

Jenkins is an open-source automation server widely used...

How to Install Kubernetes on Ubuntu 22.04

Kubernetes is an open-source container orchestration sy...

How to Create Kubernetes Namespace

Kubernetes, a popular container orchestration platform ...

How To Install Jenkins on Ubuntu 22.04

Jenkins is a powerful open-source automation server tha...

How to Deploy Kafka on Kubernetes

Apache Kafka is a distributed streaming platform that e...

Leave a Comment