Running Jenkins in Kubernetes Cluster with Persistent Volume

Vishal Sharma
3 min readSep 23, 2021

In DevOps age, we all know the important of Jenkins. We use Jenkins to building, deploying and automating jobs and projects. Jenkins is supported by thousands of plugins and huge community. We can find community support easily on web and 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 few clicks or CLI commands. Please check here to deploy the GKE Cluster.

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

Jenkins stores all of it’s data in $JENKINS_HOME directory. $JENKINS_HOME is where all Jenkins-based installations store configuration, build logs, and 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 config 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 following commands:

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

We need to setup 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.

Cheers ……

--

--

Vishal Sharma

IT Enthusiast, Professional, Novice Sportsperson and Fantasist of Better World. @byVishalSharma on Twitter.