Running Jenkins in Kubernetes Cluster with Persistent Volume

kubernetes

In the DevOps age, we all know the importance of Jenkins. We use Jenkins to build, deploy and automate jobs and projects. Jenkins is supported by thousands of plugins and a huge community. We can find community support easily on the web and in general tech blogs.

We can install and set up Jenkins on different platforms like:

  • Jenkins on On-Premise Bare Server
  • Jenkins on On-Premise Virtual Machine
  • Jenkins on Cloud Virtual Machine
  • Jenkins on Docker

In this section, we are going to learn how can we deploy Jenkins on Kubernetes. For this blog, I’m using Google Kubernetes Engine (GKE) as it’s super easy to set up and manage.

We can set up GKE Cluster in a few clicks or CLI commands. Please check here to deploy the GKE Cluster.

A major challenge in running Jenkins on K8S: Pods running on K8S have a major challenge related to persistent storage. If we don’t define any persistent volume in the storage of K8S Deployment then we’ll lose our data once pods are recreated.

Jenkins stores all of its data in $JENKINS_HOME directory. $JENKINS_HOME is where all Jenkins-based installations store configuration, build logs, artifacts, custom plugins, etc.

To handle this problem we need to configure a persistent volume using GCP-provided Storage Classes.

  • pd-standard — standard persistent disk
  • pd-ssd — premium SSD persistent disk
  • pd-balanced — standard balanced persistent disk

Let’s start with creating our deployment files for Jenkins on GKE:

Prerequisites:

  • K8S Cluster (GKE in our case)
  • kubectl
  • Knowledge of K8S deployment

Persistent Volume Claim:

We need to create and apply PVC using GCP provided Storage Class, we’ll use high-performance ‘pd-ssd’ as our Storage Class Type.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  annotations:
  name: jenkins-pvc
  namespace: jenkins-dev
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 80Gi
  storageClassName: pd-ssd
  volumeMode: Filesystem

Jenkins K8S Deployment:

We now create a K8S Deployment which would use the above-created PVC to persist the data in $JENKINS_HOME

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: jenkins-dev
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
      - image: jenkins/jenkins:lts
        imagePullPolicy: Always
        name: container-0
        ports:
          - name: http-port
            containerPort: 8080
          - name: jnlp-port
            containerPort: 50000
        securityContext:
          allowPrivilegeEscalation: true
          privileged: true
          readOnlyRootFilesystem: false
          runAsUser: 0
        volumeMounts:
          - mountPath: /var/jenkins_home
            name: jenkins-vol
    volumes:
    - name: jenkins-vol
      persistentVolumeClaim:
      claimName: jenkins-pvc

Expose Deployment using K8S Services:

apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: jenkins-dev
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30000
  selector:
    app: jenkins---apiVersion: v1
kind: Service
metadata:
  name: jenkins-jnlp
  namespace: jenkins-dev
spec:
  type: ClusterIP
  ports:
    - port: 50000
      targetPort: 50000
  selector:
    app: jenkins

Please check all the deployed configs using:

kubectl get all -n jenkins-dev

To get the NodePort details from Kubectl:

kubectl get svc -n jenkins-dev

Browse the Jenkins using NodePort (http://nodeIPaddress:nodeport), we will get this screen to provide initialadminpassword

We can get the initialadminpassword in the logs using the following commands:

kubectl get po -n jenkins-dev
kubectl logs <pod-name> -n jenkins-dev

We need to set up a username, password and domain, etc. Then, we are good to log in to Jenkins.

Test the Persistent Volume Data:

To test if the data is persisted, we can delete the deployment or Pod to see the same configurations again.

Also, Read Add Jenkins Slave node in Linux system

AmritMatti

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 5 years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

AmritMatti

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 5 years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

View all posts by AmritMatti →

Leave a Reply

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